Checking a Value is above a certain level- before running rest of Program

Hi
Im really trying to check a value on HallSensor is Equal to or less than 200. (foot off peddle ).

if it is then continue,

~But if its higher then display the Message "Check Throttle !" but dont run the Void Loop stuff

but i dont know where to put it in my script. It needs to run just once once as the controller starts up after the first LCD Messages.

have i got this is the right place guys?. will it work here ?

Many thanks im new to arduino language :slight_smile:

James

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11, 10, 5, 4, 3, 2);


int motor = 12;
int HallSensor = 0;

void setup(){
  pinMode(motor,OUTPUT);
  pinMode(HallSensor,INPUT);
  
    // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print Welcome message to the LCD.
  lcd.print("--Lipo Kart 17--");
  delay(3000);
  lcd.clear();
  delay(1000);
  lcd.print("Are you Ready to");
  lcd.setCursor(0,1);
  lcd.print("     Ride ?");
  delay(3000);
  lcd.clear();
  delay(1000);
  
//  int startvalue = analogRead(HallSensor);

while (analogRead(HallSensor) >= 200) {
  
  lcd.print("Check Throttle !");
}


  Serial.begin(9600);
}



void loop(){
  int value = analogRead(HallSensor);
  
  value = map(value,200,819,0,255);
  value = constrain(value,0,255);
                     //read input value: range between (0,1023)
  //int motor_speed = value/4;
                    //PWM can only ouput 255 different values
                    
  analogWrite(motor,value);
 
  Serial.println(value);//for testing purposes
  int percentagevalue = map(value,0,255,0,100);
  
  lcd.print("Throttle Peddle");
    // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  
  lcd.print(percentagevalue);
  lcd.print(" %");
  delay(100);
  lcd.clear();
}

have i got this is the right place guys?. will it work here ?

Looks right to me. However it will keep on printing the same message until it fills up the LCD and then it will wrap round perhaps displaced some what.
Why not do this:-

if (analogRead(HallSensor) >= 200)   lcd.print("Check Throttle !");
while (analogRead(HallSensor) >= 200) {  }

Thanks Mike thats a nice simple way of doing it, i was wondering if it was in the right place there. `Ill give a whirl.

im making a little go kart project for my son you see :slight_smile:

The next bit is to get a soft start on that throttle so that when he hits the gas fast - it ramps up that pwm just a little - sort of a delay :- i found this ?? what do you reckon? - I got confused :roll_eyes: with the switch states in this code- i dont have switches you see. - THank you for your help - sometimes i just get stuck!.....P>S Its raining here in sheffield !

boolean pressed=false;
boolean onstate=false;
int softstart=25;
int speedset=0;
boolean speedreach=false;


void setup(){
}




void loop(){
  
  
  
  if (digitalRead(2)==1 && onstate==false && pressed == false){
pressed=true;onstate=true;}




if (onstate==true){
  speedset=analogRead(A0);
  speedset=map(speedset,0,1023,20,255);
  softstart++; 

if(softstart>60){softstart=speedset;speedreach=true;}
  
  
if (speedreach==false){analogWrite (3,softstart);analogWrite (5,softstart);}
else {analogWrite (3,speedset);analogWrite (5,speedset);}

}





if (digitalRead(2)==1 && onstate==true && pressed == false){
pressed=true;onstate=false;digitalWrite (3,0);digitalWrite (5,0);softstart=25; speedreach=false;}
 
  if (digitalRead(2)==0){pressed=false;}
  
  
  
  delay(100);
}
if(softstart>60){softstart=speedset;speedreach=true;}
  
  
if (speedreach==false){analogWrite (3,softstart);analogWrite (5,softstart);}
else {analogWrite (3,speedset);analogWrite (5,speedset);}

Time to start developing a good coding style.

Hi

Yes i could tidy that lot up a bit your right.

So..........i guess that snippet you just posted back from my code is the bit that does the soft start ?

So if i wanted to implement this into my code , do you (or can you show me pretty please XD XD how to do this bit). and i promise to tidy up the "Style ".

My code is now:-

//Go Kart Little Speed Controller

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11, 10, 5, 4, 3, 2);


int motor = 12;
int HallSensor = 0;

void setup(){
  pinMode(motor,OUTPUT);
  pinMode(HallSensor,INPUT);
  
    // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print Welcome message to the LCD.
  lcd.print("--Lipo Kart 17--");
  delay(3000);
  lcd.clear();
  delay(1000);
  lcd.print("Are you Ready to");
  lcd.setCursor(0,1);
  lcd.print("     Ride ?");
  delay(3000);
  lcd.clear();
  delay(1000);
  

if (analogRead(HallSensor) >= 200)   lcd.print("Check Throttle !");
while (analogRead(HallSensor) >= 200) {  }


  Serial.begin(9600);
}



void loop(){
  int value = analogRead(HallSensor);
  
  value = map(value,200,819,0,255);
  value = constrain(value,0,255);
                    
  analogWrite(motor,value);
 
  Serial.println(value);//for testing purposes
  int percentagevalue = map(value,0,255,0,100);
  
  lcd.print("Throttle Peddle");
    // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  
  lcd.print(percentagevalue);
  lcd.print(" %");
  delay(100);
  lcd.clear();
}

avaproductions:
So..........i guess that snippet you just posted back from my code is the bit that does the soft start ?

No, I wouldn't say that. It was just hard to read, no offence.

void loop(){
  int value = analogRead(HallSensor);

  value = map(value,200,819,0,255);
  value = constrain(value,0,255);

I don't get this bit. Is more than 200 throttle down or throttle up?

Assuming it is "normal" (ie. the higher the more motor) how about ramping it up like this:

  if (value > throttleValue)
    {
    throttleValue += 5;  // add a bit to the throttle
    if (throttleValue > value)
      throttleValue = value;   // don't overshoot
    analogWrite(motor, throttleValue);  // set motor to new value
    }
  else
    { 
    throttleValue = value;   // slow down instantly
    analogWrite(motor, throttleValue);  // set motor to new value
    }

Hi there

The sensor has a min voltage (0.8v) of 200 - so that's throttle off. So more than 200 is increasing in motor speed.

Max throttle is around 800. Which is 255 100% duty.

That's looks very interesting code. I'll try it placed into the loop.

I'm trying to fathom out how it works, so code this would make the motor go from 0-x% (0-30%) if the peddle was quickly pushed say ...slowly adding 5 each time it went around until it reached the position (30%) ? Time of ramp up could be controlled by an delay in that loop of maybe 100ms ?

Would this effect still happen if we are at 30% and we quick push now up to 75% throttle -I.e it's effect is applied regardless of throttle position.

This would be advantage as the esc would not get hard heavy current spikes that way. - add protection.

Will that code be correct for your 200 question?
Thankyou
James

Yes I think you have it. You could change the +5 to be some other number to make it more responsive. (eg. +15 or +50).

Or if you want to get fancy use a PID algorithm (look up "arduino pid").

I'll try it placed into the void loop.

Sorry to sound pedantic but can you call it "the loop function" because that is what it is. The word void indicates it returns nothing. Calling it the void loop annoys the hell out of most people here.
I know you don't know, so that is why I am telling you, I am not telling you off. :slight_smile:

Hi there, its coming together but i think i have missed a little something ? im lost again - i think its to do with not know how to declare that new var "throttlevalue" or more of what things i need to put into the int throttlevalue = ();

GoKart_sketch_V2.ino: In function 'void loop()':
GoKart_sketch_V2:50: error: 'throttleValue' was not declared in this scope
GoKart_sketch_V2:63: error: 'throttleValue' was not declared in this scope

//Go Kart Little Speed Controller

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11, 10, 5, 4, 3, 2);
int motor = 12;
int HallSensor = 0;

void setup(){
  pinMode(motor,OUTPUT);
  pinMode(HallSensor,INPUT);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print Welcome message to the LCD.
  lcd.print("--Lipo Kart 17--");
  delay(3000);
  lcd.clear();
  delay(1000);
  lcd.print("Are you Ready to");
  lcd.setCursor(0,1);
  lcd.print("     Ride ?");
  delay(3000);
  lcd.clear();
  delay(1000);
  
if (analogRead(HallSensor) >= 200)   lcd.print("Check Throttle !");
while (analogRead(HallSensor) >= 200) {  }

  Serial.begin(9600);
}

void loop(){
  int value = analogRead(HallSensor);
  
  value = map(value,200,819,0,255);
  value = constrain(value,0,255);
                    
  //analogWrite(motor,value);
  
 //SoftStart Function
   if (value > throttleValue)
    {
    throttleValue += 5;  // add a bit to the throttle
    if (throttleValue > value)
      throttleValue = value;   // don't overshoot
    analogWrite(motor, throttleValue);  // set motor to new value
    }
  else
    { 
    throttleValue = value;   // slow down instantly
    analogWrite(motor, throttleValue);  // set motor to new value
    }
 
  Serial.println(throttleValue);//for testing purposes
  
  //To Display the Throttle Value as Percent
  int percentagevalue = map(throttleValue,0,255,0,100);
  
  lcd.print("Throttle Peddle");
    // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  lcd.print(percentagevalue);
  lcd.print(" %");
  
  //Clear Display and delay and loop over again
  delay(100);
  lcd.clear();
}

is this correct ? i just declared it as int throttleValue;

//Go Kart Little Speed Controller

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11, 10, 5, 4, 3, 2);
int motor = 12;
int HallSensor = 0;

void setup(){
  pinMode(motor,OUTPUT);
  pinMode(HallSensor,INPUT);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print Welcome message to the LCD.
  lcd.print("--Lipo Kart 17--");
  delay(3000);
  lcd.clear();
  delay(1000);
  lcd.print("Are you Ready to");
  lcd.setCursor(0,1);
  lcd.print("     Ride ?");
  delay(3000);
  lcd.clear();
  delay(1000);
  
if (analogRead(HallSensor) >= 200)   lcd.print("Check Throttle !");
while (analogRead(HallSensor) >= 200) {  }

  Serial.begin(9600);
}

void loop(){
  int value = analogRead(HallSensor);
  int throttleValue;
  value = map(value,200,819,0,255);
  value = constrain(value,0,255);
                    
  //analogWrite(motor,value);
  
 //SoftStart Function
   if (value > throttleValue)
    {
    throttleValue += 5;  // add a bit to the throttle
    if (throttleValue > value)
      throttleValue = value;   // don't overshoot
    analogWrite(motor, throttleValue);  // set motor to new value
    }
  else
    { 
    throttleValue = value;   // slow down instantly
    analogWrite(motor, throttleValue);  // set motor to new value
    }
 
  Serial.println(throttleValue);//for testing purposes
  
  //To Display the Throttle Value as Percent
  int percentagevalue = map(throttleValue,0,255,0,100);
  
  lcd.print("Throttle Peddle");
    // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  lcd.print(percentagevalue);
  lcd.print(" %");
  
  //Clear Display and delay and loop over again
  delay(100);
  lcd.clear();
}

No, don't just thrash around making compiler errors disappear.

The variable should be global, so it persists for each iteration of loop. So instead of:

void loop(){
  int value = analogRead(HallSensor);
  int throttleValue;

You want:

int throttleValue;
void loop(){
  int value = analogRead(HallSensor);

It might seem subtle, but there's a big difference. Moving it outside loop makes it persist, so you can gradually add something to it.

ahh got it now Nick. it can exist and be changed without it constantly being declared :slight_smile:

So when we declare the var first off - its null right ?

so fresh start we have null in throttleValue.

so:-

if (value > throttleValue)
    {

value is say 0 and throttle value is null - then do the else statement - make throttleValue =to value.

as value increases just by even 1 then its greater than null and off we go. :smiley:

Ive reduced the += to 1 because im working on the 0-255 range

  value = map(value,200,819,0,255);
  value = constrain(value,0,255);

and 10 is actually quite fast in motor speed with gearing etc.
It compiles fine and heres the final code before i hook it up to 400A speed control and two 2HP motors :smiley:
of course ill put the Oscilloscope on pin 12 first though. :astonished: I will report back Sir

//Go Kart Little Speed Controller

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11, 10, 5, 4, 3, 2);
int motor = 12;
int HallSensor = 0;

void setup(){
  pinMode(motor,OUTPUT);
  pinMode(HallSensor,INPUT);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print Welcome message to the LCD.
  lcd.print("--Lipo Kart 17--");
  delay(3000);
  lcd.clear();
  delay(1000);
  lcd.print("Are you Ready to");
  lcd.setCursor(0,1);
  lcd.print("     Ride ?");
  delay(3000);
  lcd.clear();
  delay(1000);
  
if (analogRead(HallSensor) >= 200)   lcd.print("Check Throttle !");
while (analogRead(HallSensor) >= 200) {  }

  Serial.begin(9600);
}

int throttleValue;
void loop(){
  int value = analogRead(HallSensor);
  value = map(value,200,819,0,255);
  value = constrain(value,0,255);
                    
  //analogWrite(motor,value);
  
 //SoftStart Function
   if (value > throttleValue)
    {
    throttleValue += 1;  // add a bit to the throttle
    if (throttleValue > value)
      throttleValue = value;   // don't overshoot
    analogWrite(motor, throttleValue);  // set motor to new value
    }
  else
    { 
    throttleValue = value;   // slow down instantly
    analogWrite(motor, throttleValue);  // set motor to new value
    }
 
  Serial.println(throttleValue);//for testing purposes
  
  //To Display the Throttle Value as Percent
  int percentagevalue = map(throttleValue,0,255,0,100);
  
  lcd.print("Throttle Peddle");
    // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  lcd.print(percentagevalue);
  lcd.print(" %");
  
  //Clear Display and delay and loop over again
  delay(100);
  lcd.clear();
}

So when we declare the var first off - its null right ?

No, it's an int, and since it's global, it will be initialized. Ints initialize to zero.

Of course, generally, null just happens to be zero so there is little practical difference in arduino terms, but saying that an integer initializes to null seems.... strange.

All code works perfectly have tested it today and it's been great fun everything works exactly as it should.

In the end after many driving tests we settled on a value of 13 this gave a good balance of smooth start versus acceleration.

I must say it's incredibly sophisticated and it's a superb way of controlling a go-kart motor. Compared to simple analogue type discrete circuits. The flexibility for you to be able to get it exactly the way you want is quite astonishing I think I may take this project little further thank you for all your help and support that is to everybody that is on this forum esp Nick. XD

James

I'm glad it's working. I thought the value might need tweaking, but this is certainly the advantage of using microprocessors here. You could make it start slowly, go up in larger amounts, and then go back down again (if you wanted to).

int HallSensor = 0

Wait a second, are you sure you're using the right pin for the Hall sensor? Pin 0 should be a digital pin, not an analog one. Even if you say it's working, that doesn't seem right.

  int value = analogRead(HallSensor);

The way the libraries are written it assumes A0 if you pass 0 to analogRead.

By "libraries" are you talking about the Arduino core? Because the only other library in that code in the LCD library.

Yes, the file with analogRead in it.