Guys
In a sketch I have written, I use 4 switches to determine both the game level and # of players.
There is a 5 sec window to make the selections. If I press, say, button 1, that sets level 1 & 1 player. Further presses, up to 4, increments the # of players; button 3, sets game level 3 with addition presses increasing # of players.
No matter how I tried, pressing any of the 4 buttons caused it to rip through and select the game level correctly, and immediately selects 2, then 3, then 4 players.
Switch bounce? Checked with the DSO, and the worst was <10uS. That should not have caused a problem, as the s/w flip-flop would block that.
Started to include Serial.println to monitor what the hell was going on. Doing a print out of the Serial Monitor data showed it working correctly. It only incremented the players when the switch was pressed.
Commented out the Serial.prints and it did everything on the first press, once again.
I tried delay(20) immediately after the first button press, to see if that did anything. No difference.
Here is the code, with the Serial,prints commented out.
const byte switchpin[] = {2, 4, 6, 8};
const byte leds[] = {3, 5, 7, 9};
byte Players = 0;
byte Game_Level = 0;
byte First_Button_Pressed_Flag = 0;
byte button_count_Flag = 0;
byte Start_Flag = 0;
bool currentbuttonstate = true;
bool previousbuttonstate = true;
unsigned long Test_Millis = 0;
unsigned long LED_Millis = 0;
void setup()
{
Serial.begin(9600);
pinMode(switchpin[0], INPUT_PULLUP);
pinMode(switchpin[1], INPUT_PULLUP);
pinMode(switchpin[2], INPUT_PULLUP);
pinMode(switchpin[3], INPUT_PULLUP);
pinMode(leds[0], OUTPUT);
pinMode(leds[1], OUTPUT);
pinMode(leds[2], OUTPUT);
pinMode(leds[3], OUTPUT);
// LED_Millis = millis();
}
void loop()
{
if (Start_Flag == 0)
{
digitalWrite(leds[0], HIGH);
for (int i = 0; i <= 3; i++) digitalWrite(leds[i], HIGH);
Start_Flag = 1;
Test_Millis = millis();
}
if (millis() - Test_Millis <= 5000ul)
{
for (byte i = 0; i < 4; i++)
{
if ((digitalRead(switchpin[i]) == LOW) && (currentbuttonstate == true)) // If a button is pressed
{
// Delay(20);
/* Serial.println("Here1");
Serial.print("i = ");
Serial.println(i);
Serial.println("Button pressed");
Serial.print(currentbuttonstate);
Serial.print(" ");
Serial.print(previousbuttonstate);
Serial.println(" "); */
if (First_Button_Pressed_Flag == 0) // Have I been here
{
Game_Level = i; // This gives me the Select Game Level #, 0-3
First_Button_Pressed_Flag = 1; // I've been here
/* Serial.println("Here2");
Serial.print("i = ");
Serial.println(i);
Serial.print(currentbuttonstate);
Serial.print(" ");
Serial.print(previousbuttonstate);
Serial.println(" "); */
}
currentbuttonstate = digitalRead(switchpin[i]);
delay(10);
/* Serial.println("Here3");
Serial.print("i = ");
Serial.println(i);
Serial.print(currentbuttonstate);
Serial.print(" ");
Serial.print(previousbuttonstate);
Serial.println(" "); */
if (currentbuttonstate == 0 && previousbuttonstate == 1) //%%%%%%
{
// Serial.println("Flip");
Players++;
Serial.print("Players = ");
Serial.println(Players);
Serial.print("Game Level = ");
Serial.println(Game_Level);
Serial.println();
Serial.println();
previousbuttonstate = 0;
// delay(50);
if (Players >= 4)
{
Serial.println("Here at end");
while (1);
}
/* Serial.println("Here4");
Serial.print("i = ");
Serial.println(i);
Serial.print(currentbuttonstate);
Serial.print(" ");
Serial.print(previousbuttonstate);
Serial.println(" "); */
} //%%%%%%%%%
}
if ((digitalRead(switchpin[i]) == HIGH) && (previousbuttonstate == false)) // If a button is released
{
currentbuttonstate = digitalRead(switchpin[i]);
previousbuttonstate = 1;
/* Serial.println("Here5");
Serial.print("i = ");
Serial.println(i);
Serial.println("Button pressed");
Serial.print(currentbuttonstate);
Serial.print(" ");
Serial.print(previousbuttonstate);
Serial.println(" "); */
}
/* Serial.println("Here6");
Serial.print("i = ");
Serial.println(i);
Serial.print(currentbuttonstate);
Serial.print(" ");
Serial.print(previousbuttonstate);
Serial.println(" "); */
}
}
}
Any ideas as to why it only works while the Serial.print lines are in?
TIA
Fof
Amended the title.