Hi good people!
I am currently going through the beginning stages of learning about coding for arduino circuits and keep running into the issue that my code feels extremely bulky as soon as I start doing something that is semi complicated to me.
For instance, I have built a circuit with a few LEDs that have essentially two main goals (this is just to try and get a better understanding of working with potentiometers and switches before building an Arduino based guitar pedal)
- Lights turn on two at a time based on the potentiometer reading (kinda like car racing start lights/traffic lights), then after the potentiometer reaches a certain resistance,
- The LEDs flash in sequence with a delay between each switching on and off based on the potentiometer reading.
My code is currently as follows, would really appreciate if someone could guide me to how to make this less bulky, as making this more complicated would likely lead to more mistakes and issues in the circuit.
// C++ code
//
int pot = A5; // Define potmeter wiper input as A5
int potValue = 0; // Define default value of potmeter
int switch_pin = 2; // allocate switch to digital pin 2
int switch_state; // ??
int redled1 = 8; // Define redled1 input as Digital pin 8
int redled2 = 9; // Define redled2 input as Digital pin 9
int orangeled1 = 10; // Define orangeled1 input as Digital pin 10
int orangeled2 = 11; // Define orangeled2 input as Digital pin 11
int greenled1 = 12; // Define greenled1 input as Digital pin 12
int greenled2 = 13; // Define greenled2 input as Digital pin 13
void setup()
{
Serial.begin(9600); //Start serial monitor with 9600 baud
pinMode(8, OUTPUT); //Define pin 8 as OUTPUT
pinMode(9, OUTPUT); //Define pin 9 as OUTPUT
pinMode(10, OUTPUT); //Define pin 10 as OUTPUT
pinMode(11, OUTPUT); //Define pin 11 as OUTPUT
pinMode(12, OUTPUT); //Define pin 12 as OUTPUT
pinMode(13, OUTPUT); //Define pin 13 as OUTPUT
pinMode(pot, INPUT); //Define pin A5 (potmeter) as INPUT
pinMode(switch_pin, INPUT_PULLUP); //Define Switch pin as input
}
void loop()
{
int switch_state = digitalRead(switch_pin); //Make variable read of switch and define switch state to read from pin
int potValue = analogRead(pot); //Read analog value of potmeter
int delayTime = map(potValue, 342, 1023, 100, 5); //Map lower end of resistor to higher delay between flashes
Serial.println(potValue); //Enable printing of the pot value in Serial Monitor
delay(100); //delay read (is this necessary??)
if (switch_state==LOW) //If switch is off, do nothing
{
digitalWrite(13,0);
digitalWrite(12,0);
digitalWrite(11, 0);
digitalWrite(10, 0);
digitalWrite(9, 0);
digitalWrite(8, 0);
}
else
{
if (potValue==0) //If pot Value is zero, keep LEDs off (Should this be removed given the function (term?) above?
{
digitalWrite(13,0);
digitalWrite(12,0);
}
else if(potValue>0 && potValue<=113) //If pot Value is between x & y, switch on green LEDs and keep Orange and Red Off
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, 0);
digitalWrite(10, 0);
}
else if(potValue>=114 && potValue<=226) //If pot Value is between x & y, switch on green & Orange LEDs and keep Red Off
{
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, 0);
digitalWrite(8, 0);
}
else if(potValue>=227 && potValue<=341) //If pot Value is between x & y, switch on green, Orange & Red LEDs
{
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
}
else if(potValue>=342 && potValue<=1023) //If pot value is between x & y, flash leds one and off based on potmeter reading
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
delay(delayTime);
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
delay(delayTime);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
delay(delayTime);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
delay(delayTime);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
delay(delayTime);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
}
}
}
Please keep in mind I am pretty much a beginner so my understanding of the intricacies of coding in C++ is limited, so there might be a few mistakes in the comments. Any and all corrections or suggestions would be greatly appreciated.