Deva_Rishi:
ah, you forgot to addpinMode(BUTTON_PIN, INPUT__PULLUP);
Thanks sir,Now its working perfectly.I've got what i need.
Deva_Rishi:
ah, you forgot to addpinMode(BUTTON_PIN, INPUT__PULLUP);
Thanks sir,Now its working perfectly.I've got what i need.
sreekanthmp:
Thanks sir,Now its working perfectly.I've got what i need.
I'm glad that you have your project working, but for future reference, you really need to learn to do this timing stuff a better way; dare I say a "right" way.
That code you were given in your other thread, and which started all your problems, is a very strange way to do timings, and you're lucky that Deva_Rishi came to your rescue with a fix. In my opinion, what you have there is a kluge. It's going to bite your arse if you carry on that way.
Below fwiw is my previous sketch with the button added, using millis() the "right" way:
// https://forum.arduino.cc/index.php?topic=630113
// 7 august 2019
// this mark3 using millis() properly
// and a button to cancel current state and jump to next
// this one uses an array instead of switch...case
byte ledPins[] = {11, 12, 13}; //index from 0 to 2 for 3 leds
byte howManyLEDs = 3;
int interval = 2000;
unsigned long previousMillisLed;
byte ledNumber = -1;
byte buttonPin = 5;
bool buttonState;
bool lastButtonState;
void setup()
{
Serial.begin(9600);
Serial.println(".... 630133 mk3 proper bwod with a button ....");
Serial.println(".... the button press uses state change detect ....");
Serial.print("Compiler: ");
Serial.print(__VERSION__);
Serial.print(", Arduino IDE: ");
Serial.println(ARDUINO);
Serial.print("Created: ");
Serial.print(__TIME__);
Serial.print(", ");
Serial.println(__DATE__);
Serial.println(__FILE__);
for (int i = 0; i < howManyLEDs; i++)
{
pinMode(ledPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP);
//initialize button states
buttonState = digitalRead(buttonPin);
lastButtonState = buttonState;
Serial.println("setup() complete");
} //setup
// the loop routine runs over and over again forever:
void loop()
{
doMillisStuff();
readTheButton();
lightTheLeds();
}//loop
void doMillisStuff()
{
if (millis() - previousMillisLed >= interval)
{
previousMillisLed = millis();
ledNumber++;
if (ledNumber == 3) ledNumber = 0;
Serial.println(ledNumber);
}
}//millis stuff
void readTheButton()
// this is from the state change detect example
{
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) // means it changed... but which way?
{
if (buttonState == LOW) // changed to pressed
{
// if the current state is LOW then the button was pressed
ledNumber++;
if (ledNumber == 3) ledNumber = 0;
Serial.print("Jumping to next led... ");
Serial.println(ledNumber);
}
// poor man's de-bounce
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}//button
void lightTheLeds()
{
//turn them all off
for (int i = 0; i < howManyLEDs; i++)
{
digitalWrite(ledPins[i], LOW);
}
//turn on the right one
digitalWrite(ledPins[ledNumber], HIGH);
}//lightTheLeds