Author Topic: compiling a led script  (Read 3360 times)

0 Members and 1 Guest are viewing this topic.

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
compiling a led script
« on: August 13, 2012, 07:50:00 pm »
Hello my friends
This is what Ive got written and it seems okay to me but I think there are a few noob mistakes. Please help me edit so it is compiled correctly.


Code: [Select]
const int ledPin1 =  3;
const int ledPin2 =  4;
const int ledPin3 =  5;
const int ledPin4 =  6;
const int ledPin5 =  7;
const int ledPin6 =  8;
const int ledPin7=  9;
const int ledPin8 =  10;
const int ledPin9 =  11;
const int ledPin10 =  12;
const int ledPin11 =  13;

const int ledPin12 =  14;
const int ledPin13 =  15;
const int ledPin14 =  16;

const int ledPin15 =  17;


const int buttonPin1 = 1;
const int buttonPin2 = 2;


int pinArray[] = {
  3, 4, 5, 6, 7,8,9,10,11,12,13};
int count = 0;
int timer = 1000;
int buttonPin1State = 0;
int buttonPin2State = 0;


void setup()
{



 
    pinMode(pinArray[count], OUTPUT);
    pinMode(buttonPin2, INPUT);
  }


  void loop()
  {
    for (count=0;count<11;count++)
    {
      buttonPin1State = digitalRead(buttonPin1);
      if (buttonPin1State == HIGH)
        delay(timer*4);

      digitalWrite(pinArray[count], HIGH);
      delay(timer);
      digitalWrite(pinArray[count], LOW);
      delay(timer*2);
    }
    for (count=0;count<11;count++)
    {
      digitalWrite(pinArray[count], HIGH);
      delay(timer);
      digitalWrite(pinArray[count], LOW);
      delay(timer*2);
    }
    for (count=0;count<11;count++)
    {
      digitalWrite(pinArray[count], HIGH);
      delay(timer);
      digitalWrite(pinArray[count], LOW);
      delay(timer*2);
{
{


      digitalWrite(ledPin12, HIGH);
      delay(2000);
      digitalWrite(ledPin13, HIGH);
      delay(2000);
      digitalWrite(ledPin14, HIGH);
      delay(2000);
      digitalWrite(ledPin12, LOW);
      digitalWrite(ledPin13, LOW);
      digitalWrite(ledPin14, LOW);
      delay(6000);

    }
   

 
{
buttonPin2State = digitalRead(buttonPin2);

 
if((buttonPin2State== HIGH) && (digitalRead(ledPin12) ==  HIGH))
 
               
     }
      for (count=0;count<11;count++ )
      {
        digitalWrite(pinArray[count], HIGH);
        delay(timer);
        digitalWrite(pinArray[count], LOW);
        delay(timer);
}
     

else if ((buttonPin2State== HIGH) && (digitalRead(ledPin13) ==  HIGH))
      }
      for (count=0;count<11;count++)
      {
        digitalWrite(pinArray[count], HIGH);
        delay(timer);
        digitalWrite(pinArray[count], LOW);
        delay(500);

      }
    else((buttonPin2State== HIGH) && (digitalRead(ledPin14) ==  HIGH))
    }

    for (count=0;count<11;count++)
    {
      digitalWrite(pinArray[count], HIGH);
      delay(500);
      digitalWrite(pinArray[count], LOW);
      delay(0);

    }
}


and how how can i do it so if led (12) high and button 2 is pressed(high) to change the delay time in the led pinarray. This seemed the most logical but I dont know if it will work. This code setup is as follows:

Code: [Select]

{
buttonPin2State = digitalRead(buttonPin2);

 
if((buttonPin2State== HIGH) && (digitalRead(ledPin12) ==  HIGH))
 
               
     }
      for (count=0;count<11;count++ )
      {
        digitalWrite(pinArray[count], HIGH);
        delay(timer);
        digitalWrite(pinArray[count], LOW);
        delay(timer);
}
     

else if ((buttonPin2State== HIGH) && (digitalRead(ledPin13) ==  HIGH))

« Last Edit: August 13, 2012, 08:12:37 pm by c3d20 »
 

jucole

  • Guest
Re: compiling a led script
« Reply #1 on: August 13, 2012, 10:24:14 pm »
It would be more helpful if you post the entire code and the compiler error message you're getting.
 

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Re: compiling a led script
« Reply #2 on: August 13, 2012, 10:27:38 pm »
It would be more helpful if you post the entire code and the compiler error message you're getting.

The full code is the uppermost. Theres probably loads of errors. It may not even be the correct sort of function. How would you write the code for button 2?

Thanks
 

jucole

  • Guest
Re: compiling a led script
« Reply #3 on: August 13, 2012, 11:23:45 pm »
It would be more helpful if you post the entire code and the compiler error message you're getting.

The full code is the uppermost. Theres probably loads of errors. It may not even be the correct sort of function. How would you write the code for button 2?
Thanks

Sorry I didn't see that.  (crappy ipads!)

Ok, Firstly I think you're really brave to jump in with something like this.  Did you really write this yourself?

To be honest you really need to grasp the basics of the code construction. Especially the difference between { and } there are loads of online tutorials once you get a basic grasp, you will be able to fix this yourself.



« Last Edit: August 13, 2012, 11:25:58 pm by jucole »
 

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4694
  • Country: au
  • Question Everything... Except This Statement
Re: compiling a led script
« Reply #4 on: August 14, 2012, 10:56:26 am »
perhaps something like this?

Code: [Select]
boolean Button[2] = {1,2}; // Buttons
boolean Led[15] = {3,4,5,6,7,8,9,10,11,12,13,14,15,16,17}; // LED's
int count = 0;
int timer = 1000;

void setup()
  {
    for(int L=3;L<18; L++)
      {pinMode(L, OUTPUT);} //setting led pins as outputs
       
    for(int B=1;B<3; B++)
      {pinMode(B, INPUT);} //setting button pins as inputs
  }
void loop()
  {
    for (count=0;count<11;count++) // your count loop
    {
        for (int B=1;B<3; B++) // read both buttons
      {  Button[B]=digitalRead(B);
      }
        if (Button[1] == HIGH)
      {   delay(timer*4);

          digitalWrite(Led[count], HIGH);
          delay(timer);
          digitalWrite(Led[count], LOW);
          delay(timer*2);
          digitalWrite(Led[12], HIGH);
          delay(2000);
          digitalWrite(Led[13], HIGH);
          delay(2000);
          digitalWrite(Led[14], HIGH);
          delay(2000);
          digitalWrite(Led[12,13,14], LOW);
          delay(6000);
      }
        if((Button[2]== HIGH) && (digitalRead(Led[12]) ==  HIGH))
      {   digitalWrite(Led[count], HIGH);
          delay(timer);
          digitalWrite(Led[count], LOW);
          delay(timer);     
      }
        else if((Button[2]== HIGH) && (digitalRead(Led[13]) ==  HIGH))
      {   digitalWrite(Led[count], HIGH);
          delay(timer);
          digitalWrite(Led[count], LOW);
          delay(500);
      }
        else if((Button[2]== HIGH) && (digitalRead(Led[14]) ==  HIGH))
      {   digitalWrite(Led[count], HIGH);
          delay(500);
          digitalWrite(Led[count], LOW);
   }}}

you had a bunch of {'s and }'s where they didnt belong, and i felt like tidying it up a bit,

few tid bits, the actions of each command are enclosed within those 2, so for instance, the first if statement, you dont want the second nested inside it but rather as its own action
and for the count range, if you nest everything else inside it, you only need to declare it once,

now i did remove your repeated repeated flashes of the led pins as i wasnt quite sure why they where but they are easily re-added, equally i removed the 0 delay on your last step, as i see no point to it, also some of your lines i simply made use of the arrays to shorten into a single line, and also set up your pins as booleans as they are either on or off, rather than a number

hope this helps, felt like returning a favour to the community after being helped out on my own starter code some time ago,
« Last Edit: August 14, 2012, 11:01:28 am by Rerouter »
 

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Re: compiling a led script
« Reply #5 on: August 14, 2012, 06:55:49 pm »
Great thanks I edited mine the best I could and changed some using your script. I m not sure if the booleans will work with what I want to do later though but I was considering and now I have a better idea how they work. Thank you. I know they go at front and back and number up but I get confused and as the script keeps changing I forget things.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf