I'm working on a project for my studio class that's a sort of LED star pattern with different modes (controlled by an IR sensor) for blinking, waves, etc. So far, I've been able to make my counter work and cycle through the modes, and to make simple patterns using digitalWrite. I could stop there, but the general aim of the project is to be more calming than just twitchy lights, so I want to be able to make the LEDs fade on and off. I tried to copy the included Fade sketch into my counter 1 statment, but it doesn't even twitch on and off like my code was doing before I fixed the modes. I'm sure it's obvious, but what am I missing? Can I not use digitalWrite and analogWrite both?
int sensorPin = 0; //analog pin 0
int counter = 0; //counter
void setup(){
//initialize sensor and
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop(){
int brightness = 0;
int fadeAmount = 5;
int targetBright = 0;
int val = analogRead(sensorPin);
Serial.println(val);
//just to slow down the output - remove if trying to catch an object passing by
delay(1000);
if (val >= 1000){
counter++;
counter = counter%5;
}
Serial.println(counter);
if (counter == 1){
// set the brightness of pin 9:
analogWrite(2, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
//some else ifs here for the counters 2 - 4, cut for length
else {
//all off
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}
}
brightness is zero every time through loop. loop is essentially this...
void loop(){
int val = analogRead(sensorPin);
Serial.println(val);
//just to slow down the output - remove if trying to catch an object passing by
delay(1000);
if (val >= 1000){
counter++;
counter = counter%5;
}
Serial.println(counter);
if (counter == 1){
// set the brightness of pin 9:
analogWrite(2, 0);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
//some else ifs here for the counters 2 - 4, cut for length
else {
//all off
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}
}
I...not really. I think I understand what you were saying about how it's basically resetting brightness every time it goes through the loop, but I don't understand how I'd go about fixing that.
Final question...it only increases brightness when the sensor checks. Is there a way to get around that without completely altering the (simple) structure of my code? Or will it just have to be tied to that delay?
Eh. The only reason the sensor has a delay is so that it won't jump several numbers ahead when you pass your hand over it. It looks alright when the fade is relatively slow, so I think it's alright. Thank you for your help.