It looks like you can replace the If/Elseif block of code with a Switch/case block, which looks something like this:
Code: Select all
switch(case)
{
case 1:
digitalWrite(ledPin5, HIGH);
digitalWrite(ledPin6, LOW);
digitalWrite(ledPin7, LOW);
break;
case 2:
...
break;
...
...
..
}
However, if you're looking to reduce the amount of code, then writing a function to set the pins based on an input number would remove the need for the if/elseif/switch/case block altogether. The function would look something like this (I'm not 100% sure on the function syntax Arduino is expecting, so this might not be quite right)
Code: Select all
void pinoutput(int count)
{
digitalWrite(ledPin1, count & 0b1);
digitalWrite(ledPin2, (count >> 1) & 0b1);
digitalWrite(ledPin3, (count >> 2) & 0b1);
digitalWrite(ledPin4, (count >> 3) & 0b1);
}
I left-shift the bits so that the bit that I am interested in is in the LSB position. For example for the 2nd pin, I'm interested in the 2nd bit of the variable count. By doing (count >> 1), the 2nd bit of the variable ends up in the lowest position (after being left-shifted once); if count = 5, then the binary would have been 0b0101, after being left-shifted, it would be 0b0010, (the lowest bit drops off the end, I'm only showing 4 bits of the int here, the rest should be zeros).
Then because digitalWrite() expects a 1 or 0 (HIGH or LOW), I mask out all the other bits using a binary AND operation, for example the 0b0010 from before, we're really only interested in the last bit, not the other bits. so doing (count >> 1) & 0b1; effectively sets all bits other than the LSB to zero.
The final code looks something like this:
Code: Select all
/*
* count
*
* This is another basic Arduino example. It
* uses 7 led's to count from 0 to 9 in binary
* on the first 4 pins, and counts from 0 to 5
* in binary on the last 3 pins. It also outputs
* the number of the last 3 pins to the serial port.
*/
int ledPin1 = 7;
int ledPin2 = 6;
int ledPin3 = 5;
int ledPin4 = 4;
int ledPin5 = 8;
int ledPin6 = 9;
int ledPin7 = 10;
int count = 0;
int i;
void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin7, OUTPUT);
Serial.begin(9600);
Serial.println(count);
}
void loop()
{
for(i=0;i<10;i++)
{
digit1output(i);
delay(1000);
}
count = ++count;
if(count > 5) {
count = 0;
}
digit2output(i);
Serial.println(count);
}
void digit1output(int count)
{
digitalWrite(ledPin1, count & 0b1);
digitalWrite(ledPin2, (count >> 1) & 0b1);
digitalWrite(ledPin3, (count >> 2) & 0b1);
digitalWrite(ledPin4, (count >> 3) & 0b1);
}
void digit2output(int count)
{
digitalWrite(ledPin5, count & 0b1);
digitalWrite(ledPin6, (count >> 1) & 0b1);
digitalWrite(ledPin7, (count >> 2) & 0b1);
}
Code: Select all
/*
* count
*
* This is another basic Arduino example. It
* uses 7 led's to count from 0 to 9 in binary
* on the first 4 pins, and counts from 0 to 5
* in binary on the last 3 pins. It also outputs
* the number of the last 3 pins to the serial port.
*/
int ledPin1 = 7;
int ledPin2 = 6;
int ledPin3 = 5;
int ledPin4 = 4;
int ledPin5 = 8;
int ledPin6 = 9;
int ledPin7 = 10;
int count = 0;
void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin7, OUTPUT);
Serial.begin(9600);
Serial.println(count);
}
void loop()
{
count++;
if(count > 59) {
count = 0;
}
twodigit(count);
delay(1000);
}
void twodigit(int count)
{
int tens, units;
units = count % 10;
tens = (count - digit2)/10
digitalWrite(ledPin1, units & 0b1);
digitalWrite(ledPin2, (units >> 1) & 0b1);
digitalWrite(ledPin3, (units >> 2) & 0b1);
digitalWrite(ledPin4, (units >> 3) & 0b1);
digitalWrite(ledPin5, tens & 0b1);
digitalWrite(ledPin6, (tens >> 1) & 0b1);
digitalWrite(ledPin7, (tens >> 2) & 0b1);
Serial.println(tens);
}
hope this helps!
Disclaimer: I am not an Arduino expert, I have almost never used the Arduino, so some of my syntax might not be correct. My experience is in PICs and ARM, but code is in C all the same.