need a quick syntax check (pwm led + pushbutton)


so i've got script check see status of software push button, , turns on , off color fading rgb led. problem can't red power up, goes green --> blue , jumps green.

if on code , see if i've on looked something, appreciated.

code: [select]
/*
rgb led fade - million colors, push button on/off.
*/

//pins definition

#define greenpin  9
#define bluepin   10
#define redpin    11
#define actionbtn 12
#define statusled 13

/* ** program variables ** */

//button variables
int btnval = 0;
int softval = 0;
int bounceinterval = 50;
int bouncetime = 0;
int lastbtnval = high;

//rgb led million color fade variables
int greenval = 255; // variables store values send pins
int blueval  = 1;  // initial values green full, blue , red off
int redval   = 1;
int i2       = 0;  // loop counter  
int wait     = 50;  // 50ms (.05 second) delay; shorten faster fades

//pulsation variables
long previousmilis = 0;
int interval       = 0;
int              = 0;
int valled         = 0;
int stopnextrun    = 0;


// output
//int greenpin = 9;  // green led, connected digital pin 9
//int bluepin  = 10;  // blue led,  connected digital pin 10
//int redpin   = 11;   // red led,   connected digital pin 11

// setup
void setup() {
 pinmode(greenpin, output);
 pinmode(bluepin, output);
 pinmode(redpin, output);
 pinmode(actionbtn, input);
 
 serial.begin(9600);
 
//  reportavrstate(3,1);
}
void loop() {
 btnval = digitalread(actionbtn);
 
//  if (btnval) {
//    readremotesoftval();
//  }
 
 if (btnval == low && lastbtnval == high && bouncetime != -1) {
   lastbtnval = low;
   serial.print("going low");
 }
 
 if (bouncetime == -1 && btnval == high) {
   bouncetime = 0;
 }
 
 if (btnval == low && lastbtnval == low) {
   bouncetime++;
   if (bouncetime >= bounceinterval) {
     bouncetime = -1;
     btnval = high;
     serial.print("action button debounced");
   }
 }
 
 if (btnval == high && lastbtnval == low) {
   softval = !softval;
   lastbtnval = high;
   serial.print("going high softval to: ");
   serial.print(softval);
 }
 
 // want led off!
 if (!softval) {
   digitalwrite(greenpin, softval);
   digitalwrite(bluepin, softval);
   digitalwrite(redpin, softval);
   previousmilis = 0;
   valled = 0;
   i = 0;
 }
 
 //begin color fades
 else {
 i2 += 1;      // increment counter
 if (i < 255) // first phase of fades
 {
   greenval   -= 1; // green down
   blueval += 1; // blue up
   redval   = 1; // red low
 }
 else if (i < 509) // second phase of fades
 {
   greenval    = 1; // green low
   blueval -= 1; // blue down
   redval  += 1; // red up
 }
 else if (i < 763) // third phase of fades
 {
   greenval  += 1; // green up
   blueval = 1; // blue low
   redval -= 1; // red down
 }
 else // re-set counter, , start fades again
 {
   i2 = 1;
 }  

 analogwrite(greenpin, greenval);   // write current values led pins
 analogwrite(bluepin, blueval);
 analogwrite(redpin, redval);

 delay(wait); // pause 'wait' milliseconds before resuming loop
 }
}
 
 void readremotesoftval ( void ) {
   int incomingbyte = 0;
   
   //send data when recieve data:
   if (serial.available() > 0) {
     
   if (incomingbyte == 49) {
     btnval&= low;
   }
 }
 
 btnval&= high;
}

//reports state of controller via pin 13's led , serial
//void reportavrstate(int howmanytimes, int leaveon) {
// int i;
 
//  pinmode(statusled, output);
 
//  for (i=0; i< howmanytimes; i++) {
//    digitalwrite(statusled, high);
//    delay(200);
//    digitalwrite(statusled, low);
//    delay(200);
//  }
 
//  if (leaveon) {
//    ditialwrite(statusled, high);
//  }
 
//  serial.printin("avr initialized");
//}


// {
//  pinmode(greenpin,   output);   // sets pins output
//  pinmode(bluepin, output);  
//  pinmode(redpin,  output);
//  pinmode(inpin, input);
//  pinmode(outpin, output);
//  }

the thing notice of program logic based on value of variable "i"... never change value of variable "i".  it zero.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > need a quick syntax check (pwm led + pushbutton)


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums