Sending data trough Serial Monitor

Good Morning!

I’m working on simple stuff… But I’d need some help! I’m trying to code something easy: when a button is pushed, a number is printed in the serial monitor. However, there is a problem.

I need the Arduino to send a continuous number to the serial monitor. This only happens when the third button is pressed. When the first or the second buttons are pressed, the number appears with a “0” in front. And this is not good for me! Because the movies that I would like to play are not played if a zero is sent.

I think that is a debouncing problem… but I don’t know how to fix it :frowning:

I attach the code and a few screenshots of what I would need for the project, and what I am having now.

Thank you in advance for everything!!

/*
   WAITING ROOM
   INSTALLATION CODED BY Jordi Farreras).

   The code sets up a few buttons. When they are pushed,
   a number is sent to processing. Then, depending on
   the button, a video is played.
*/


//1. DECLARE PINS

int switchPin1 = 4;   // Switch connected to pin 4
int switchPin2 = 5;   // Switch connected to pin 5
int switchPin3 = 6;   // Switch connected to pin 6

//2. SET EVERYTHING UP

void setup() {
  //Serial Port begin
  Serial.begin (19200);
  //Define inputs and outputs
  pinMode(switchPin1, INPUT_PULLUP);
  pinMode(switchPin2, INPUT_PULLUP);
  pinMode(switchPin3, INPUT_PULLUP);
}

//3. THE MAGIC HAPPENS

void loop()
{
  // CODE BUTTONS

  if (digitalRead(switchPin1) == LOW) {
    Serial.println(1);
    delay(1000);
  }

  if (digitalRead(switchPin2) == LOW) {
    Serial.println(2);
    delay(1000);
  }

  if (digitalRead(switchPin3) == LOW) {
    Serial.println(3);
    delay(1000);
  }
  else {
    delay(1000);
    Serial.println(0);
    delay(50);
  }
}

Images from Original Post so we don’t have to download them. See this Simple Image Guide

24ad9cae58c04067357631562847aedaf6da61e4.png

00c6ce7b78d7523674a025f0d012ca1ddc1c2b5f.png

…R

Maybe I am missing the point but why not just remove the line

Serial.println(0);

from your program?

By the way, you could remove all the delay(1000) lines in the different IF clauses and replace them with a single delay(1000) as the last item in loop().

Better still, don't use delay() at all as it blocks the Arduino from doing other things during the delay() period. The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

try this:

void loop
{

   int val1 = digitalRead(switchPin1);
   int val2 = digitalRead(switchPin2);
   int val3 = digitalRead(switchPin3);

   if (val1 == HIGH || val2 == HIGH || val3 == HIGH) {
      if (val1 == HIGH) {
         Serial.println(1);
         delay(1000);
      }
      if (val2 == HIGH) {
         Serial.println(2);
         delay(1000);
      }
      if (val3 == HIGH) {
         Serial.println(3);
         delay(1000);
      }
   } else {
      //none of the buttons are pressed
      delay(1000);
      Serial.println(0);
   }
}

advise: try to learn millis() instead of delay() for timming

arduino_new:
try this:
advise: try to learn millis() instead of delay() for timming

Thank you! It worked perfectly!!! :slight_smile:

————

Robin2:
Images from Original Post so we don’t have to download them. See this Simple Image Guide

24ad9cae58c04067357631562847aedaf6da61e4.png

00c6ce7b78d7523674a025f0d012ca1ddc1c2b5f.png

…R

Sorry… I’m such a newbie! And thank you for the tutorial of the millis() function. It was pretty useful!
:wink: