Hi All,
Long story short ,I decided to try and control a toy car with buttons on board the case. ( code is below )
Basically what I am trying to do is:
- Have 2 buttons as incremental and decremental states (Standby,Lo,Med,High)
- Have 1 Button to switch between Forward and Reverse.
- Have 1 Button to tun ON/OFF blinking lights.
Currently I have managed to get it 90% working by hacking up an inc/dec sketch found here on the forums along with a standard button state sketch, and it works except I have not yet figured out how to include the blink part.
It's a learn and progress sketch so excuse the mess.
The direction I would like help on:
- How to clean up the mess
- Include the Blink part.
// Speed Control
const int LO = 8; // LED Standby
const int MED = 9; // LED Low
const int HI = 10; // LED Medium
const int JET = 11; // LED High
const int D_BUTTON = 2; // DOWN BUTTON
const int U_BUTTON = 3; // UP BUTTON
const int OUT_1 = 15; // Motor !!! 15 to 19 Analog 1 to 5
// FW/RVS Light Control
#define OUT 2
const int FRL_Button[OUT]={4,5};
const int FRL_Output[OUT]={16,17};
int OutState[OUT]={LOW,LOW};
int buttonState[OUT];
int lastButtonState[OUT]={LOW,LOW};
//Variables
int lastCounter = 3;
int counter; //the basic counter
int UP; // Read Up
int DWN; // Read Down
int A;
int B;
int buttonPushCounter = 0;
void setup(){
for (int i=0;i<OUT;i++) {
pinMode(FRL_Button[i], INPUT);
pinMode(FRL_Output[i], OUTPUT);
digitalWrite(FRL_Button[i], HIGH);
pinMode(UP,INPUT);
pinMode(DWN,INPUT);
pinMode(LO,OUTPUT);
pinMode(MED,OUTPUT);
pinMode(HI,OUTPUT);
pinMode(JET,OUTPUT);
pinMode(OUT_1,OUTPUT);
}
}
void loop(){
for (int i=0; i<OUT; i++) {
int reading = digitalRead(FRL_Button[i]);
if (reading != lastButtonState[i])
if (buttonState[i] == LOW) {
OutState[i] = !OutState[i];
}
digitalWrite(FRL_Output[i], OutState[i]);
UP = digitalRead(U_BUTTON); //button pins reading
DWN = digitalRead(D_BUTTON);
if (UP == HIGH) { //if the up button is pressed
counter = counter++; //counter increases by one
}
if (DWN == HIGH) { //if down button is pressed
counter = counter--; //counter decreases by one
}
if (counter > 4){ //check that counter won't get further than 4
counter = 4;
}
if (counter < 2){ //check that counter won't fall bellow 1
counter = 1;
}
switch (counter){ //depending on counter value the proper STATE is sent to the leds
case 1:
digitalWrite(LO,HIGH);
digitalWrite(MED,LOW);
digitalWrite(HI,LOW);
digitalWrite(JET,LOW);
analogWrite(OUT_1,50); // THIS IS JUST A DUMMY VALUE
break;
case 2:
digitalWrite(LO,LOW);
digitalWrite(MED,HIGH);
digitalWrite(HI,LOW);
digitalWrite(JET,LOW);
analogWrite(OUT_1,100); // THIS IS JUST A DUMMY VALUE
break;
case 3:
digitalWrite(LO,LOW);
digitalWrite(MED,LOW);
digitalWrite(HI,HIGH);
digitalWrite(JET,LOW);
analogWrite(OUT_1,150); // THIS IS JUST A DUMMY VALUE
break;
case 4:
digitalWrite(LO,LOW);
digitalWrite(MED,LOW);
digitalWrite(HI,LOW);
digitalWrite(JET,HIGH);
analogWrite(OUT_1,250); // THIS IS JUST A DUMMY VALUE
break;
}
if ( (UP == HIGH) || (DWN == HIGH) ) { //if down button is pressed
lastButtonState[i] = reading;
}
delay(200); // Seems to be a good enough delay between button states
}
}
Any and all help is appreciated
LED_Button_Play.ino (3.07 KB)