hey guys please help me ...
im simply reading the status of pin no 12 to which i hv connected a pushbutton , on serial monitor
but its continuously repeating the same status like when button is off its displaying 1 1 1 1 1 1 1 1 1 1 1 11 and vice versa... i want to get out of this.. it should show 1 at once when button is off and 0 only at once when button is on and again the same..
thanks in advance .. pls help me im new to the programming
{
//start serial connection
Serial.begin(9600);
//configure pin12 as an input and enable the internal pull-up resistor
pinMode(12, INPUT_PULLUP);
pinMode(13, OUTPUT);
}
void loop()
{
//read the pushbutton value into a variable
int sensorVal = digitalRead(12);
//print out the value of the pushbutton
Serial.println(sensorVal);
Save the value each time into oldVal. Then when you get the current Val, do an if to see if it's the same oldVal. Print if it's different, don't print if the same.
You should probably also allow for debouncing the button push.
something like this covers both issues.
unsigned long previousButton = 0; // will store last time Button was executed
#define Buttonpin 12 // pin attached to interrupt button
#define Buttondelay 500 // minimum time before repeating Button - for debounce
int OldSensorVal = 0;
void setup() {
//start serial connection
Serial.begin(9600);
pinMode(13, OUTPUT);
//configure buttonpin as an input and enable the internal pull-up resistor
pinMode(Buttonpin, INPUT_PULLUP);
}
void loop() {
//read the pushbutton value into a variable
int sensorVal = digitalRead(Buttonpin);
//print out the value of the pushbutton
if(sensorVal != OldSensorVal && millis() >= (previousButton + Buttondelay))
{
Serial.println(sensorVal);
digitalWrite(13, sensorVal);
previousButton = millis();
}
}