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.
#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;
 Â
 }
}