compiling a led script - Page 1
Forum section: electronics / beginners

author: c3d20
author_rank: Contributor
timestamp: August 13, 2012, 07:50:00 pm
content: Hello my friendsThis 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: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, OUTPUT);pinMode(buttonPin2, INPUT);}void loop(){for (count=0;count<11;count++){buttonPin1State = digitalRead(buttonPin1);if (buttonPin1State == HIGH)delay(timer*4);digitalWrite(pinArray, HIGH);delay(timer);digitalWrite(pinArray, LOW);delay(timer*2);}for (count=0;count<11;count++){digitalWrite(pinArray, HIGH);delay(timer);digitalWrite(pinArray, LOW);delay(timer*2);}for (count=0;count<11;count++){digitalWrite(pinArray, HIGH);delay(timer);digitalWrite(pinArray, 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, HIGH);delay(timer);digitalWrite(pinArray, LOW);delay(timer);}else if ((buttonPin2State== HIGH) && (digitalRead(ledPin13) == HIGH))}for (count=0;count<11;count++){digitalWrite(pinArray, HIGH);delay(timer);digitalWrite(pinArray, LOW);delay(500);}else((buttonPin2State== HIGH) && (digitalRead(ledPin14) == HIGH))}for (count=0;count<11;count++){digitalWrite(pinArray, HIGH);delay(500);digitalWrite(pinArray, 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:{buttonPin2State = digitalRead(buttonPin2);if((buttonPin2State== HIGH) && (digitalRead(ledPin12) == HIGH))}for (count=0;count<11;count++ ){digitalWrite(pinArray, HIGH);delay(timer);digitalWrite(pinArray, LOW);delay(timer);}else if ((buttonPin2State== HIGH) && (digitalRead(ledPin13) == HIGH))

author: Unknown
author_rank: Unknown
timestamp: August 13, 2012, 10:24:14 pm
content: It would be more helpful if you post the entire code and the compiler error message you're getting.

author: c3d20
author_rank: Contributor
timestamp: August 13, 2012, 10:27:38 pm
content: Quote from: jucole on August 13, 2012, 10:24:14 pmThe 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

author: Unknown
author_rank: Unknown
timestamp: August 13, 2012, 11:23:45 pm
content: Quote from: c3d20 on August 13, 2012, 10:27:38 pmSorry 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.

author: Rerouter
author_rank: Super Contributor
timestamp: August 14, 2012, 10:56:26 am
content: perhaps something like this?Code:boolean Button = {1,2}; // Buttonsboolean Led = {3,4,5,6,7,8,9,10,11,12,13,14,15,16,17}; // LED'sint count = 0;int timer = 1000;void setup(){for(int L=3;L<18; L++){pinMode(L, OUTPUT);} //setting led pins as outputsfor(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=digitalRead(B);}if (Button == HIGH){ delay(timer*4);digitalWrite(Led, HIGH);delay(timer);digitalWrite(Led, LOW);delay(timer*2);digitalWrite(Led, HIGH);delay(2000);digitalWrite(Led, HIGH);delay(2000);digitalWrite(Led, HIGH);delay(2000);digitalWrite(Led[12,13,14], LOW);delay(6000);}if((Button== HIGH) && (digitalRead(Led) == HIGH)){ digitalWrite(Led, HIGH);delay(timer);digitalWrite(Led, LOW);delay(timer);}else if((Button== HIGH) && (digitalRead(Led) == HIGH)){ digitalWrite(Led, HIGH);delay(timer);digitalWrite(Led, LOW);delay(500);}else if((Button== HIGH) && (digitalRead(Led) == HIGH)){ digitalWrite(Led, HIGH);delay(500);digitalWrite(Led, 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 actionand 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 numberhope this helps, felt like returning a favour to the community after being helped out on my own starter code some time ago,

author: c3d20
author_rank: Contributor
timestamp: August 14, 2012, 06:55:49 pm
content: 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.