Switches for pass & fail

Hello, everyone, I somewhat new to arduino, I have always used it for small programs but not seriously to get stumped like now. what I am trying to accomplish for this small little project is to have a 2 switches and 1 would be for pass and the other would be for fail and each has an LED for visual verification, after every time either switch is pressed a screen prompts to hit enter to clear . i have already a code and its kind of working, but the problem is, when either switch is pressed all is good, problem is when i try to clear with enter i get a delay and have to press enter several times before it clears. check out the code and see if i can get help.

Thanks in Advance

test_pass_fail.ino (2.7 KB)

Please copy the contents of your INO file and paste them between code tags so no one has to download your file, and make a folder on their computer just to determine if they can answer your question.

#include <SPI.h>
#include <EEPROM.h>

// set pin numbers:
const byte passPin = 4; // the number of the pushbutton pin
const byte failPin = 6;

const int passLed = 9;
const int failLed = 10; // the number of the LED pin

// variables will change:
int passState = 0; // variable for reading the pushbutton status
int failState = 0;

// chara varible to receive.
char rx_byte = 0;

// Counter for the functions
int counter1 = 0;
int counter2 = 0;

void setup() {
Serial.begin(9600); // start serial communication…

// initialize the LED pin as an output:
pinMode(passLed, OUTPUT);
pinMode(failLed, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(passPin, INPUT_PULLUP);
pinMode(failPin, INPUT_PULLUP);
}

void loop()
{
Pass();
Fail();
}

void Pass(){

// read the state of the pushbutton value:
passState = digitalRead(passPin); // call and assign pass pin function

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (passState == LOW) {

counter1 ++ ; // get count for pass function
digitalWrite(passLed, HIGH); // turn LED on:
Serial.print(counter1); // print the count for pass function
Serial.print(" | Pass: "); // screen print on the same line as count
Serial.println(“Press ‘Enter’ to continue”); // screen print on the same line as count
delay(300); // delay set min 300ms to give time for the function to complete
}
if (Serial.available() > 0) { // is a character available?
rx_byte = Serial.read();

switch (rx_byte) {
case ‘\r’: // set Return as char respond.
digitalWrite(passLed, LOW); //Serial.println(“LED is off”);
break;

default:
Serial.println(“Invalid option”);
delay(50);
break;

switch (rx_byte){
if (Serial.available() > 0){}

}
}
}
}

void Fail(){
// read the state of the pushbutton value:
failState = digitalRead(failPin); // call and assign pass pin function

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (failState == LOW) {
// turn LED on:
counter2 ++ ;
digitalWrite(failLed, HIGH);

Serial.print(counter2);
Serial.print(" | Fail: ");
Serial.println(“Press ‘Enter’ to continue”);
delay(300);
}
if (Serial.available() > 0) { // is a character available?
rx_byte = Serial.read();

switch (rx_byte) {
case ‘\r’:
digitalWrite(failLed, LOW);
//Serial.println(“LED is off”);
break;

default:
Serial.println(“Invalid option”);
delay(50);

break;

switch (rx_byte){
if (Serial.available() > 0){}

}
}
}
}

A few problems:

  1. You forgot to use code tags.
  2. The brackets are horrible to align. What closes which open?
  3. This
    switch (rx_byte){
    if (Serial.available() > 0){}
    is a weird statement. I'm not sure what you are trying to do. But read next before you think too much on it.
  4. You read and act on the Serial.available() in two places, always calling both. I suggest you un comment the Serial.Println in both and make the message more useful (such as "Pass LED off" and "Fail LED Off"). I think then you will see the program is acting on the serial input but you are having to press enter multiple times in order to catch the correct (corresponding) Serial.available().

I would move and combine the serial available into a single subroutine. Then add a test_status variable (0=none, 1=pass, 2=fail) set in each Pas() or Fail() subroutine which can then be used when you receive the enter, and decide which LED to turn off based on which LED is on per the new test_status variable.

code tags?? I don't get it!

whats the second problem?

Program is running good, just the small delay after i hit 'Enter' when i want to clear, but everything is running good.
i am playing millis, but some how i am getting still problem

thanks,

that makes lots of sense, i will go ahead and try that, and i apologize for poor programming abilities as i am just learning as i go along…

once again I appreciate the help.

gbetromero:
code tags?? I don’t get it!

before your code, press the </> icon above, then past the code in the box. or type
[ code ] without the spaces before your code and [ /code ] without the spaces after your code.
It will then look something like

this, which is very handy to hit the select link above if I want to take it into my IDE, among other things, like it's own scroll bar.

gbetromero:
thanks,

that makes lots of sense, i will go ahead and try that, and i apologize for poor programming abilities as i am just learning as i go along..

once again I appreciate the help.

Place each { and } on its own line

void subroutine()
 {
  if (true)
   {
    do_true_stuff();
   }
   else
    {
      do_else_stuff();
    }
  endif
 }

Aligning the braces in the manner provides instant alignment of the open and close braces and visual contain of the commands within each condition.