Hi all,
Im actually writing some tutorial codes with my arduino UNO. . they were good at start now when i put some own modifications, it seems not working. Im quite afraid it is coming from hardware.
A simple sketch, is i pressed the pushbutton to light all my 5 leds, and i pressed the same to put them to low.
However, when the leds are on, they goes out without me pressing the button again after a while, as if an off delay is present.
I tried to figure out what is happening, and i wrote some codes to monitor within the serial monitor, when the serial monitor is opened, i can see all my variables being read, and my leds i.e. output work normally, no shut off delay.
As soon as i close my serial monitor, same thing happen leds goes out within a delay. quite weird.
I tried to change my sketch with something else, a blinky state this time, same system, ON state with button and same push button to OFF mode. Again same thing happening, as if an off delay is present though it is not in my code.
Can someone please advise, Thanks . . .
The code below:
/*
- Blink ON & OFF with same switch . . .
*/int switchPin = 2; // switch is connected to pin 2
int LED1 = 12; // LED1 connected to pin 12
int LED2 = 11; // LED2 connected to pin 11
int LED3 = 10; // LED3 connected to pin 10
int LED4 = 9; // LED4 connected to pin 9
int LED5 = 8; // LED5 connected to pin 8int buttonState; // variable to hold the last button state
int val; // Variable to compare with buttonState
int val2; // Variable to read the debounced status
int lightMode = 0; // Variable to set LEDs ON / OFF depending on mode.void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(LED1, OUTPUT); // Set LED1 as ouput
pinMode(LED2, OUTPUT); // Set LED2 as ouput
pinMode(LED3, OUTPUT); // Set LED3 as ouput
pinMode(LED4, OUTPUT); // Set LED4 as ouput
pinMode(LED5, OUTPUT); // Set LED5 as ouputSerial.begin(9600); // Set up serial communication at 9600bps
buttonState = digitalRead(switchPin); // read the initial state}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
Serial.print("val=");
Serial.println (val);
delay(10);val2 = digitalRead(switchPin); // Read input again to check for bounces
Serial.print("val2 =");
Serial.println (val2);
if (val == val2){
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (lightMode == 0){ // Check status of lightMode
lightMode = 1; // Sets lightMode to high, LEDs ON
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,HIGH);
}
else{
lightMode = 0; // Sets lightMode to Low, LEDs OFF
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
}
}
}buttonState = val; // save the new state in our variable
Serial.print("buttonState =");
Serial.println (buttonState);
}
}
Looking forward ..
Thanks
Taz ...