Hello forum, im new here and im new to arduino, so i hope im posting the right place
i have a problem, i want to output the status of a button,
there is example code for it, where a led light up when the button is pressed.
But i want to output text to my pc and here the board behaves strangely, i hope you can help me!
The signal seems to get more and more unstable for every serial.print command i use.
Here is an example to show what i mean
The original code (from the website):
/* Button
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2. */
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
When i measure I get a stable signal of 5V when the button is not pressed.
But when i now add the code to output it to a pc, the signal gets unstable (new lines marked with //NEW LINE):
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600); //NEW LINE!
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
Serial.print(" leftButton: "); //NEW LINE
// check if the pushbutton is pressed.
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
Serial.print("0"); //NEW LINE
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
Serial.print("1"); //NEW LINE
}
delay(250); //NEW LINE
}
And it gets worse for every Serial.print i add, when I write:
Serial.print(" leftButton: ");Serial.print(" leftButton: ");Serial.print(" leftButton: ");Serial.print(" leftButton: ");Serial.print(" leftButton: ");Serial.print(" leftButton: ");
instead of just
Serial.print(" leftButton: ");
i get a signal which varies from 3-5V instead of stable 5V when the button is not pressed
I dont think it will be able to measure input as digital if it varies so much.
what am i doing wrong?