Tuesday, April 19, 2011

YAY small victory!

Forget the pull-up/pull-down resistors (for now), I fixed my problem with code! I went to pseudocode to problem solve. I wrote this:
I want to make the code do this:

IF analogRead > 0 for some amount of time (some # of measurements)
ledPin, HIGH
DELAY ____

ELSE ledPin LOW

The data is being transferred so quickly that any tiny fluctuation in light that the photocell registers will instantly flick the LED. I won't notice this when it's in a HIGH state, because  the milliseconds the LED goes off it will just look a bit dimmer, but I expect the LOW state to be entirely off, not just dimmer than fully on. Right? So putting a little delay on the LOW state
For a complete code newb like myself, this is a little taste of victory.

_____________________________

int analogPin = 0;
int ledPin = 13;
int val;                       

void setup() {
  pinMode(analogPin, INPUT);
pinMode(ledPin, OUTPUT);
}


void loop(){
  val = analogRead(analogPin); 
  if (val > 0) {              
    digitalWrite(ledPin, LOW);       
delay(500);
  }
  else  {         
    digitalWrite(ledPin, HIGH); 
  }

}

No comments:

Post a Comment