Go Down

Topic: When I turn off the lamp above my Arduino, 1 loop of sketch is run.  (Read 1 time) previous topic - next topic


I've just been tinkering with the first couple of projects in the Arduino starter kit, and decided to turn the lamp above the Arduino off so that I could see the lights flashing in the dark. 

To my surprise when I did this, one loop of the following program (a modification of Project 2) was run.

Code: [Select]

// global variables
int switchstate = 0;

void setup(){
  // outputs

  // inputs   

void loop(){

  // read value of pin 2
  // either HIGH or LOW
  switchstate = digitalRead(2);

  // if the button is not pressed
  if (switchstate == LOW) {
    digitalWrite(3, LOW); // turn the green LED on pin 3 on
    digitalWrite(4, LOW);  // turn the red LED on pin 4 off
    digitalWrite(5, LOW);  // turn the red LED on pin 5 off
  // if the button is pressed
  else {
    digitalWrite(3, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(5, LOW);

Later, as I was writing this post, when I turned the lamp on, it occured again.

What could be possibly causing this?

Heat from the lamp above the Arduino? 


I think this is some electrical noise between the source of your arduino and the source of the lamp (Arduino powered by pc, and pc connected to AC plug right?

Possible solution: How about debouncing the button?

Go Up