1 button controlling 4 different modes

Hello everyone

I am new in using arduino and I need some help with my sketch. it wont let me out of the first loop meaning i cant get to the second mode using the button.

[sup]#include "OneButton.h"

int witheLEDPin1=2;       //Declare whiteLEDPin1 an int, and set to pin 12
int witheLEDPin2=3;       //Declare whiteLEDPin2 an int, and set to pin 13 
int witheLEDPin3=4;       //Declare whiteLEDPin1 an int, and set to pin 12

int witheOnTime1=125;     //Declare witheOnTime1 an int.
int witheOffTime1=150;    //Declare witheOffTime1 an int.
 
int witheOnTime2=250;     //Declare witheOnTime2 an int.
int witheOffTime2=150;    //Declare witheOffTime2 an int.

int witheOnTime3=125;      //Declare witheOnTime3 an int.
int witheOffTime3=150;     //Declare witheOffTime3 an int.

int numwithe1Blinks=7500;  //Number of times to blink withe LED1
int numwithe2Blinks=0;     //Number of times to blink withe LED2
int numwithe3Blinks=0;     //Number of times to blink withe LED2
 
OneButton button1(A1, true);  // Setup a new OneButton on pin A1. 

  void setup() {                // setup code here, to run once:                                 
  Serial.begin(9600);           // Setup the Serial port. 
  while (!Serial) {             // wait for serial port to connect.
      }
  Serial.println("Starting 1Buttons...");

  button1.attachClick(click1);  // link the button 1 functions.
  button1.attachDoubleClick(doubleclick1); 
  button1.attachLongPressStart(longPressStart1);
  button1.attachLongPressStop(longPressStop1);
  button1.attachDuringLongPress(longPress1);
  pinMode(witheLEDPin1, OUTPUT);  // Tell Arduino that witheLEDPin1 is an output pin
  pinMode(witheLEDPin2, OUTPUT);  // Tell Arduino that witheLEDPin2 is an output pin
  pinMode(witheLEDPin3, OUTPUT);  // Tell Arduino that witheLEDPin3 is an output pin
  } 

void loop() {              // main code here, to run repeatedly:   
  button1.tick();          // keep watching the push buttons:  
  delay(100);              // Wait
   
} // loop


// ----- button 1 callback functions
void click1() {                     // This function will be called when the button1 was pressed 1 time 
  Serial.println("Button 1 click.");
 digitalWrite(witheLEDPin1,LOW);
  digitalWrite(witheLEDPin2,LOW); 
  digitalWrite(witheLEDPin3,LOW); 
  
for (int j=1; j<=numwithe1Blinks+numwithe2Blinks+numwithe3Blinks; j=j+1) {  // Start our for loop
    digitalWrite(witheLEDPin1,HIGH); //Turn white1 LED on
    delay(witheOnTime1);             //Leave on for whiteOnTime1
    digitalWrite(witheLEDPin1,LOW);  //Turn white1 LED off
    delay(witheOffTime1);            //Leave off for withe

    digitalWrite(witheLEDPin2,HIGH); //Turn white2 LED on
    delay(witheOnTime2);             //Leave on for whiteOnTime2
    digitalWrite(witheLEDPin2,LOW);  //Turn white2 LED off
    delay(witheOffTime2);            //Leave off for withe

    digitalWrite(witheLEDPin3,HIGH); //Turn white3 LED on
    delay(witheOnTime3);             //Leave on for whiteOnTime3
    digitalWrite(witheLEDPin3,LOW);  //Turn white3 LED off
    delay(witheOffTime3);            //Leave off for withe

}

} // click1

// This function will be called when the button1 was pressed 2 times in a short timeframe.
void doubleclick1() {
  Serial.println("Button 1 doubleclick.");
  digitalWrite(witheLEDPin1,LOW);
  digitalWrite(witheLEDPin2,LOW); 
  digitalWrite(witheLEDPin3,LOW); 
  digitalWrite(witheLEDPin1,HIGH);
  delay(500);
  digitalWrite(witheLEDPin1,LOW);
  delay(500);
  
  digitalWrite(witheLEDPin2,HIGH);
  delay(500);
  digitalWrite(witheLEDPin2,LOW);
  delay(500);

  digitalWrite(witheLEDPin3,HIGH);
  delay(500);
  digitalWrite(witheLEDPin3,LOW);
  delay(500);

  digitalWrite(witheLEDPin1,HIGH);
  delay(500);
  digitalWrite(witheLEDPin1,LOW);
  delay(500);
  
  digitalWrite(witheLEDPin2,HIGH);
  delay(500);
  digitalWrite(witheLEDPin2,LOW);
  delay(500);

  digitalWrite(witheLEDPin3,HIGH);
  delay(500);
  digitalWrite(witheLEDPin3,LOW);
  delay(500);
} // doubleclick1

// This function will be called once, when the button1 is pressed for a long time.
void longPressStart1() {
  Serial.println("Button 1 longPress start");
  digitalWrite(witheLEDPin1,HIGH);
  digitalWrite(witheLEDPin2,HIGH);
  digitalWrite(witheLEDPin3,HIGH); 
 
} // longPressStart1

// This function will be called often, while the button1 is pressed for a long time.
void longPress1() {
  Serial.println("Button 1 longPress...");
 } // longPress1

// This function will be called once, when the button1 is released after beeing pressed for a long time.
void longPressStop1() {
  Serial.println("Button 1 longPress stop");
} // longPressStop1

// End
[/sup]

where im i going wrong

any advice would be greatly appreciated.

Thank you

I don’t know about the sketch itself, but please use the code tag button </> for your next post.

CrossRoads:
I don’t know about the sketch itself, but please use the code tag button </> for your next post.

Sorry about that!

Hello everyone

I am new in using arduino and I need some help with my sketch. it wont let me out of the first loop meaning i cant get to the second mode using the button.

where im i going wrong?

#include "OneButton.h"

int witheLEDPin1=2;       //Declare whiteLEDPin1 an int, and set to pin 12
int witheLEDPin2=3;       //Declare whiteLEDPin2 an int, and set to pin 13 
int witheLEDPin3=4;       //Declare whiteLEDPin1 an int, and set to pin 12

int witheOnTime1=125;     //Declare witheOnTime1 an int.
int witheOffTime1=150;    //Declare witheOffTime1 an int.
 
int witheOnTime2=250;     //Declare witheOnTime2 an int.
int witheOffTime2=150;    //Declare witheOffTime2 an int.

int witheOnTime3=125;      //Declare witheOnTime3 an int.
int witheOffTime3=150;     //Declare witheOffTime3 an int.

int numwithe1Blinks=7500;  //Number of times to blink withe LED1
int numwithe2Blinks=0;     //Number of times to blink withe LED2
int numwithe3Blinks=0;     //Number of times to blink withe LED2
 
OneButton button1(A1, true);  // Setup a new OneButton on pin A1. 

  void setup() {                // setup code here, to run once:                                 
  Serial.begin(9600);           // Setup the Serial port. 
  while (!Serial) {             // wait for serial port to connect.
      }
  Serial.println("Starting 1Buttons...");

  button1.attachClick(click1);  // link the button 1 functions.
  button1.attachDoubleClick(doubleclick1); 
  button1.attachLongPressStart(longPressStart1);
  button1.attachLongPressStop(longPressStop1);
  button1.attachDuringLongPress(longPress1);
  pinMode(witheLEDPin1, OUTPUT);  // Tell Arduino that witheLEDPin1 is an output pin
  pinMode(witheLEDPin2, OUTPUT);  // Tell Arduino that witheLEDPin2 is an output pin
  pinMode(witheLEDPin3, OUTPUT);  // Tell Arduino that witheLEDPin3 is an output pin
  } 

void loop() {              // main code here, to run repeatedly:   
  button1.tick();          // keep watching the push buttons:  
  delay(100);              // Wait
   
} // loop


// ----- button 1 callback functions
void click1() {                     // This function will be called when the button1 was pressed 1 time 
  Serial.println("Button 1 click.");
 digitalWrite(witheLEDPin1,LOW);
  digitalWrite(witheLEDPin2,LOW); 
  digitalWrite(witheLEDPin3,LOW); 
  
for (int j=1; j<=numwithe1Blinks+numwithe2Blinks+numwithe3Blinks; j=j+1) {  // Start our for loop
    digitalWrite(witheLEDPin1,HIGH); //Turn white1 LED on
    delay(witheOnTime1);             //Leave on for whiteOnTime1
    digitalWrite(witheLEDPin1,LOW);  //Turn white1 LED off
    delay(witheOffTime1);            //Leave off for withe

    digitalWrite(witheLEDPin2,HIGH); //Turn white2 LED on
    delay(witheOnTime2);             //Leave on for whiteOnTime2
    digitalWrite(witheLEDPin2,LOW);  //Turn white2 LED off
    delay(witheOffTime2);            //Leave off for withe

    digitalWrite(witheLEDPin3,HIGH); //Turn white3 LED on
    delay(witheOnTime3);             //Leave on for whiteOnTime3
    digitalWrite(witheLEDPin3,LOW);  //Turn white3 LED off
    delay(witheOffTime3);            //Leave off for withe

}

} // click1

// This function will be called when the button1 was pressed 2 times in a short timeframe.
void doubleclick1() {
  Serial.println("Button 1 doubleclick.");
  digitalWrite(witheLEDPin1,LOW);
  digitalWrite(witheLEDPin2,LOW); 
  digitalWrite(witheLEDPin3,LOW); 
  digitalWrite(witheLEDPin1,HIGH);
  delay(500);
  digitalWrite(witheLEDPin1,LOW);
  delay(500);
  
  digitalWrite(witheLEDPin2,HIGH);
  delay(500);
  digitalWrite(witheLEDPin2,LOW);
  delay(500);

  digitalWrite(witheLEDPin3,HIGH);
  delay(500);
  digitalWrite(witheLEDPin3,LOW);
  delay(500);

  digitalWrite(witheLEDPin1,HIGH);
  delay(500);
  digitalWrite(witheLEDPin1,LOW);
  delay(500);
  
  digitalWrite(witheLEDPin2,HIGH);
  delay(500);
  digitalWrite(witheLEDPin2,LOW);
  delay(500);

  digitalWrite(witheLEDPin3,HIGH);
  delay(500);
  digitalWrite(witheLEDPin3,LOW);
  delay(500);
} // doubleclick1

// This function will be called once, when the button1 is pressed for a long time.
void longPressStart1() {
  Serial.println("Button 1 longPress start");
  digitalWrite(witheLEDPin1,HIGH);
  digitalWrite(witheLEDPin2,HIGH);
  digitalWrite(witheLEDPin3,HIGH); 
 
} // longPressStart1

// This function will be called often, while the button1 is pressed for a long time.
void longPress1() {
  Serial.println("Button 1 longPress...");
 } // longPress1

// This function will be called once, when the button1 is released after beeing pressed for a long time.
void longPressStop1() {
  Serial.println("Button 1 longPress stop");
} // longPressStop1

// End

any advice would be greatly appreciated.

Thank you

Anyone?

Well I'm only guessing because I've never used the OneButton library but the instructions do say tick() must be called frequently. You aren't calling it anything like frequently.

So if you just mean it spends ages in click1() then what do you expect with all those delays and the number of times that for loop goes round ? Perhaps you don't realise that the delay() call blocks the Arduino and stops it from doing anything else ? And if it's blocked you aren't calling tick() so it isn't looking for new button presses.

Steve