I am trying to update my current code so that it functions better and work how I want it to. However I cant get my IF ELSE statements to work. What I'm looking for is, when button number 3 or 4 is held down I want the led to turn off and stay off until the button is released. However currently if I leave it as just an if statement the led shuts off and wont turn back on even when it released which makes sense to me. But if I add the else statement I cant get the led to turn off at all. The first two buttons work perfectly every time.
int j = 1; // integer used in scanning the array designating column number
//2-dimensional array for asigning the buttons and there high and low values
int Button[21][3] = {{1, 507, 511}, // button 1
{2, 316, 320}, // button 2
{3, 233, 237}, // button 3
{4, 6, 10}}; // button 4
int analogpin = A0; // analog pin to read the buttons
int label = 0; // for reporting the button label
int counter = 0; // how many times we have seen new value
long time = 0; // the last time the output pin was sampled
int debounce_count = 50; // number of millis/samples to consider before declaring a debounced input
int current_state = 0; // the debounced input value
int ButtonVal;
const int CLAM = 2;
const int VODKA = 4;
const int WOR = 3;
void setup()
{
Serial.begin(9600);
pinMode (CLAM, OUTPUT);
digitalWrite (CLAM, HIGH);
pinMode (VODKA, OUTPUT);
digitalWrite (VODKA, HIGH);
pinMode (WOR, OUTPUT);
digitalWrite (WOR, HIGH);
}
void loop()
{
// If we have gone on to the next millisecond
if (millis() != time)
{
// check analog pin for the button value and save it to ButtonVal
ButtonVal = analogRead(analogpin);
if(ButtonVal == current_state && counter >0)
{
counter--;
}
if(ButtonVal != current_state)
{
counter++;
}
// If ButtonVal has shown the same value for long enough let's switch it
if (counter >= debounce_count)
{
counter = 0;
current_state = ButtonVal;
//Checks which button or button combo has been pressed
if (ButtonVal > 0)
{
ButtonCheck();
}
}
time = millis();
}
}
void ButtonCheck()
{
// loop for scanning the button array.
for(int i = 0; i <= 21; i++)
{
// checks the ButtonVal against the high and low vales in the array
if(ButtonVal >= Button[i][j] && ButtonVal <= Button[i][j+1])
{
// stores the button number to a variable
label = Button[i][0];
Action();
}
}
}
void Action()
{
if(label == 1)
{
Serial.println("CLAM ON");
digitalWrite(CLAM, LOW);
Serial.println("VODKA ON");
digitalWrite(VODKA, LOW);
Serial.println("WOR ON");
digitalWrite(WOR, LOW);
delay(500); //starting value 2195
Serial.println("WOR OFF");
digitalWrite(WOR, HIGH);
delay(500); //starting value 3700
Serial.println("VODKA OFF");
digitalWrite(VODKA, HIGH);
delay(500); //starting value 28955
Serial.println("CLAM OFF");
digitalWrite(CLAM, HIGH);
Serial.println();
}
if(label == 2)
{
Serial.println("VODKA ON");
digitalWrite(VODKA, LOW);
delay(500);
digitalWrite(VODKA, HIGH);
Serial.println();
}
if(label == 3)
{
Serial.println("WOR ON");
digitalWrite(WOR, LOW);
}
else
{
digitalWrite(WOR, HIGH);
Serial.println();
}
if(label == 4)
{
Serial.println("CLAM ON");
digitalWrite(CLAM, LOW);
}
else
{
digitalWrite(CLAM, HIGH);
Serial.println();
}
}
Yes, I pieced this code together from a couple sources as well as added some of my own code. I have tried to change that for for whatever reason the code seems to stop working.
but I only want that to run for the specific label (3 and 4). Its basically the same functional ability as the stock button example. If I push and hold down button 3 (label 3) I want the clam led to turn off. Same goes for the wor led. But I only want it to do it for those two buttons and those two leds.
Here is another way of showing what I am after. But I dont want to have to use the delay funtion for the last two labels. I want it to be based off of the button being held down or not being held down. Basiclly as if you were to walk up to a soda machine and hold your cup on the trigger, as long as your pushing it, its dispensing. But as soon as you take pressure off of the trigger the flow stops. Its not based on time.
int j = 1; // integer used in scanning the array designating column number
//2-dimensional array for asigning the buttons and there high and low values
int Button[4][3] = {{1, 507, 511}, // button 1
{2, 316, 320}, // button 2
{3, 233, 237}, // button 3
{4, 6, 10}}; // button 4
int analogpin = A0; // analog pin to read the buttons
int label = 0; // for reporting the button label
int counter = 0; // how many times we have seen new value
long time = 0; // the last time the output pin was sampled
int debounce_count = 50; // number of millis/samples to consider before declaring a debounced input
int current_state = 0; // the debounced input value
int ButtonVal;
const int CLAM = 2;
const int VODKA = 4;
const int WOR = 3;
void setup()
{
Serial.begin(9600);
pinMode (CLAM, OUTPUT);
digitalWrite (CLAM, HIGH);
pinMode (VODKA, OUTPUT);
digitalWrite (VODKA, HIGH);
pinMode (WOR, OUTPUT);
digitalWrite (WOR, HIGH);
}
void loop()
{
// If we have gone on to the next millisecond
if (millis() != time)
{
// check analog pin for the button value and save it to ButtonVal
ButtonVal = analogRead(analogpin);
if(ButtonVal == current_state && counter >0)
{
counter--;
}
if(ButtonVal != current_state)
{
counter++;
}
// If ButtonVal has shown the same value for long enough let's switch it
if (counter >= debounce_count)
{
counter = 0;
current_state = ButtonVal;
//Checks which button or button combo has been pressed
if (ButtonVal > 0)
{
ButtonCheck();
}
}
time = millis();
}
}
void ButtonCheck()
{
// loop for scanning the button array.
for(int i = 0; i <= 21; i++)
{
// checks the ButtonVal against the high and low vales in the array
if(ButtonVal >= Button[i][j] && ButtonVal <= Button[i][j+1])
{
// stores the button number to a variable
label = Button[i][0];
Action();
}
}
}
void Action()
{
if(label == 1)
{
Serial.println("CLAM ON");
digitalWrite(CLAM, LOW);
Serial.println("VODKA ON");
digitalWrite(VODKA, LOW);
Serial.println("WOR ON");
digitalWrite(WOR, LOW);
delay(500); //starting value 2195
Serial.println("WOR OFF");
digitalWrite(WOR, HIGH);
delay(500); //starting value 3700
Serial.println("VODKA OFF");
digitalWrite(VODKA, HIGH);
delay(500); //starting value 28955
Serial.println("CLAM OFF");
digitalWrite(CLAM, HIGH);
Serial.println();
}
if(label == 2)
{
Serial.println("VODKA ON");
digitalWrite(VODKA, LOW);
delay(500);
digitalWrite(VODKA, HIGH);
Serial.println();
}
if(label == 3)
{
Serial.println("WOR ON");
digitalWrite(WOR, LOW);
delay(500);
Serial.println();
}
if(label == 4)
{
Serial.println("CLAM ON");
digitalWrite(CLAM, LOW);
delay(500);
Serial.println();
}
}