Start coding and in cases of trouble we are able to help.
We’re honestly TRYING to help, but your explanation is lacking, and no, your code doesn’t compile.
if (digitalRead == (2, HIGH));
What are you expecting this to do ?
I know and much appreciated. Thanks.
BUT, I need a code that it will go to the next loop every time I press a button.
for exp.
I have 1 LED and 1 button.
each time I press the button the LED is ON 1 second off and ON again
if I press the button again the LED is ON, 2 second off and ON again
This is simplest explanation of my case.
I composed this a while back --
// pbmillis3Speeds_01
// pb/input is accurate
// works 2016.09.03
//
// if the pb is held down, kept 'active',
// then the speeds get cycled
//
unsigned long markTime;
unsigned long elapsedTime;
unsigned long lockoutmark; // pb-related
const unsigned long debounceTime = 250; // debounceTime
const unsigned long blinkSpeed [3] = {50,200,1000};
byte speedchoice; // blinkSpeed[speedchoice]
boolean lockout; // if true then PB is not checked
byte pbstate;
const byte pbpin = 2;
boolean ledstate;
const byte ledpin = 13;
void setup ()
{
pinMode(pbpin,INPUT_PULLUP);
pinMode(ledpin,OUTPUT);
digitalWrite(ledpin,LOW);
speedchoice = 0;
lockout = false;
markTime = millis();
}
void loop ()
{
elapsedTime = millis();
if(lockout == false) // ok to read
{
pbstate = digitalRead(pbpin);
if (pbstate == 0) // pb made active
{
lockoutmark = millis(); // record millis when pb went LOW
speedchoice++;
lockout = true;
}
if(speedchoice > 2)
{
speedchoice = 0;
}
elapsedTime = millis();
}
elapsedTime = millis();
if ((lockout == true) && ((elapsedTime - lockoutmark) >= debounceTime))
{
lockout = false; // lockout done, threshold met
}
elapsedTime = millis();
if((elapsedTime - markTime) >= blinkSpeed[speedchoice]) //
{
markTime = millis(); // new mark
ledstate = !ledstate;
if (ledstate == true)
{
digitalWrite(ledpin,HIGH);
}
else
{
digitalWrite(ledpin,LOW);
}
}
}
This one has LEDs on 8,9,10,11 and the pushbutton is still on 2.
It's posted on my blog.
/*
1 pushbutton and 4 LEDs
LED blink patterns selected with pushbutton actuation
*/
//
// pbmillisRoutines_01
//
// Working
// 2016.09.05
//
// pattern remains active if held down
//
unsigned long markTime;
unsigned long elapsedTime;
unsigned long lockoutmark; // pb-related
const unsigned long debounceTime = 500; // debounceTime
byte pinsArray [4] = {8,9,10,11};
const byte frame [] = {B00000011, B00001100,
B00000101, B00001010,
B00000110, B00001001,
B00000000, B11111111,
B00000001, B00000010, B00000100, B00001000,
B00000000, B00000001, B00000011, B00000111,
B00001111, B00001110, B00001100, B00001000};
const byte routinePlan [6][2] = {0,1, // first, last
2,3, // first, last
4,5, // first, last
6,7, // first, last
8,11, // first, last
12,19};
byte first;
byte last;
byte routineIndex = 0;
byte iteration = 0;
const unsigned long blinkSpeed = 500;
byte pbstate;
boolean lockout; // if true then PB doesn't get checked
boolean tripped; // pb hit, prep Update for new pointers etc.
const byte pbpin = 2;
byte index; // general application, utility
void setup ()
{
//Serial.begin(9600);
pinMode(pbpin,INPUT_PULLUP);
for(index=0; index<4; index ++)
{
pinMode(pinsArray[index],OUTPUT);
digitalWrite(pinsArray[index],LOW);
}
routineIndex = 0;
lockout = false;
markTime = millis();
tripped = true;
Update();
}
void loop ()
{
elapsedTime = millis();
if(lockout == false) // ok to read
{
pbstate = digitalRead(pbpin);
if (pbstate == 0) // pb made active
{
lockoutmark = millis(); // record millis when pb went LOW
routineIndex++;
lockout = true;
tripped = true;
if(routineIndex > 5) // rollover
{
routineIndex = 0;
}
}
}
if(tripped == true)
{
Update(); // brings a 'tripped' with it
}
elapsedTime = millis();
if ((lockout == true) && ((elapsedTime - lockoutmark) >= debounceTime))
{
lockout = false; // lockout done, threshold met
}
elapsedTime = millis();
if((elapsedTime - markTime) >= blinkSpeed) // determine whether Update() is due
{
markTime = millis(); // new mark
Update();
}
}
void Update ()
{
if(tripped == true) // if tripped == false then we got here because IT'S TIME
{
// new line, new frame
first = routinePlan[routineIndex][0];
last = routinePlan[routineIndex][1];
tripped = false;
iteration = 0;
}
if ((iteration + first) > last)
{
iteration = 0;
}
byte bitmask = 1;
for(byte progress = 0; progress < 4; progress++)
{
if((frame[iteration + first] & bitmask) == 0)
{
digitalWrite(pinsArray[progress],LOW);
}
else
{
digitalWrite(pinsArray[progress],HIGH);
}
bitmask = bitmask * 2;
}
iteration++;
elapsedTime = millis();
}
Another outstanding question…
What if you push the button in the middle of a sequence ?
Should it stop, or wait until the current sequence has finished - then begin the next sequence ?
apparently the "language is a little heavy" and that may be beyond his grasp