Advice for LED Program Needed


so many posting here new arduino , coding.  i've been reading rear end off , trying understand how programs working.  i've made decent progress far, going through ladyada's tutorials, shiftout, , others.  in fact i've lashed best code both places make program starting down road need go...it works it's not working right , i'm not sure it's best road go down.  i've placed code far @ bottom.  what want have multiple light patterns displayed on custom led light bar (think knight rider kit better), , have each pattern scroll through 1 @ time each press of button.  my code below start doesn't listen button, think problem it's executing first light code it's not watching see press of button.  would millis() fix possibly?  don't know how code yet work on it.  or there better way don't know about?

and guess big overall question is nested if() statement style i'm using in code below best way go doing want do?  or there better structure use can read on , try impliment?  eventually when have light programs (about 8 total) coded way want them effect, i'll replace button car remote set
(http://cgi.ebay.com/rf-wireless-superheterodyne-rolling-code-rx-module-tx_w0qqitemz290296099145qqihz019qqcategoryz33723qqcmdzviewitemqq_trksidzp1742.m153.l1262)
and have programs scroll through each press of remote...but that's later on!

thanks in advance help/advice may able provide!  :)


//pin connected st_cp of 74hc595
int latchpin = 8;
//pin connected sh_cp of 74hc595
int clockpin = 12;
////pin connected ds of 74hc595
int datapin = 11;
//pin connected buttonswitch
int switchpin = 2;

int val; //intial value
int val2; //second value reading
int buttonstate; //state of button

int lightmode = 0; //to start off with

//holders infromation you're going pass shifting function
byte data;
byte dataarray[15];

void setup() {
 //set pins output because addressed in main loop
 pinmode(latchpin, output);
 pinmode(switchpin, input);
 serial.begin(9600);

 //arduino doesn't seem have way write binary straight code
 //so these values in hex.  decimal have been fine, too.
 dataarray[0] = 0x81;  //10000001
 dataarray[1] = 0xc3;  //11000011
 dataarray[2] = 0xe7;  //11100111
 dataarray[3] = 0xff;  //11111111
 dataarray[4] = 0x7e;  //01111110
 dataarray[5] = 0x3c;  //00111100
 dataarray[6] = 0x18;  //00011000
 dataarray[7] = 0x00;  //00000000
 dataarray[8] = 0x18;  //00011000
 dataarray[9] = 0x3c;  //00111100
 dataarray[10] = 0x7e; //01111110
 dataarray[11] = 0xff; //11111111
 dataarray[12] = 0xe7; //11100111
 dataarray[13] = 0xc3; //11000011
 dataarray[14] = 0x81; //10000001

buttonstate = digitalread(switchpin);

}

void loop() {
 
 val = digitalread(switchpin);      // read input value , store in val
 delay(10);                         // 10 milliseconds amount of time
 val2 = digitalread(switchpin);     // read input again check bounces
 if (val == val2) {                 // make sure got 2 consistant readings!
   if (val != buttonstate) {          // button state has changed!
     if (val == low) {                // check if button pressed
       if (lightmode == 0) {          // if off
         lightmode = 1;               // turn lights on!
       } else {
         if (lightmode == 1) {        // if all-on
           lightmode = 2;             // make wave
         } else {
           if (lightmode == 2) {      // if waving
             lightmode = 0;           // turn off
           }
         }
       }
     }
   }
   buttonstate = val;                 // save new state in our variable
 }


 if (lightmode == 0) {
   digitalwrite(latchpin, 0);
   shiftout(datapin, clockpin, 0);
   digitalwrite(latchpin, 1);
 }


 if (lightmode == 1) {
   for (int j = 0; j < 15; j++) {
     //load light sequence want array
     data = dataarray[j];
     //ground latchpin , hold low long transmitting
     digitalwrite(latchpin, 0);
     //move 'em out
     shiftout(datapin, clockpin, data);  
     //return latch pin high signal chip
     //no longer needs listen information
     digitalwrite(latchpin, 1);
     delay(125);
 }
}
 if (lightmode == 2) {
   for (int j = 0; j < 8; j++) {
   lightshiftpina(j);
   delay(150);
 }
}
}

//this function uses bitwise math move pins up
void lightshiftpina(int p) {
 //defines local variable
 int pin;

 //this line uses bitwise operator
 //shifting bit left using << same
 //as multiplying decimal number two.
 pin = 1<< p;

 //ground latchpin , hold low long transmitting
 digitalwrite(latchpin, 0);
 //move 'em out
 shiftout(datapin, clockpin, pin);  
 //return latch pin high signal chip
 //no longer needs listen information
 digitalwrite(latchpin, 1);
}


// heart of program
void shiftout(int mydatapin, int myclockpin, byte mydataout) {
 // shifts 8 bits out msb first,
 //on rising edge of clock,
 //clock idles low

 //internal function setup
 int i=0;
 int pinstate;
 pinmode(myclockpin, output);
 pinmode(mydatapin, output);

 //clear out in case to
 //prepare shift register bit shifting
 digitalwrite(mydatapin, 0);
 digitalwrite(myclockpin, 0);

 //for each bit in byte mydataout?
 //notice counting down in our loop
 //this means %00000001 or "1" go through such
 //that pin q0 lights.
 for (i=7; i>=0; i--)  {
   digitalwrite(myclockpin, 0);

   //if value passed mydataout , bitmask result
   // true then... if @ i=6 , our value is
   // %11010100 code compares %01000000
   // , proceeds set pinstate 1.
   if ( mydataout & (1<<i) ) {
     pinstate= 1;
   }
   else {      
     pinstate= 0;
   }

   //sets pin high or low depending on pinstate
   digitalwrite(mydatapin, pinstate);
   //register shifts bits on upstroke of clock pin  
   digitalwrite(myclockpin, 1);
   //zero data pin after shift prevent bleed through
   digitalwrite(mydatapin, 0);
 }

 //stop shifting
 digitalwrite(myclockpin, 0);
}




Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Advice for LED Program Needed


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