Ive been working on a project that would allow me to adjust the speed of a motor using an ESC and a lcd keypad shield. I'm perplexed as to why my speed variable will not go up or down in increments of 10. It jumps wildly around.
Everything up until this point has been paste and upload, so I really want to be successful with this.
I have no coding experience other than reading examples and others sketches.
In fact this was my first attempt and used two source codes and tried to combine them.
a simple equation of sm1 = (sm1 -10) doesn't seem to do anything different than sm1-=10
Edit: I now understand that this is the same equation in coding. Thank you septillion
#include <Servo.h>
#include <LiquidCrystal.h>
Servo m1;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //These are the pins used on this shield
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int lcd_key = 0;
int adc_key_in = 0;
int adc_key_prev = 0;
int sm1 = 90; //SPEED OF MOTOR 1
void setup ()
{
//LCD START
lcd.begin(16,2); // Start LCD
lcd.setCursor(0,0);
lcd.print ("Speed = ");
lcd.setCursor(9,0);
lcd.print (sm1);
//ESC SETUP
m1.attach(11); //attach to pin 11 (digital)
delay(100);
m1.write(10); //writing the minimum value (Arming it)
delay(1500); //delay to let it process that
m1.write(180); //Writes max value
delay(1500);
m1.write(90); //writes neutral
delay(1500);
m1.write(10); //writing the minimum value
delay(1500);
m1.write(90); //motor speed max is 180 min is 90 best 96><170. 90 is Neutral.
}
void loop()
{
m1.write(sm1);
adc_key_prev = lcd_key ; // Looking for changes
lcd_key = read_LCD_buttons(); // read the buttons
if (adc_key_prev != lcd_key)
{
lcd.setCursor(9,0);
lcd.print(" "); // Blank, display returned Motor Speed 10-180
lcd.setCursor(9,0);
lcd.print (sm1);
}
lcd.setCursor(5,1); // move to the beginning of the second line
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
lcd.print("NA ");
break;
}
case btnLEFT:
{
lcd.print("NA ");
break;
}
case btnUP:
{
lcd.print("UP +10 ");
sm1+=10; // WHERE I ADJUST SPEED UP
break;
}
case btnDOWN:
{
lcd.print("DOWN -10 ");
sm1-=10; // WHERE I ADJUST SPEED DOWN
break;
}
case btnSELECT:
{
lcd.print("NEUTRAL "); // STOP MOTOR
sm1= 90;
break;
}
case btnNONE:
{
lcd.print("WAITING ");
break;
}
}/* --(end switch )-- */
}
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
delay(5); //switch debounce delay. Increase this delay if incorrect switch selections are returned.
int k = (analogRead(0) - adc_key_in); //gives the button a slight range to allow for a little contact resistance noise
if (5 < abs(k)) return btnNONE; // double checks the keypress. If the two readings are not equal +/-k value after debounce delay, it tries again.
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this
}