Arduino PWM LED's not fading in or out -
i attempting make arduino haunted pumpkin using pir sensor trigger led's lights , mouth. want mouth led's shut off immediately, want eyes fade away. i've been working on hours , can't figure out why eye led's not fade in or out, though same code snippit works in it's own program fine. missing small , easy, cannot seem find it.
be gentle. know code messy. i've tried numerous different things , tend comment them out rather deleting in case need them later.
//uses pir sensor detect movement. //randomly selects number corresponds set of led's //lights led's //author: //date: 11/12/2014 int inputpin = a1; // choose input pin (for pir sensor) int pirstate = low; // start, assuming no motion detected int val = 0; // variable reading pin status //int randnumber; //variable holding random number //int eyeblue =6; //variable blue eye led's //int eyered =9; //variable red eye led's //int eyegreen =3; //variable green eye led's //int mouthblue =10; // variable blue mouth led's //int mouthred = 11; //variable red mouth led's //int mouthgreen = 5; //variable green eye led's int eyespin = 0; int mouthpin = 0; int moutharray[] ={ 5,10,11}; int eyesarray[] = { 3,6,9}; //int thispin ; void setup(){ serial.begin(9600); pinmode(inputpin, input); // declare pir sensor input pinmode(3, output); pinmode(5, output); pinmode(6, output); pinmode(9, output); pinmode(10, output); pinmode(11, output); //randomseed(analogread(0)); } void motion(){ val = digitalread(inputpin); // read input value if (val == high) { // check if input high eyes(); //mouth(); delay(1000); if (pirstate == low) { // have turned on serial.println("motion detected!"); // want print on output change, not state pirstate = high; delay(300); } } else { delay(300); if (pirstate == high){ // have turned of serial.println("motion ended!"); // want print on output change, not state pirstate = low; } } } void eyes(){ eyespin = eyesarray [random (0, 3)]; mouthpin = moutharray[random (0, 3)]; for(int fadevalue = 0 ; fadevalue <= 255; fadevalue +=5) { digitalwrite(eyespin, fadevalue); // turn led on (high voltage level) delay(30); } digitalwrite(mouthpin, high); // turn led on (high voltage level) delay(500); // wait second for(int fadevalue = 255 ; fadevalue >= 0; fadevalue -=5) { digitalwrite(eyespin, fadevalue); // turn led off making t delay(30); } digitalwrite(mouthpin, low); // turn led off making voltage low delay(500); // wait second } void loop(){ // eyes(); // mouth(); motion(); }
very simple oversight. line:
for(int fadevalue = 255 ; fadevalue >= 0; fadevalue -=5) { digitalwrite(eyespin, fadevalue);
should written:
for(int fadevalue = 255 ; fadevalue >= 0; fadevalue -=5) { analogwrite(eyespin, fadevalue);
notice analogwrite rather digitalwrite. digital write can produce values 0/1, need 0-255.
hope fixes things you.
Comments
Post a Comment