Go Down

### Topic: For LED (Read 4887 times)previous topic - next topic

#### gmmazar

##### Mar 17, 2011, 08:25 pm
int inPin = 7;      // the number of the input pin
int reading;        // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
int count=0; //number of times button was pressed
const int LedPin = 13;
const int LedPin2 = 12;
const int LedPin3 = 11;
const int LedPin4 = 10;
const int LedPin5 = 9;
const int LedPinG1 = 6;
const int LedPinG2 = 5;
const int LedPinG3 = 4;
const int LedPinG4 = 3;

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;      // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
pinMode(inPin, INPUT);
pinMode(LedPin, OUTPUT);
pinMode(LedPin2, OUTPUT);
pinMode(LedPin3, OUTPUT);
pinMode(LedPin4, OUTPUT);
pinMode(LedPin5, OUTPUT);
pinMode(LedPinG1, OUTPUT);
pinMode(LedPinG2, OUTPUT);
pinMode(LedPinG3, OUTPUT);
pinMode(LedPinG4, OUTPUT);
}

void loop()
{

// if we just pressed the button (i.e. the input went from LOW to HIGH),
// and we've waited long enough since the last press to ignore any noise...
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
//increment count
count++;

// ... and remember when the last button press was
time = millis();
}
if (count==0)
{
digitalWrite(LedPin, LOW);
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, LOW);
digitalWrite(LedPin4, LOW);
digitalWrite(LedPin5, LOW);
digitalWrite(LedPinG1, LOW);
digitalWrite(LedPinG2, LOW);
digitalWrite(LedPinG3, LOW);
digitalWrite(LedPinG4, LOW);
}
if (count==1)
{
digitalWrite(LedPin, HIGH);
digitalWrite(LedPin2, HIGH);
digitalWrite(LedPin3, HIGH);
digitalWrite(LedPin4, HIGH);
digitalWrite(LedPin5, HIGH);
digitalWrite(LedPinG1, LOW);
digitalWrite(LedPinG2, LOW);
digitalWrite(LedPinG3, LOW);
digitalWrite(LedPinG4, LOW);
}
if (count==2)
{
digitalWrite(LedPin, HIGH);
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, HIGH);
digitalWrite(LedPin4, HIGH);
digitalWrite(LedPin5, HIGH);
digitalWrite(LedPinG1, HIGH);
digitalWrite(LedPinG2, LOW);
digitalWrite(LedPinG3, LOW);
digitalWrite(LedPinG4, LOW);
}
if (count==3)
{
digitalWrite(LedPin, HIGH);
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, LOW);
digitalWrite(LedPin4, HIGH);
digitalWrite(LedPin5, HIGH);
digitalWrite(LedPinG1, HIGH);
digitalWrite(LedPinG2, HIGH);
digitalWrite(LedPinG3, LOW);
digitalWrite(LedPinG4, LOW);

}
if (count==4)
{
digitalWrite(LedPin, HIGH);
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, LOW);
digitalWrite(LedPin4, LOW);
digitalWrite(LedPin5, HIGH);
digitalWrite(LedPinG1, HIGH);
digitalWrite(LedPinG2, HIGH);
digitalWrite(LedPinG3, HIGH);
digitalWrite(LedPinG4, LOW);
}
if (count==5)
{
digitalWrite(LedPin, HIGH);
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, LOW);
digitalWrite(LedPin4, LOW);
digitalWrite(LedPin5, LOW);
digitalWrite(LedPinG1, HIGH);
digitalWrite(LedPinG2, HIGH);
digitalWrite(LedPinG3, HIGH);
digitalWrite(LedPinG4, HIGH);
//whatever you want to do over here
// ex [count=0; //reset count]
}
if (count==6)
{
count=0;
}
}

#### robtillaart

#1
##### Mar 17, 2011, 08:57 pm
PLease use the # button toplace [ code] and [ /code] tags around your code.

Can you tell us what the goal of your sketch is?

One tip: read about using array's, that could make your code much smaller - http://www.arduino.cc/en/Reference/Array

// partial code
Code: [Select]
`// partial code, will not compileconst int LedPin[9] = {13,12,11,10,9,6,5,4,3};void setup(){  pinMode(inPin, INPUT);  for (int i = 0; i< 9; i++) pinMode(LedPin[i], OUTPUT);}void loop(){...  if (count == n)  {    for (int i = 0; i< 2; i++) digitalWrite(LedPin[i], HIGH);    for (int i = 2; i< 5; i++) digitalWrite(LedPin[i], LOW);    for (int i = 5; i< 9; i++) digitalWrite(LedPin[i], HIGH);  }...}`
You could even use a 2 dimensional array to hold the values for all the pins for a certain value of count, and then you can do most of your code in one line:

Code: [Select]
`for (int i=0; i<9; i++) digitalWrite(LedPin[i], value[count][i]);`

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### Grumpy_Mike

#2
##### Mar 21, 2011, 05:38 pm
This is totally the wrong section.

Without asking a question it is hard to help.

Your only other previous post talked about a count down clock. However looking at the code, which I have seen several times from different people it looks like you are trying to get us to do your homework for you.

Go Up

Please enter a valid email to subscribe