Go Down

Topic: Speed limiter using hall sensor (Read 409 times) previous topic - next topic

Vetsoltech

Hi



I'm trying to build a speed limiter to a ATV.

My thought is to replace the wire to the  throttle control with a servo using a potentiometer at the pedal.

To limit the speed I'll use a hall effect sensor on one of the forward driveshafts.

The speed limit has a on/off button so that if it's on the atv only does 30 km/h regardless if the high or low gear is in.

I've managed to write som code but I can't get it to work properly. The problem appears to be that the hall sensor only gives readings once (when all the code is active). If I deactivate the part that is suppose to regulate the servo the hall sensor works.

Can anyone please give me some suggestions on how to solve it ?

I appologise for the uggly code in advanced!

ps. attached the code also....

#include <Servo.h>

Servo myservo;

int potpin = 1;
int val1;
int val2;
int rpm1;
int off = 3;
const int rpmt = 30000;
int hall_pin = 2;
int decl;
volatile int counter = 0;
int rpm;
unsigned long passedtime = 0;
void rpmf(){
  counter++;
}

void setup()
{ Serial.begin(9600);
attachInterrupt(0, rpmf, FALLING);
  pinMode(hall_pin,INPUT);
  pinMode(off,INPUT);
 
  rpm = 0;
//  rpmt = 30000;
  decl = 60000;
myservo.attach(8);
}

void loop()
{
 
 byte offpin = digitalRead(off);
 
 if (offpin == 0){
  val1 = analogRead(potpin);         
  val1 = map(val1, 0, 1023, 0, 180);
  myservo.write(val1);}

else if (millis() - passedtime == 1000){
 detachInterrupt(0);
rpm = counter * 60;
 rpm1=rpm;
 Serial.print("rpm1=");
 Serial.println(rpm1);
  counter = 0;
passedtime = millis();
attachInterrupt(0,rpmf,FALLING);}
 val1 = analogRead(potpin);         
  val1 = map(val1, 0, 1023, 0, 180);
  myservo.write(val1);
 
   

 

 if (rpm1 < rpmt){
  val1 = analogRead(potpin);         
  val1 = map(val1, 0, 1023, 0, 180);
  myservo.write(val1);
  Serial.println("under");
  Serial.println(rpm);
  Serial.println(rpmt);
  }
 
  else if(rpm1 == rpmt){
  val1 = analogRead(potpin);       
  val1 = map(val1, 0, 1023, 0, 180);
  myservo.write(val1);
  Serial.println("på");
  Serial.println(rpm);
 }
 
 else if (rpm1 > rpmt) {
 val1 = analogRead(potpin);       
val2 = (val1 - 20) ;
 val2 = map(val2, 0, 1023, 0, 180);
 myservo.write(val2);
 Serial.println("över");
 Serial.println(rpm);
 Serial.println(rpmt);
 }
 else{}       
 // rpm1 = 0;
//  }

 
//delay(250);
     
}






hammy

#1
Feb 25, 2019, 01:11 pm Last Edit: Feb 25, 2019, 01:14 pm by hammy
Most fly by wire systems use two potentiometers and check the output against each other and use various other safety measures .
You need to consider failures causing wide open throttle . So I would not use a simple Arduino to do this.
It would be better to just cut the ignition or fuel when your limit is reached - much safer.

You need to use code tags to post your code.

demkat1

what sensor are you using?

Code: [Select]

else if (millis() - passedtime == 1000) {


this is not correct. suppose difference is 1001ms, what happens?

MarkT

Your volatile counter variable is an int, so cannot be used in the main body of the program safely
without using a critical section around the accesses:

Code: [Select]

// for writing:
  noInterrupts() ;
  counter = 0 ;   // write both bytes atomically.
  interrupts();

// and for reading:

  noInterrupts();
  int my_copy = counter;  // read both bytes atomically
  interrupts();
  // now use my_copy


The Arduino is not designed for safety critical application, nor are hobby servos.

You also need to think of an intrinsically fail-safe way to do this, one that can physically only reduce power,
not max it out if there's a bug.
[ I DO NOT respond to personal messages, I WILL delete them unread, use the forum please ]

Vetsoltech

Hi

I'm aware of the need for a backup safety system or similar. Full throttle in the wrong situation could indeed be fatal...

Anyway, the sensor used is a 55140-3M-03-A.

The main functionality I need is to monitor the speed and make adjustments according to the system.

I tried another code earlier but the delay in the code made the servo "lagg".

I'd like to find a way so that the rpm value gets stored, this way you could use it in the sketch as a condition for different things.

I couldn't manage that with the sketch I got.

It would be greatly appreciated if someone could show me an example where there isn't a long delay and where the rpm value gets stored for usage.

//Anders

Go Up