Go Back   RIBnet Forums > RIB talk > Other stuff
Click Here to Login

Reply
 
Thread Tools Search this Thread
 
Old 10 March 2021, 11:14   #1
Member
 
Country: UK - England
Town: Kent
Boat name: ever dry
Make: Elling KB350
Length: 3m +
Engine: Yamaha 15hp 2 stroke
Join Date: Jun 2020
Posts: 626
Arduino Nano sketch format?

Hoping there are some Arduino programmers on here, I have a sketch to run to interface with a Mosfet on another board to drive a variable speed DC motor. The mosfet drive is from D3 via a BC557 PNP transistor switching a 12V supply.


The sketch was given to me as below, I really dont want to go learn all about how to programme this if some kind soul can just give it the once over for me please. Can I assume the */ is not part of the sketch? Also the double } at the end will make no difference?
I compiled and uploaded with */ removed using Arduino IDE.



*/

float hiTime;
float loTime;
int current;

void setup() {
pinMode(3, OUTPUT);
}

void loop() {
loTime = (analogRead(2) / 3.2) - 30; // Read and scale control voltage , every 0.34 secs
// remove offset (-100) to correct speed
if (loTime < 0) loTime = 0;
hiTime = 340 - loTime; // hiTime is balance of 50µsec period (when MOSFET to be OFF)
if (hiTime < 0) hiTime = 0;
current = analogRead(0); // Obtain value to represent current through MOSFET (read from source resistor)
if (hiTime < 339) { // If above low limit, speed control
for (int i = 0; i < 1000; i++) {
digitalWrite(3, HIGH); // generate fast PWM burst of 1000 cycles
delayMicroseconds(hiTime);
digitalWrite(3, LOW);
delayMicroseconds(loTime);
}
}
else { // If near low limit, stop motor
digitalWrite(3, HIGH);
delay(340);
}
}
__________________
Oldman2 is offline   Reply With Quote
Old 10 March 2021, 19:22   #2
Member
 
Country: UK - England
Town: Barnstaple
Length: 7m +
Join Date: Mar 2020
Posts: 390
If you format it it will look like this which is a lot more readable...

PHP Code:
float hiTime;
float loTime;
int current;

void setup() {
  
pinMode(3OUTPUT);
}

void loop() {
  
loTime = (analogRead(2) / 3.2) - 30// Read and scale control voltage , every 0.34 secs
  // remove offset (-100) to correct speed
  
if (loTime 0loTime 0;
  
hiTime 340 loTime// hiTime is balance of 50µsec period (when MOSFET to be OFF)
  
if (hiTime 0hiTime 0;
  
current analogRead(0); // Obtain value to represent current through MOSFET (read from source resistor)
  
if (hiTime 339) { // If above low limit, speed control
    
for (int i 01000i++) {
      
digitalWrite(3HIGH); // generate fast PWM burst of 1000 cycles
      
delayMicroseconds(hiTime);
      
digitalWrite(3LOW);
      
delayMicroseconds(loTime);
    }
  } else { 
// If near low limit, stop motor
    
digitalWrite(3HIGH);
    
delay(340);
  }

The double parenthesis are needed as the first is closing the if else statement and the second is closing the main loop of the program.

The */ is just the end operator of a multi line comment.

PHP Code:
/* comment this code
And this code */ 
Hope that helps
__________________
jakew009 is offline   Reply With Quote
Old 10 March 2021, 19:39   #3
Member
 
Country: UK - England
Town: Kent
Boat name: ever dry
Make: Elling KB350
Length: 3m +
Engine: Yamaha 15hp 2 stroke
Join Date: Jun 2020
Posts: 626
Super Jake thanks, I did upload it to my Nano today with */ removed and I did leave the double parenthesis in so all is good.
I just need to remove the dead Mosfet gate control circuit now and wire this Nano in and we should be good to go.
Many thanks.
__________________
Oldman2 is offline   Reply With Quote
Old 21 March 2021, 23:26   #4
Member
 
Country: UK - England
Town: Kent
Boat name: ever dry
Make: Elling KB350
Length: 3m +
Engine: Yamaha 15hp 2 stroke
Join Date: Jun 2020
Posts: 626
Hi Jake, I finished replacing Mosfets today and ran the sketch, its sending pwm control to a mosfet gate to control the speed of a dc motor. The motor has a knock noise every .3 of a second.
If I just comment out // this line to stop the analog read that will still allow all else to continue as before yes?

current = analogRead(0); // Obtain value to represent current through MOSFET (read from source resistor)


The knock noise is not mechanical btw its very much sketch instigated. Many thanks.
__________________
Oldman2 is offline   Reply With Quote
Old 22 March 2021, 00:28   #5
Member
 
Country: UK - England
Town: Kent
Boat name: ever dry
Make: Elling KB350
Length: 3m +
Engine: Yamaha 15hp 2 stroke
Join Date: Jun 2020
Posts: 626
Ok I have the answer now Jake. Thanks
__________________
Oldman2 is offline   Reply With Quote
Old 22 March 2021, 08:05   #6
Member
 
Country: UK - England
Town: Barnstaple
Length: 7m +
Join Date: Mar 2020
Posts: 390
Quote:
Originally Posted by Oldman2 View Post
Ok I have the answer now Jake. Thanks
What was it?
__________________
jakew009 is offline   Reply With Quote
Old 22 March 2021, 08:39   #7
Member
 
Country: UK - England
Town: Kent
Boat name: ever dry
Make: Elling KB350
Length: 3m +
Engine: Yamaha 15hp 2 stroke
Join Date: Jun 2020
Posts: 626
Seems the pwm was stopping 3 times a second to read the current draw and I need to // the line out and that should remove the knocking. Seems a bit clunky to me but then I cant sketch write to save my life.
The original pwm module was huge (size of a fag packet) but potted up so no idea what was inside, so this Nano, a few resistors and a transistor takes its place but the original read the current and speed independent of taking a motor speed input command. Hopefully when I enter the compiled sketch today the knock will be gone, I then need to figure out how to get the speed selected to be closer to the actual speed of the motor via the sketch.
__________________
Oldman2 is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 22:05.


Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2024, vBulletin Solutions, Inc.