In this function, I try to gradually light a set of LED eyes from being off up to full brightness. Once they reach full brightness, I want them to flash on and off a few times. You can see from my “Serial.print” commands that I’ve been trying to figure this out.
Is there a problem with mixing variable types? My delayTime variable I called a byte, and I add it to lastTime, which is unsigned long.
I print the value of variable “eyeBrightness” to the serial monitor, and it’s always 0.
I also print the value of variable “lastTime,” and it seems to be updating properly.
I really have no clue why not – if it’s a scope problem, or variable type problem, or syntax. I’ve been trying to fix this for a while and hope someone can spot a problem.
The “idleBeast” function is just a bunch of digitalWrite(LOW) commands to turn off all the outputs before I start this sequence. Sometimes I treat this PWM pin as a digital output and sometimes as an analog, so I am mixing up digitalWrite and analogWrite to that pin. I’m not sure if that can cause problems or not.
[color=#CC6600]void[/color] throbTheEyes()
{
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#006699]"You are at the start of throbTheEyes function"[/color]);
idleBeast(); [color=#7E7E7E]//This function includes a delay to give time to release button[/color]
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#006699]"Still in throb function, idleBeast was called and you have returned."[/color]);
[color=#7E7E7E]//Next are some variables to track time.[/color]
[color=#CC6600]unsigned[/color] [color=#CC6600]long[/color] lastTime=[color=#CC6600]millis[/color]();
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"Millis value is "[/color]);
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](lastTime);
[color=#CC6600]byte[/color] delayTime=14; [color=#7E7E7E]//ms to wait before brightening eyes[/color]
[color=#7E7E7E]//Next is a variable to track brightness of eyes[/color]
[color=#CC6600]byte[/color] eyeBrightness=0;
[color=#CC6600]int[/color] blinkTimer=1250; [color=#7E7E7E]//When eyes blink, 1250 milliseconds on, 1250 ms off[/color]
[color=#7E7E7E]//What follows is a loop to run the eye throbbing sequence.[/color]
[color=#CC6600]do[/color]{ [color=#7E7E7E]//Starts infinite loop.[/color]
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#006699]"You made it to the do loop."[/color]);
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"eyeBrightness variable is "[/color]);
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](eyeBrightness);
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"lastTime variable is "[/color]);
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](lastTime);
lastTime=[color=#CC6600]millis[/color](); [color=#7E7E7E]//updates the last time we changed eyes[/color]
checkInputs();
[color=#CC6600]if[/color] (eyeBrightness<255 && [color=#CC6600]millis[/color]()>=lastTime + delayTime) [color=#7E7E7E]//1st stage is for brightening the eyes.[/color]
{
[color=#CC6600]analogWrite[/color](LEDpin,eyeBrightness);
lastTime=[color=#CC6600]millis[/color]();
eyeBrightness=eyeBrightness++;
}
[color=#CC6600]if[/color] (eyeBrightness==255){
lastTime=[color=#CC6600]millis[/color]();
[color=#CC6600]byte[/color] blinkCounter=0;
[color=#CC6600]digitalWrite[/color](LEDpin,[color=#006699]HIGH[/color]);
eyesLit=[color=#CC6600]true[/color];
[color=#CC6600]do[/color]{
checkInputs(); [color=#7E7E7E]//Checks to see if a button is pressed on remote.[/color]
[color=#CC6600]if[/color] ([color=#CC6600]millis[/color]()>=lastTime+blinkTimer && eyesLit==[color=#CC6600]true[/color]){
[color=#CC6600]digitalWrite[/color](LEDpin,[color=#006699]LOW[/color]);
eyesLit=[color=#CC6600]false[/color];
lastTime=[color=#CC6600]millis[/color]();
blinkCounter++;
}
[color=#CC6600]if[/color] ([color=#CC6600]millis[/color]()>=lastTime+blinkTimer && eyesLit==[color=#CC6600]false[/color]){
[color=#CC6600]digitalWrite[/color](LEDpin,[color=#006699]HIGH[/color]);
eyesLit=[color=#CC6600]true[/color];
lastTime=[color=#CC6600]millis[/color]();
blinkCounter++;
}
} [color=#CC6600]while[/color] (blinkCounter<8);
eyeBrightness=0; [color=#7E7E7E]//Reset variable to work right in the brightening sequence.[/color]
}
} [color=#CC6600]while[/color](alwaysTrue==[color=#CC6600]true[/color]); [color=#7E7E7E]//end of infinite loop[/color]
}