Problem with "while" statement

Hi all, I have an issue with my "while" statement on line 146.

The idea is that I have a keypad, when I push a button, I can enter a code, and when the code is right, I have some gear motor turning. While these are turning, I have 12 LEDs red turning to show that the motor is still not finished. When motors are finished, the red lights would turn off (not yet in the code), and some green one would light on.

Problem is that the red turning light (working fine if alone), part of ledloop() stop as soon as the motor is starting Closepiston().
I don't get how to write it so that they keep on turning until the motor are finished.
I have added one var1, and though that, i could change the variable at the end of the Closepiston() going back to the loop and with the changed value, the ledloop() would then stop. But still, same, the ledloop() actually stop as soon as Closepiston() starts ...
Here my code:

int var1=0;

// include the library keypad & LiquidCrystal:
#include <LiquidCrystal.h>
#include <Keypad.h>

//Define constantn for the keypad
const int ROW_NUM = 4;
const int COLUMN_NUM = 3;
//Matrix for the Keypad
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
//DEFINE pins for the keypad
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
//DEFINE code for the keypad
const String password = "1234"; // password
String input_password;

//Define matrix for colors
const int LED_1 = 21;     //LED row 1
const int LED_2 = 19;     //LED row 2    
const int LED_3 = 20;     //LED row 3

//Declare PIN for the green LEDS
const int LED_4 = 29;

// Declare pins for the LCD
const int rs = 30, en = 31, d4 = 35, d5 = 34, d6 = 33, d7 = 32;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#define LED 41

// PushbuttonOpen
const int buttonPinOpen = 40;
// PushbuttonClose
const int buttonPinClose = 41;
// variables will change:
int buttonStateOpen = 0;
int buttonStateClose = 0;

// Motor A connections
int enA = 12;
int in1 = 42;
int in2 = 43;
// Motor B connections
int enB = 13;
int in3 = 44;
int in4 = 45;
// Motor C connections
int enC = 10;
int in5 = 46;
int in6 = 47;
// Motor D connections
int enD = 11;
int in7 = 48;
int in8 = 49;
// Motor E connections
int enE = 1;
int in9 = 50;
int in10 = 51;
// Motor F connections
int enF = 2;
int in11 = 52;
int in12 = 53;

void setup(){
  Serial.begin(9600);
  input_password.reserve(32); // maximum input characters is 33, change if needed
  pinMode(LED, OUTPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Welcome");
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(enC, OUTPUT);
  pinMode(enD, OUTPUT);
  pinMode(enE, OUTPUT);
  pinMode(enF, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(in5, OUTPUT);
  pinMode(in6, OUTPUT);
  pinMode(in7, OUTPUT);
  pinMode(in8, OUTPUT);
  pinMode(in9, OUTPUT);
  pinMode(in10, OUTPUT);
  pinMode(in11, OUTPUT);
  pinMode(in12, OUTPUT);
  
  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  digitalWrite(in5, LOW);
  digitalWrite(in6, LOW);
  digitalWrite(in7, LOW);
  digitalWrite(in8, LOW);
  digitalWrite(in9, LOW);
  digitalWrite(in10, LOW);
  digitalWrite(in11, LOW);
  digitalWrite(in12, LOW);
  // Set motors to maximum speed
  // For PWM maximum possible values are 0 to 255
  analogWrite(enA, 255);
  analogWrite(enB, 255);
  analogWrite(enC, 255);
  analogWrite(enD, 255);
  analogWrite(enE, 255);
  analogWrite(enF, 255);
}

void loop(){
   char key = keypad.getKey();
  // read the state of the pushbutton value:
  buttonStateOpen = digitalRead(buttonPinOpen);
  buttonStateClose = digitalRead(buttonPinClose);

if (buttonStateOpen == HIGH) {

  if (key)
  {
 // lcd.clear();
//  lcd.print("Type code");
  //Serial.println(key);
  //   if(key == '*') {
  //     input_password = ""; // clear input password
  //   } else 
    if(key == '#') 
    {
      if(password == input_password) 
      {
        lcd.clear();
        lcd.print("CORRECT");
        //while loop to have red lights
        while (var1 < 1) {
          ledloop();
          ClosePiston();
          };
      }
      else
      {
        Serial.println("password is incorrect, try again");
        lcd.clear();
        lcd.print("INCORRECT");
      }
    input_password = ""; // clear input password
    }
    else
    {
      input_password += key; // append new character to input password string
      lcd.clear();
      lcd.print(input_password);
    }
  }
}
}
// This function lets you control spinning direction of motors
void ClosePiston() {
  
  // Turn on motor A
  lcd.clear();
  lcd.print("Lock A");
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  delay(2000);
  // Turn on motor B
    lcd.clear();
  lcd.print("Lock B");
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  delay(2000);
  // Turn on motor C
    lcd.clear();
  lcd.print("Lock C");
  digitalWrite(in5, HIGH);
  digitalWrite(in6, LOW);
  delay(2000);
  // Turn on motor D
    lcd.clear();
  lcd.print("Lock D");
  digitalWrite(in7, HIGH);
  digitalWrite(in8, LOW);
  delay(2000);
  // Turn on motor E
    lcd.clear();
  lcd.print("Lock E");
  digitalWrite(in9, HIGH);
  digitalWrite(in10, LOW);
  delay(2000);
  // Turn on motor F
    lcd.clear();
  lcd.print("Lock F");
  digitalWrite(in11, HIGH);
  digitalWrite(in12, LOW);
  delay(2000);
  
  // Now change motor directions
  //digitalWrite(in1, LOW);
  //digitalWrite(in2, HIGH);
  //digitalWrite(in3, LOW);
  //digitalWrite(in4, HIGH);
  //delay(2000);
  
  // Turn off motors
  digitalWrite(in1, LOW);
  delay(2000);
  digitalWrite(in3, LOW);
  delay(2000);
  digitalWrite(in5, LOW);
  delay(2000);
  digitalWrite(in7, LOW);
  delay(2000);
  digitalWrite(in9, LOW);
  delay(2000);
  digitalWrite(in11, LOW);
  lcd.clear();
  lcd.print("All closed");
  var1++;
  pinMode(LED_4, OUTPUT); 
  digitalWrite(LED_4, HIGH);
}
void ledloop()
{
  //turn on LED L1
  pinMode(LED_1, OUTPUT);     //row 1
  digitalWrite(LED_1, LOW);
  pinMode(LED_2, OUTPUT);     //row 2
  digitalWrite(LED_2, HIGH);  
  pinMode(LED_3, INPUT);      //row 3
  digitalWrite(LED_3, LOW);
  
  delay(200);
  
  //turn on LED L2
  pinMode(LED_1, OUTPUT);     //row 1
  digitalWrite(LED_1, HIGH);
  pinMode(LED_2, OUTPUT);     //row 2
  digitalWrite(LED_2, LOW);   
  pinMode(LED_3, INPUT);      //row 3
  digitalWrite(LED_3, LOW);
  
  delay(200);
  
  //turn on LED L3
  pinMode(LED_1, INPUT);      //row 1
  digitalWrite(LED_1, LOW);
  pinMode(LED_2, OUTPUT);     //row 2
  digitalWrite(LED_2, LOW);  
  pinMode(LED_3, OUTPUT);     //row 3
  digitalWrite(LED_3, HIGH);
  
  delay(200);
  
  //turn on LED L4
  pinMode(LED_1, INPUT);     //row 1
  digitalWrite(LED_1, LOW);
  pinMode(LED_2, OUTPUT);    //row 2
  digitalWrite(LED_2, HIGH);  
  pinMode(LED_3, OUTPUT);    //row 3
  digitalWrite(LED_3, LOW);
  
  delay(200);

  //turn on LED L5
  pinMode(LED_1, OUTPUT);    //row 1
  digitalWrite(LED_1, LOW);
  pinMode(LED_2, INPUT);     //row 2
  digitalWrite(LED_2, LOW);
  pinMode(LED_3, OUTPUT);    //row3
  digitalWrite(LED_3, HIGH);
  
  delay(200);
  
  //turn on LED L6
  pinMode(LED_1, OUTPUT);
  digitalWrite(LED_1, HIGH);
  pinMode(LED_2, INPUT);
  digitalWrite(LED_2, LOW);
  pinMode(LED_3, OUTPUT);
  digitalWrite(LED_3, LOW);
  
  delay(200);
}


You need to get away from using delay() for timing. See these tutorials for how to do timing with millis().

Blink without delay().
Beginner's guide to millis().
Several things at a time.

Also a Finite State Machine maybe useful.

The function ClosePiston() spends about 22 seconds doing nothing. ledloop() is completly prevented from running during that 22 seconds. The code is working properly as written.

something like this but not tested.

int var1 = 0;
const unsigned long Count2K = 2000;
const unsigned long Count200 = 200;
unsigned long pastMotorTicks = millis();
unsigned long pastLEDTicks = millis();
int motorState = 0;
int ledState = 0;
// include the library keypad & LiquidCrystal:
#include <LiquidCrystal.h>
#include <Keypad.h>

//Define constantn for the keypad
const int ROW_NUM = 4;
const int COLUMN_NUM = 3;
//Matrix for the Keypad
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
//DEFINE pins for the keypad
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
//DEFINE code for the keypad
const String password = "1234"; // password
String input_password;

//Define matrix for colors
const int LED_1 = 21;     //LED row 1
const int LED_2 = 19;     //LED row 2
const int LED_3 = 20;     //LED row 3

//Declare PIN for the green LEDS
const int LED_4 = 29;

// Declare pins for the LCD
const int rs = 30, en = 31, d4 = 35, d5 = 34, d6 = 33, d7 = 32;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#define LED 41

// PushbuttonOpen
const int buttonPinOpen = 40;
// PushbuttonClose
const int buttonPinClose = 41;
// variables will change:
int buttonmotorStateOpen = 0;
int buttonmotorStateClose = 0;

// Motor A connections
int enA = 12;
int in1 = 42;
int in2 = 43;
// Motor B connections
int enB = 13;
int in3 = 44;
int in4 = 45;
// Motor C connections
int enC = 10;
int in5 = 46;
int in6 = 47;
// Motor D connections
int enD = 11;
int in7 = 48;
int in8 = 49;
// Motor E connections
int enE = 1;
int in9 = 50;
int in10 = 51;
// Motor F connections
int enF = 2;
int in11 = 52;
int in12 = 53;

void setup() {
  Serial.begin(9600);
  input_password.reserve(32); // maximum input characters is 33, change if needed
  pinMode(LED, OUTPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Welcome");
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(enC, OUTPUT);
  pinMode(enD, OUTPUT);
  pinMode(enE, OUTPUT);
  pinMode(enF, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(in5, OUTPUT);
  pinMode(in6, OUTPUT);
  pinMode(in7, OUTPUT);
  pinMode(in8, OUTPUT);
  pinMode(in9, OUTPUT);
  pinMode(in10, OUTPUT);
  pinMode(in11, OUTPUT);
  pinMode(in12, OUTPUT);

  // Turn off motors - Initial motorState
  TurnOffMotors();
  //digitalWrite(in1, LOW);
  //digitalWrite(in2, LOW);
  //digitalWrite(in3, LOW);
  //digitalWrite(in4, LOW);
  //digitalWrite(in5, LOW);
  //digitalWrite(in6, LOW);
  //digitalWrite(in7, LOW);
  //digitalWrite(in8, LOW);
  //digitalWrite(in9, LOW);
  //digitalWrite(in10, LOW);
  //digitalWrite(in11, LOW);
  //digitalWrite(in12, LOW);
  // Set motors to maximum speed
  // For PWM maximum possible values are 0 to 255
  MotorsToMaximumSpeed();
  //analogWrite(enA, 255);
  //analogWrite(enB, 255);
  //analogWrite(enC, 255);
  //analogWrite(enD, 255);
  //analogWrite(enE, 255);
  ///analogWrite(enF, 255);
}

void MotorsToMaximumSpeed()
{
  analogWrite(enA, 255);
  analogWrite(enB, 255);
  analogWrite(enC, 255);
  analogWrite(enD, 255);
  analogWrite(enE, 255);
  analogWrite(enF, 255);
}
////
void TurnOffMotors()
{
  // Turn off motors - Initial motorState
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  digitalWrite(in5, LOW);
  digitalWrite(in6, LOW);
  digitalWrite(in7, LOW);
  digitalWrite(in8, LOW);
  digitalWrite(in9, LOW);
  digitalWrite(in10, LOW);
  digitalWrite(in11, LOW);
  digitalWrite(in12, LOW);
}

void loop()
{
  char key = keypad.getKey();
  // read the motorState of the pushbutton value:
  buttonmotorStateOpen = digitalRead(buttonPinOpen);
  buttonmotorStateClose = digitalRead(buttonPinClose);

  if (buttonmotorStateOpen == HIGH) {

    if (key)
    {
      // lcd.clear();
      //  lcd.print("Type code");
      //Serial.println(key);
      //   if(key == '*') {
      //     input_password = ""; // clear input password
      //   } else
      if (key == '#')
      {
        if (password == input_password)
        {
          lcd.clear();
          lcd.print("CORRECT");
          //while loop to have red lights
          while (var1 < 1) {
            ledloop();
            ClosePiston();
          };
        }
        else
        {
          Serial.println("password is incorrect, try again");
          lcd.clear();
          lcd.print("INCORRECT");
        }
        input_password = ""; // clear input password
      }
      else
      {
        input_password += key; // append new character to input password string
        lcd.clear();
        lcd.print(input_password);
      }
    }
  }
}
// This function lets you control spinning direction of motors
void ClosePiston()
{

  switch ( motorState )
  {

    case 0 :
      // Turn on motor A
      lcd.clear();
      lcd.print("Lock A");
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
      pastMotorTicks = millis();
      motorState = 1;
      break;
    case 1 :
      if ( (millis() - pastMotorTicks ) >= Count2K )
      {
        // Turn on motor B
        lcd.clear();
        lcd.print("Lock B");
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
        pastMotorTicks = millis();
        motorState = 2;
      }
      break;
    case 2 :
      if ( (millis() - pastMotorTicks ) >= Count2K )
      {
        // Turn on motor C
        lcd.clear();
        lcd.print("Lock C");
        digitalWrite(in5, HIGH);
        digitalWrite(in6, LOW);
        pastMotorTicks = millis();
        motorState = 3;
      }
      break;
    case 3:
      if ( (millis() - pastMotorTicks ) >= Count2K )
      {
        // Turn on motor D
        lcd.clear();
        lcd.print("Lock D");
        digitalWrite(in7, HIGH);
        digitalWrite(in8, LOW);
        pastMotorTicks = millis();
        motorState = 4;
      }
      break;
    case 4:
      if ( (millis() - pastMotorTicks ) >= Count2K )
      {
        // Turn on motor E
        lcd.clear();
        lcd.print("Lock E");
        digitalWrite(in9, HIGH);
        digitalWrite(in10, LOW);
        //delay(2000);
        pastMotorTicks = millis();
        motorState = 5;
      }
      break;
    case 5:
      if ( (millis() - pastMotorTicks ) >= Count2K )
      {
        // Turn on motor F
        lcd.clear();
        lcd.print("Lock F");
        digitalWrite(in11, HIGH);
        digitalWrite(in12, LOW);
        pastMotorTicks = millis();
        motorState = 6;
      }
      break;
    case 6:
      if ( (millis() - pastMotorTicks ) >= Count2K )
      {
        // Turn off motors
        digitalWrite(in1, LOW);
        pastMotorTicks = millis();
        motorState = 7;
      }
      break;
    case 7:
      if ( (millis() - pastMotorTicks ) >= Count2K )
      {
        digitalWrite(in3, LOW);
        pastMotorTicks = millis();
        motorState = 8;
      }
      break;
    case 8:
      if ( (millis() - pastMotorTicks ) >= Count2K )
      {
        digitalWrite(in5, LOW);
        pastMotorTicks = millis();
        motorState = 9;
      }
      break;
    case 9:
      if ( (millis() - pastMotorTicks ) >= Count2K )
      {
        digitalWrite(in7, LOW);
        pastMotorTicks = millis();
        motorState = 10;
      }
      break;
    case 10:
      if ( (millis() - pastMotorTicks ) >= Count2K )
      {
        digitalWrite(in9, LOW);
        pastMotorTicks = millis();
        motorState = 11;
      }
      break;
    case 11:
      if ( (millis() - pastMotorTicks ) >= Count2K )
      {
        digitalWrite(in11, LOW);
        lcd.clear();
        lcd.print("All closed");
        var1++;
        pinMode(LED_4, OUTPUT);
        digitalWrite(LED_4, HIGH);
        motorState = 0;
      }
      break;
    default:
      TurnOffMotors();
      motorState = 0;
      break;
  }
}
void ledloop()
{
  switch ( ledState )
  {
    case 0:
      //turn on LED L1
      pinMode(LED_1, OUTPUT);     //row 1
      digitalWrite(LED_1, LOW);
      pinMode(LED_2, OUTPUT);     //row 2
      digitalWrite(LED_2, HIGH);
      pinMode(LED_3, INPUT);      //row 3
      digitalWrite(LED_3, LOW);
      pastLEDTicks = millis();
      ledState = 1;
      break;
    case 1:
      if ( (millis() - pastLEDTicks) >= Count200 )
      {
        //turn on LED L2
        pinMode(LED_1, OUTPUT);     //row 1
        digitalWrite(LED_1, HIGH);
        pinMode(LED_2, OUTPUT);     //row 2
        digitalWrite(LED_2, LOW);
        pinMode(LED_3, INPUT);      //row 3
        digitalWrite(LED_3, LOW);
        pastLEDTicks = millis();
        ledState = 2;
      }
      break;
    case 2:
      if ( (millis() - pastLEDTicks) >= Count200 )
      {
        //turn on LED L3
        pinMode(LED_1, INPUT);      //row 1
        digitalWrite(LED_1, LOW);
        pinMode(LED_2, OUTPUT);     //row 2
        digitalWrite(LED_2, LOW);
        pinMode(LED_3, OUTPUT);     //row 3
        digitalWrite(LED_3, HIGH);
        pastLEDTicks = millis();
        ledState = 3;
      }
      break;
    case 3:
      if ( (millis() - pastLEDTicks) >= Count200 )
      {
        //turn on LED L4
        pinMode(LED_1, INPUT);     //row 1
        digitalWrite(LED_1, LOW);
        pinMode(LED_2, OUTPUT);    //row 2
        digitalWrite(LED_2, HIGH);
        pinMode(LED_3, OUTPUT);    //row 3
        digitalWrite(LED_3, LOW);
        pastLEDTicks = millis();
        ledState = 4;
      }
      break;
    case 4:
      if ( (millis() - pastLEDTicks) >= Count200 )
      {
        //turn on LED L5
        pinMode(LED_1, OUTPUT);    //row 1
        digitalWrite(LED_1, LOW);
        pinMode(LED_2, INPUT);     //row 2
        digitalWrite(LED_2, LOW);
        pinMode(LED_3, OUTPUT);    //row3
        digitalWrite(LED_3, HIGH);
        pastLEDTicks = millis();
        ledState = 5;
      }
      break;
    case 5:
      if ( (millis() - pastLEDTicks) >= Count200 )
      {
        //turn on LED L6
        pinMode(LED_1, OUTPUT);
        digitalWrite(LED_1, HIGH);
        pinMode(LED_2, INPUT);
        digitalWrite(LED_2, LOW);
        pinMode(LED_3, OUTPUT);
        digitalWrite(LED_3, LOW);
        pastLEDTicks = millis();
        ledState = 0;
      }
      break;
    default:
      ledState = 0;
      break;
  }
}

Well, I can try to rewrite that. The reason for the delay is that I have 6 motors, that are supposed to lock a door, and I don't want them the run at the same time, but have one to start every 1000ms (for example), so that it looks nicer to see when all locks are closing one after the other.
But maybe the millis is then the solution .... I'll look into that. Thanks

Hi, it seems to work, I need to create and print the rest of the 3D block, make sure that no connection is lose, and then test again. I will report to you then
THANKS !!!

1 Like

Do you know why it seems to work?

So, after 2 days of hard work on the box and the wires, I was able to test for real.
There are apparently several problems. But the main one is about the rocker.


The rocker I use are like this.
The idea being that one should be for "closing", one for opening.
And having the rocker "on" would allow the program to further start with the motors.
But the program does not care about the rockers.
So I uploaded a small code:

// PushbuttonOpen
const int buttonPinOpen = 25;
// PushbuttonClose
const int buttonPinClose = 23;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
 Serial.println("test Pin 25");
 delay(1000);
 Serial.println(digitalRead(buttonPinOpen));
 delay(1000);
Serial.println("end test 25");
 Serial.println("test Pin 23");
 delay(1000);
 Serial.println(digitalRead(buttonPinClose));
 delay(1000);
Serial.println("end test 23");
}

ON the board, I have rocker A and B.
1A&1B are linked to GND
3A&3B are lnked to 5V
2A to 23 and 2B to 25.

I was expected the code to show only "0" until I switch the rocker.
If the result (and the rocker LED with light) is "1" when the rocker are "on", it is not "0" when they are both off. It just occilates between 1 and 0 ... I just don't get it.
Is it hardware problem? when then, what??

test Pin 25
0
end test 25
test Pin 23
1
end test 23
test Pin 25
1
end test 25
test Pin 23
1
end test 23
test Pin 25
0
end test 25
test Pin 23
1
end test 23
test Pin 25
1
end test 25
test Pin 23
1
end test 23
test Pin 25
0
end test 25
test Pin 23
0
end test 23
test Pin 25
1
end test 25
test Pin 23

I mean, I need to fix this problem before going further, otherwise, I can't trigger opening or closing of the pistons.

Where in setup() are the GPIO pins configured?

How do you expect to catch a button press when your code is sleeping for most of the time? When the code is executing delay(1000) the code CANNOT detect a button press.

And you put 12V onto a MCU pins? Which MCU are you using that has a 12V GPIO?

Indeed they are not declared, I guess you meant as:

pinMode(buttonPinOpen, INPUT);
digitalWrite(buttonPinOpen, LOW);
pinMode(buttonPinClose, INPUT);
digitalWrite(buttonPinClose, LOW);

But it makes no difference. Same monitor result.
As well if I remove the delay. The fact is that, even "off", the button are still detected as sometimes high, sometimes low.

And, no, I don't put 12V, :wink: ; it was just an example picture. I put 5V in. It is just the only rocker I have for that.


You can see the full project here.
The idea behind is:
-the user turns on
-blue light is shown
-if he press on keypad, it will be asked to first select if "open" or "close"
-user switch rocker (green one), LED of rocker is on, and LCD says to enter code, red LEDs are all on
-when user is using right password, LED starts turning, and motor are working
-when finished, LED turns green
-when rocker is turned off, lights go back to blue

I hope this helps ...
THANKS again for your help
julien

It helps that when you change your code you, in a new post, post your code changes.

Post a schematic.

How are your buttons wired? If you have no pull up (or down) resistor or internal setting, the inputs will be floating random vaules with the switches open.

This should just print the sate of the two switches. I've used the internal pull up and wired each swtich between the input pin and ground. Pressed = LOW or 0, Not pressed = HIGH or 1.

const int buttonPinOpen = 25;
const int buttonPinClose = 23;

void setup() {
  Serial.begin(9600);

  pinMode(25, INPUT_PULLUP);
  pinMode(23, INPUT_PULLUP);
}

void loop() {
  Serial.print("test Pin 25         ");
  Serial.println(digitalRead(buttonPinOpen));

  Serial.print("test Pin 23    ");
  Serial.println(digitalRead(buttonPinClose));

  delay(333);
}

HTH

a7

ON the board, I have rocker A and B.
1A&1B are linked to GND
3A&3B are lnked to 5V
2A to 23 and 2B to 25.
rocker

Nop, it just provide "1" like that:
test Pin 25 1
test Pin 23 1
test Pin 25 1
test Pin 23 1
test Pin 25 1
test Pin 23 1
test Pin 25 1
test Pin 23 1
test Pin 25 1

I even just tried:

const int buttonPinOpen = 11;


void setup() {
  Serial.begin(9600);

  pinMode(11, INPUT_PULLUP);

}

void loop() {
  Serial.print("test Pin 11         ");
  Serial.println(digitalRead(buttonPinOpen));



  delay(333);
}

with another arduino, just wiring the rocker on the 5V, the GND and the 11.
Nothing, it gives 1 all the time ... There has to be something I don't get on the hardware

or this is about the rocker not being the right one ... how complicated could that be, I just want a rocker that shows a LED on when the current is going through, and not when it is off ...

PLEASE!!!!
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, component names and pin labels.
Also include power supplies.

Do you have a DMM?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Yes, I do have a multimeter if that is what you meant. And yes, I can make a hand drawing too.
But the circuit is anyway not the problem right now.
Idahowalker did mostly fix the code for the motors and the rotating LEDs.
Right now, the problem is the switch rocker.
It is a 3-pin rocker, that can be used at 5-12V, and it has a LED build-in, so you can see when it is on or off. And as simple as it sounds, I want this rocker, to trigger actions, so, I want the state of the rocker to be sent to the arduino. The arduino must be able to tell if the rocker is switched on or off. The rocker itself is not linked to any other parts of the system, only the 5V to have its LED to light on when the switch is on.
And currently, no matter if INPUT or PULLUP, I can't figure out a way to have the Rocker to either send a "HIGH" or a "LOW" (1 or 0) depending on its state (and therefore its light).
When this is fixed, I can then use it to trigger the rest of the system.

If the rocker can't even get me a constant response when it is alone on the UNO, I can't expect it to work for triggering things on the full system.
Or do I have a wrong understanding of the whole thing.

thanks
julien

I wonder if you have an accurate idea of what all is going on in these alleged rocker switches.

Please draw a standard schematic diagram, by hand and shoot a picture of it is fine, of how

  • the LED and the switch contacts in a "rocker"

are actually contributing to the circuit.

You've described it only as pin 1 goes here, pin B goes there… but what is going on inside the rocker? Draw the rocker as its two constituents so we can see why your circuit isn't working.

a7