Pages: [1]   Go Down
Author Topic: Structuring to due two or more sub programs -Caution NOOB alert :)  (Read 749 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am currently learning the basics of Arduino and programming but have a project in mind to control a fair few LED's for a digital pinabll cabinet that I am building. They will be recessed into the side panels to glow behind the decal that I am going to get printed.

I have found some code examples and have had no problems getting them to work, but now I am looking to combine a few to all work at once but have run into a problem. I want a couple of groups of LED's to work with different sequences but the way I have structured my code, one sequence affects the other for obvious reasons but I have no idea where to start to seperate them to work independently.

Code:
/*Pinball Side Lights

LED Control Bride of Pinbot
*/

const byte LED0=2;//LED 1
const byte LED1=3;//LED 2
const byte LED2=4;//LED 3
const byte LED3=5;//LED 4
const byte LED4=6;//LED 5
const byte LED5=7;//LED 6
const byte LED6=8;//LED 7
const byte LED7=9;//LED 8

int ledPin = 10;
const word delayDuration=1000;

void setup()//"setup" always present
{
   pinMode(LED0,OUTPUT);
   pinMode(LED1,OUTPUT);
   pinMode(LED2,OUTPUT);
   pinMode(LED3,OUTPUT);
   pinMode(LED4,OUTPUT);
   pinMode(LED5,OUTPUT);
   pinMode(LED6,OUTPUT);
   pinMode(LED7,OUTPUT);


}

void loop()//This function always present

{
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(2);                           
  }

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(2);                           
  }
//show zero: All LEDs off, - - -
      setLEDs(0,0,0,0,0,0,0,0);
      delay(delayDuration);//Delay between lights

//1st LED
      setLEDs(0,0,0,0,0,0,0,1);
      delay(delayDuration);

//2nd LED
      setLEDs(0,0,0,0,0,0,1,0);
      delay(delayDuration);

//3rd LED
      setLEDs(0,0,0,0,0,1,0,0);

      delay(delayDuration);

//LED 4
      setLEDs(0,0,0,0,1,0,0,0);
      delay(delayDuration);

//LED 5
      setLEDs(0,0,0,1,0,0,0,0);
      delay(delayDuration);

//LED 6
      setLEDs(0,0,1,0,0,0,0,0);
      delay(delayDuration);

//LED 7
setLEDs(0,1,0,0,0,0,0,0);
      delay(delayDuration);

//LED 8
setLEDs(1,0,0,0,0,0,0,0);
      delay(delayDuration);

//LED 8+7  Begin Count Back and adding light each step
      setLEDs(1,1,0,0,0,0,0,0);
      delay(delayDuration);

//LED 8+7+6
      setLEDs(1,1,1,0,0,0,0,0);
      delay(delayDuration);

//LED 8+7+6+5
      setLEDs(1,1,1,1,0,0,0,0);
      delay(delayDuration);

//LED 8+7+6+5+4
      setLEDs(1,1,1,1,1,0,0,0);
      delay(delayDuration);

//LED 8+7+6+5+4+3
      setLEDs(1,1,1,1,1,1,0,0);
      delay(delayDuration);

//LED 8+7+6+5+4+3+2
      setLEDs(1,1,1,1,1,1,1,0);
      delay(delayDuration);

//All On
setLEDs(1,1,1,1,1,1,1,1);
      delay(delayDuration);

//All On
setLEDs(1,1,1,1,1,1,1,1);
      delay(delayDuration);

//All On
setLEDs(1,1,1,1,1,1,1,1);
      delay(delayDuration);


// and finally....
   delay(delayDuration);
         //Delay to start again.
}


 


void setLEDs(byte bL7, byte bL6,byte bL5, byte bL4, byte bL3,byte bL2, byte bL1, byte bL0)
{
   if (bL0==0) digitalWrite(LED0,LOW);
               else  digitalWrite(LED0,HIGH);
   if (bL1==0) digitalWrite(LED1,LOW);
               else  digitalWrite(LED1,HIGH);
   if (bL2==0) digitalWrite(LED2,LOW);
               else  digitalWrite(LED2,HIGH);
   if (bL3==0) digitalWrite(LED3,LOW);
               else  digitalWrite(LED3,HIGH);
   if (bL4==0) digitalWrite(LED4,LOW);
               else  digitalWrite(LED4,HIGH);
   if (bL5==0) digitalWrite(LED5,LOW);
               else  digitalWrite(LED5,HIGH);
   if (bL6==0) digitalWrite(LED6,LOW);
               else  digitalWrite(LED6,HIGH);
   if (bL7==0) digitalWrite(LED7,LOW);
               else  digitalWrite(LED7,HIGH);
 
 }





Can someone please point me in the right direction on where to read and learn more about how to structure my code correctly to make the PWM work the whole time the setLED sequenceis going through its steps?

Thanks in advance.
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 63
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Right now your code is structured in a "do a, then b, then c, etc..." fashion.  You will need to implement a state-machine that determines what each LED should do at a given time.  You could use the millis()  function or even a simple "tick counter."

Here is an explanation I wrote on how to get 2 LEDs to flash (independently.)
http://www.cmiyc.com/blog/2011/01/06/millis-tutorial/

If you can understand how to make that work, then your next step is writing a function that takes the current "counter" as an argument.  That function will update the LEDs based on the amount of time that has passed.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks very much for the relpy James. Got some more reading to do now.   smiley-grin
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void setLEDs(byte bL7, byte bL6,byte bL5, byte bL4, byte bL3,byte bL2, byte bL1, byte bL0)
{
   if (bL0==0) digitalWrite(LED0,LOW);
               else  digitalWrite(LED0,HIGH);

Simple tip:
Code:
void setLEDs(byte bL7, byte bL6,byte bL5, byte bL4, byte bL3,byte bL2, byte bL1, byte bL0)
{
 digitalWrite(LED0, bL0);

Also, arrays will help enormously, as will bit-packing and unpacking.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks AWOL for your reply. I am looking to learn arrays next to do (a well needed) clean up of my code.

Another question, can someone let me know why my LED's would be dimly lit when they are supposed to be set to off while going through the stepping sequence? I assume its due to the output floating but I am not sure where I am wrong with my code.

Again apologies for my Noobness. My skills are in electrical and electronics, not so good with code unless comodore 64 basic counts  smiley-lol

Code:
int value, value2 ;
int ledpin = 10;                           // light connected to digital pin 10
int ledpin2 = 11;                           // light connected to digital pin 11
long time=0;

int periode = 3000;
int displace = 800;


const byte LED0=2;//LED 1
const byte LED1=3;//LED 2
const byte LED2=4;//LED 3
const byte LED3=5;//LED 4    Large LED's for 1-8 Million
const byte LED4=6;//LED 5
const byte LED5=7;//LED 6
const byte LED6=8;//LED 7
const byte LED7=9;//LED 8

long startSeq;
long delaySet = 300;
long waitUntil0;
long waitUntil1;
long waitUntil2;
long waitUntil3;
long waitUntil4;
long waitUntil5;
long waitUntil6;
long waitUntil7;
long waitUntil8;
long waitUntil9;
long waitUntil10;
long waitUntil11;
long waitUntil12;
long waitUntil13;
long waitUntil14;
long waitUntil15;
long waitUntil16;
long waitUntil17;
long waitUntil18;
long waitUntil19;
long waitUntil20;
long waitUntil21;
long waitUntil22;
long waitUntil23;
long waitUntil24;
long waitUntil25;
long waitUntil26;

boolean LED10state = true;

void setup()
{
  Serial.begin(9600); //PC comms
 
   pinMode(LED0,OUTPUT);
   pinMode(LED1,OUTPUT);
   pinMode(LED2,OUTPUT);
   pinMode(LED3,OUTPUT);
   pinMode(LED4,OUTPUT);
   pinMode(LED5,OUTPUT);
   pinMode(LED6,OUTPUT);
   pinMode(LED7,OUTPUT);
   pinMode(10, OUTPUT);

}

void loop()
{
  time = millis();
  value = 128+127*cos(2*PI/periode*time);
  value2 = 128+127*cos(2*PI/periode*(displace-time));
  analogWrite(ledpin, value);           // sets the value (range from 0 to 255)
  analogWrite(ledpin2, value2);           // sets the value (range from 0 to 255)


 
//show zero: All LEDs off, - - -
                 
//1st LED;
      setLEDs(0,0,0,0,0,0,0,1);
    if (millis() >= waitUntil0) {
      //2nd LED
      setLEDs(0,0,0,0,0,0,1,0);
       if(millis()>=waitUntil1) {
         //3rd LED
      setLEDs(0,0,0,0,0,1,0,0);
       if(millis()>=waitUntil2) {
         //LED 4
      setLEDs(0,0,0,0,1,0,0,0);
       if(millis()>=waitUntil3) {
         //LED 5
      setLEDs(0,0,0,1,0,0,0,0);
       if(millis()>=waitUntil4) {
         
       //LED 6
      setLEDs(0,0,1,0,0,0,0,0);
        if(millis()>=waitUntil5) {
//LED 7
      setLEDs(0,1,0,0,0,0,0,0);
       if(millis()>=waitUntil16) {

//LED 8
      setLEDs(1,0,0,0,0,0,0,0);
       if(millis()>=waitUntil17) {

//LED 8+7  Begin Count Back and adding light each step
      setLEDs(1,1,0,0,0,0,0,0);
       if(millis()>=waitUntil18) {

//LED 8+7+6
      setLEDs(1,1,1,0,0,0,0,0);
     if(millis()>=waitUntil19) {

//LED 8+7+6+5
      setLEDs(1,1,1,1,0,0,0,0);
       if(millis()>=waitUntil20) {

//LED 8+7+6+5+4
      setLEDs(1,1,1,1,1,0,0,0);
       if(millis()>=waitUntil21) {

//LED 8+7+6+5+4+3
      setLEDs(1,1,1,1,1,1,0,0);
      if(millis()>=waitUntil22) {

//LED 8+7+6+5+4+3+2
      setLEDs(1,1,1,1,1,1,1,0);
     if(millis()>=waitUntil23) {

//All On
      setLEDs(1,1,1,1,1,1,1,1);
      if(millis()>=waitUntil24) {

//All off
      setLEDs(0,0,0,0,0,0,0,0);
       if(millis()>=waitUntil25) {


         
      waitUntil0 =millis() + delaySet;
      waitUntil1 =millis() + (delaySet * 2);
      waitUntil2 =millis() + (delaySet * 3 );
      waitUntil3 =millis() + (delaySet * 4 );
      waitUntil4 =millis() + (delaySet * 5);
      waitUntil5 =millis() + (delaySet * 6);
      waitUntil6 =millis() + (delaySet * 7);
      waitUntil7 =millis() + (delaySet * 8);
      waitUntil8 =millis() + (delaySet * 9);
      waitUntil9 =millis() + (delaySet * 10);
      waitUntil10 =millis() + (delaySet * 11);
      waitUntil11 =millis() + (delaySet * 12);
      waitUntil12 =millis() + (delaySet * 13);
      waitUntil13 =millis() + (delaySet * 14);
      waitUntil14 =millis() + (delaySet * 15);
      waitUntil15 =millis() + (delaySet * 16);
      waitUntil16 =millis() + (delaySet * 17);
      waitUntil17 =millis() + (delaySet * 18);
      waitUntil18 =millis() + (delaySet * 19);
      waitUntil19 =millis() + (delaySet * 20);
      waitUntil20 =millis() + (delaySet * 21);
      waitUntil21 =millis() + (delaySet * 22);
      waitUntil22 =millis() + (delaySet * 23);
      waitUntil23 =millis() + (delaySet * 24);
      waitUntil24 =millis() + (delaySet * 28);
      waitUntil25 =millis() + (delaySet * 35);
}
   
      }}}}}}}}
       }}}}}}}}


void setLEDs(byte bL7, byte bL6,byte bL5, byte bL4, byte bL3,byte bL2, byte bL1, byte bL0)
{
digitalWrite(LED0,bL0);             
digitalWrite(LED1,bL1);
digitalWrite(LED2,bL2);
digitalWrite(LED3,bL3);
digitalWrite(LED4,bL4);
digitalWrite(LED5,bL5);
digitalWrite(LED6,bL6);
digitalWrite(LED7,bL7);
             
 
 }

Logged

Pages: [1]   Go Up
Jump to: