Serial Monitor false prints

sketch_SMButton.ino (479 Bytes)

Goodday everyone,

I have attached the .ino file to this.

I am trying a simple sketch to turn on an LED when pressed (following a Youtube video tutorial.) I used an "if" condition statement to read the button and turn on an LED and also print some text on the Serial monitor.

When i upload the code, the Serial monitor prints the text even without pressing the button vut the LED works as supposedly.

I have both the SM and LED code in the "if" statement but only the LED follows the instruction.

int buttonpin = 6;  //push button
int ledyellow = 7;  //yellow LED
float delset = 0;   //delay set time
int pinread;

void setup() {
  Serial.begin(9600);
  pinMode(buttonpin, INPUT_PULLUP);
  pinMode(ledyellow, OUTPUT);
}

void loop() {
  pinread = digitalRead(buttonpin);

  if (pinread == LOW) {
    //delset = delset + 5;
    Serial.print("ms > ");
    Serial.println(delset);
    digitalWrite(ledyellow, HIGH);
    }
    else {
      digitalWrite(ledyellow, LOW);
    }
  }

I doubt anybody here will click that link. Please post your entire code using code tags.

1 Like

Can you please descibe how i do that? I post from a mobile device web browser.

**edit

Nevermind.

Never is a long time.

Did you try initializing pinread state?

void setup() {
  Serial.begin(9600);
  pinMode(buttonpin, INPUT_PULLUP);
  pinMode(ledyellow, OUTPUT);
  pinread = HIGH;
}

then in void loop() resetting the state?

    else {
      digitalWrite(ledyellow, LOW);
    }
    pinread = HIGH;
}

Full Sketch (as I tested it, you'll have to change the LED pin back):

int buttonpin = 6;  //push button
int ledyellow = LED_BUILTIN;  //yellow LED
float delset = 0;   //delay set time
int pinread;

void setup() {
  Serial.begin(9600);
  pinMode(buttonpin, INPUT_PULLUP);
  pinMode(ledyellow, OUTPUT);
  pinread = HIGH;
}

void loop() {
  pinread = digitalRead(buttonpin);
  if (pinread == LOW) {
    //delset = delset + 5;
    Serial.print("ms > ");
    Serial.println(delset);
    digitalWrite(ledyellow, HIGH);
    }
    else {
      digitalWrite(ledyellow, LOW);
    }
    pinread = HIGH;
  }

Thanks @hallowed31

Would try this and revert.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.