I had a sketch working but changed the method to check on buttonpress. I now have the following algorithm that does nothing:
while (!Expose) {
ReadButtons();} // readbutton
SwLEDs();
The function ReadButtons reads the buttons. In this case the button Expose that changes the vairable Expose from false to true when pressed. It should wait to start the function SwLEDs() until the button is pressed, but nada.
This is a common case where we need to see the whole sketch, because your issue hinges on how you modify the variable 'Expose' in the code that isn't shown.
I had to narrow the sketch down a lot and came up with the following:
boolean Sw[12]; // array to hold the values read from buttons 0-9
boolean &Expose = Sw[0]; // use recognisable names for the different switches
boolean &White = Sw[1];
boolean &Red = Sw[2];
boolean &Plus = Sw[3];
boolean &Minus = Sw[4];
boolean &Shift = Sw[5];
boolean &BurnProof = Sw[6];
boolean &Filter = Sw[7];
boolean &Paper = Sw[8];
boolean &Reset = Sw[9];
int Nr=5;
boolean sw = false; // boolean to check if any switch is pressed (in that case sw=true, else false)
int V1 = 300; // value 1 to check buttonpress: if analogread < V1 button 0 (or 3)
int V2 = 750; // value 2 to check buttonpress: if V1 <= analogread < V2 button 1 (or 4); if analogread > V2, button 2 (or 5)
int value;
int PinBt[3] = {A0,A1,A2};
void setup()
{ Serial.begin(9600); }
void loop()
{ while (!BurnProof) {
SetNr(); }
Serial.println (" Start again");
}
void SetNr(){
ReadButtons;
if (Plus ) {Nr = Nr + 1 ;}
if (Minus ) {Nr = Nr - 1 ;}
Serial.println (Nr);
delay(1000);
return;
}
void ReadButtons()
{ for (int i=0; i<= 9; i++)
{Sw[i] = false;}
boolean sw = false; // use sw to remember buttonstate and set to false (no button pressed)
//
//
while (!sw) // keep reading until a button is pressed
{
for (int j=0; j<= 1; j++) { // loop to read all inputs 2 times
for (int i=0; i<= 2; i++) { // loop to read the 3 inputs
int value = analogRead (PinBt[i]); // analogread input i
if (value > 20) {
sw = true; // only values > 20 are considered, in that case one of the buttons is pressed and sw is set to true
int k = 3 * i ; // calculate which button is pressed:
if (sw && value < V1) {Sw[k] = true;} // Serial.println(k);} // if value greater than 50 and less than V1 it was switch k that was pressed
if (value >= V1 && value <= V2) {Sw[k+1] = true;} //Serial.println(k+1);} // if value greater than V1 and smaller than V2 it was switch k+1 that was pressed
if (value >= V2) {Sw[k+2] = true;} //Serial.println(k+2);} // if value greater than V2 it was switch k+2 that was pressed
delay (200); } } } // delay to debounce
//
for (int i=0; i<=8; i++) {
if (Sw[i])
{Serial.print ("Sw "); Serial.println (i); }}
Serial.println ("sw0, sw 1, sw2, sw3, sw4, sw5, sw6, sw7, sw8");
Serial.print (Expose);Serial.print(White); Serial.print(Red); Serial.print(Plus);
Serial.print( Minus); Serial.print(Shift); Serial.print(BurnProof); Serial.print(Filter); Serial.println(Paper);
//
}
return;
}
OK, think them there. This is not the sketch in question but a sketsch to demonstrate the workings:
The ReadButtons() functions works, it sets the viariable " Sw* " (the text doesnt show square bracket i square bracket) to true when button i is pressed. otherwise all Sw are false. No problem there,* The function SetNr() is not an actual function but one for testing. I would expect the loop to change Nr when the Sw[3] or Sw[4] button is pressed (Sw[6] will be false) and to print "Start again" when the button Sw[6] is pressed. This should change the value of Sw[6] to true, thus falsifying the while statement and exiting to the print of "Start again". While (!x) ... means: as long as x = false do ..., doesn't it?
TheMemberFormerlyKnownAsAWOL: } } } Don't you just hate it when that happens?
Yep.
The OP has the most unique, not to mention confusing and unreadable, indentation and brace style that I think I’ve ever seen. And I’ve seen a few in my time....
Let's start with a more conventionally formatted version courtesy of Auto format in the IDE
boolean Sw[12]; // array to hold the values read from buttons 0-9
boolean &Expose = Sw[0]; // use recognisable names for the different switches
boolean &White = Sw[1];
boolean &Red = Sw[2];
boolean &Plus = Sw[3];
boolean &Minus = Sw[4];
boolean &Shift = Sw[5];
boolean &BurnProof = Sw[6];
boolean &Filter = Sw[7];
boolean &Paper = Sw[8];
boolean &Reset = Sw[9];
int Nr = 5;
boolean sw = false; // boolean to check if any switch is pressed (in that case sw=true, else false)
int V1 = 300; // value 1 to check buttonpress: if analogread < V1 button 0 (or 3)
int V2 = 750; // value 2 to check buttonpress: if V1 <= analogread < V2 button 1 (or 4); if analogread > V2, button 2 (or 5)
int value;
int PinBt[3] = {A0, A1, A2};
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (!BurnProof)
{
SetNr();
}
Serial.println (" Start again");
}
void SetNr()
{
ReadButtons;
if (Plus )
{
Nr = Nr + 1 ;
}
if (Minus )
{
Nr = Nr - 1 ;
}
Serial.println (Nr);
delay(1000);
return;
}
void ReadButtons()
{
for (int i = 0; i <= 9; i++)
{
Sw[i] = false;
}
boolean sw = false; // use sw to remember buttonstate and set to false (no button pressed)
//
//
while (!sw) // keep reading until a button is pressed
{
for (int j = 0; j <= 1; j++) // loop to read all inputs 2 times
{
for (int i = 0; i <= 2; i++) // loop to read the 3 inputs
{
int value = analogRead (PinBt[i]); // analogread input i
if (value > 20)
{
sw = true; // only values > 20 are considered, in that case one of the buttons is pressed and sw is set to true
int k = 3 * i ; // calculate which button is pressed:
if (sw && value < V1)
{
Sw[k] = true; // if value greater than 50 and less than V1 it was switch k that was pressed
} // Serial.println(k);}
if (value >= V1 && value <= V2)
{
Sw[k + 1] = true; // if value greater than V1 and smaller than V2 it was switch k+1 that was pressed
} //Serial.println(k+1);}
if (value >= V2)
{
Sw[k + 2] = true; // if value greater than V2 it was switch k+2 that was pressed
} //Serial.println(k+2);}
delay (200);
}
}
} // delay to debounce
//
for (int i = 0; i <= 8; i++)
{
if (Sw[i])
{
Serial.print ("Sw ");
Serial.println (i);
}
}
Serial.println ("sw0, sw 1, sw2, sw3, sw4, sw5, sw6, sw7, sw8");
Serial.print (Expose);
Serial.print(White);
Serial.print(Red);
Serial.print(Plus);
Serial.print( Minus);
Serial.print(Shift);
Serial.print(BurnProof);
Serial.print(Filter);
Serial.println(Paper);
//
}
return;
}
Thanks. I simplified the sketch to get it more to the point. Now it seems to work.
It prints something while it is in the while loop and something else when I press 0.
I learned how to use the while statement and how to use a referenced variable. Thanks a lot.
boolean Sw[1]; // array to hold the values read from buttons 0-9
boolean &Expose = Sw[0] = false; // use recognisable names for the different switches
char rx_byte = 0;
//
void setup()
{
Serial.begin(9600);
}
//
void loop()
{
while (!Expose)
{
ReadButtons();
delay (1000);
Serial.println ("While condition fullfilled");
}
Serial.println ("While condition no longer fullfilled");
Expose = false;
}
//
//
void ReadButtons()
{
rx_byte = 0;
Sw[0] = false;
if (Serial.available() > 0)
{
rx_byte = Serial.read();
Serial.print("Number received: "); Serial.println(rx_byte);
if ((rx_byte == '0') )
{
Sw[0] = true;
}
}
}