Go Down

Topic: Speed Control for Lawn tractor  (Read 315 times) previous topic - next topic

Vetsoltech

Hi

I've finally managed to get the code done for my speed control circuit (I think !).

I know about dangers in implementing it in vehicles and all but anyways..
I'm also aware of limitations of hobby servos, etc, etc


Please bear in mind that I haven't built in any safety features......yet.

I will install it on my lawn mower tractor for testing (with a killswitch!)

Why? just for fun!!  :)

Please take a look if you like and feel free to comment, I'd be happy hear if it's possible to make it better.

Code: [Select]
#include <Servo.h>

Servo myservo;

int potpin = 1;
int off;
int hall_pin = 2;
int val1, val2, pos;
int v;
int vmax;
float timetaken, rpm, Time;
float wheellenght = 0.157; //diameter*pi=0,05*3,14
volatile byte counter = 0;
unsigned long passedtime = 0;


void setup()
{
 Serial.begin(9600);
 attachInterrupt(0, magnet_detect, RISING);
 pinMode(hall_pin,INPUT);
 pinMode(off,INPUT);
 myservo.attach(8);
 counter = rpm = passedtime = 0;
 val2 = 0;
 vmax = 30;
}

void loop()
{
 
 byte offpin = digitalRead(off);    //read speed control button
 
 //If speed control is off
 
 if (offpin == 0){         
 val1 = analogRead(potpin);
 pos = map(val1, 0, 1023, 0, 180);
 myservo.write(pos);
  }

//If speed control is on

else  {

  v = wheellenght* rpm *60;                               // km/h calc
  val1 = analogRead(potpin);                              //get gas pedal potentiometer value
  pos = map(val1, 0, 1023, 0, 180);                       //convert it to servo
 
  if (val2 < pos && val2 > 0){                             //checks if gas pedal is more than the too high value below and not 0 (to prevent it to take affect first loop).
  pos = val2;                                               //set the gaspedal value to the reduced value below
  }
 
  if (v <= vmax){                                         //if the speed is below or on limit move throttle   
     
  myservo.write(pos);
  Serial.println("under");
  Serial.println(v);
  }
 

 
 else {                                             
  if (val2 > 0){                                          //reduces the throttle servo by 10 if it's not the first loop
  for (val2 = val2; val2 = val2 -10; val2 -=1);
  myservo.write(val2);
  Serial.println("Bromsar");
  Serial.println(v);
  }
  else {                                                  //reduces the throttle servo by 10 if it's the first loop
  val2 = pos;                                             // gives val2 the gas pedal value
  for (val2 = val2; val2 = val2 -10; val2 -=1);
  myservo.write(val2);
  Serial.println("Bromsar");
  Serial.println(v);
  }
 }
       
   
}
}


 void magnet_detect() //Called whenever a magnet is detected
 {
   counter++;
   Time=millis();
   if(counter>=2)
   {
     timetaken = Time-passedtime; //time in millisec for 2nd rotation
     rpm=(1000/timetaken)*60;    //formulae to calculate rpm
     passedtime = Time;
     counter=0;
 
   
   }
 }

UKHeliBob

Some comments
Code: [Select]
int potpin = 1;
Although this is correct because analogRead() will interpret the pin number correctly it is probably safer and easier to understand if you use
Code: [Select]
const byte potpin = A1;
A byte is big enough and the number will never change in the code, hence const

More of a worry are the variable types used in timing using millis()
Code: [Select]
    timetaken = Time - passedtime; //time in millisec for 2nd rotation
All of these variables should be of type unsigned long because that is what millis() returns.  They should certainly not be floats, and you don;t want any problems when/if variables overflow their maximum values or go negative

Not actually a problem, but as you read the value of potpin whether or not offpin equals zero or not then why not read it unconditionally at the start of loop() and while you are at it give the variable a more descriptive name than val1.  The v variable deserves a better name too, don't you think ?

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Vetsoltech

Hi!

Good points there. Thanks alot!

TomGeorge

Hi,
Have you tried your code, or are you writing it all at once before checking output.

What model Arduino are you using?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can I suggest you google    arduino tacho
It is not advisable to do all that coding in the interrupt function.

Thanks.. Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

david_2018

What exactly are you trying to do with the FOR statement?  

Code: [Select]

 for (val2 = val2; val2 = val2 -10; val2 -=1);


val2 = val2 - 10 will not compare val2 with ( val2 - 10 ) , but will set val2 equal to ( val2 - 10 )

even if you intended to compare val2 with ( val2 - 10 ) , that will always be a false statement.

the ; at the end terminates the FOR statement, so there is no code to execute.

Vetsoltech

What exactly are you trying to do with the FOR statement? 

Code: [Select]

 for (val2 = val2; val2 = val2 -10; val2 -=1);


val2 = val2 - 10 will not compare val2 with ( val2 - 10 ) , but will set val2 equal to ( val2 - 10 )

even if you intended to compare val2 with ( val2 - 10 ) , that will always be a false statement.

the ; at the end terminates the FOR statement, so there is no code to execute.
Hi

Ooops, I missed that one ( ; ). Regarding the for statement, I want the servo to start at val2 value and gradually decrease the value by steps of one, ending on 10 less than val2.

Vetsoltech


Hi,
Have you tried your code, or are you writing it all at once before checking output.

What model Arduino are you using?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can I suggest you google    arduino tacho
It is not advisable to do all that coding in the interrupt function.

Thanks.. Tom... :)

[/quote]

The code above haven't been tried yet although the main part of it has been tested in a test rig earlier. I've allways been stuck on the part were the speed exceeds the limit and not been able to get the servo to gradually decrease. Another bottleneck has been, if the gas pedal is floored and the system decreases the gas within limit, what would the effect be then or more importantly how would I avoid that effect.

I'll rig the test rig up again and run it. I'll get back with a schetch.

By the way, it's an Uno.

Also, about the tachometer on Google, been there, done that. Alot

Go Up