# How would I simplify this?

OK, so my last thread didn’t go to well, the thing I was trying to do didn’t work, but I made it, but the code is very messy and unclean, how would I go about by making this cleaner?

Please remember I’m quite a new coder so I appreciate all the help!

``````int time = 500; //500ms delay for the number
int buttonPin = 10; //button pin to display the dice
int count = 0;
int pinArray[] = {2, 3, 4, 5, 6, 7, 8, 9};
void setup() {
for (count=0;count<8;count++) {
pinMode(pinArray[count], OUTPUT);
}
}
void loop() {
int button = 0; // checks if button is pressed
if(button == 1) { //checks if button is pressed
{
LED(); //call LED function
}
}
}
void LED() {
int num = random(1,9); //generate random number
//int LEDon = num + 1;
if (num == 1){
digitalWrite(2, HIGH);
}else if(num == 2){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
}else if(num == 3){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}else if(num == 4){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
}else if(num == 5){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
}else if(num == 6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
}else if(num == 7){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}else if(num == 8){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
}else if(num == 9){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
}
delay(time);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}
``````
``````void LED() {
int num = random(1,9); //generate random number
for (int i=2; i<num+1; i++)
digitalWrite(i, HIGH);
delay(time);
for (int i=2; i<=10; i++)
digitalWrite(i, LOW);
``````

If your pins are not consecutive:

``````  for (int i=0; i<num; i++)
digitalWrite(pinArray[i], HIGH);
delay(time);
for (int i=0; i<8; i++)
digitalWrite(pinArray[i], LOW);
``````

This is how I might have written it:

``````const unsigned long int displayInterval = 500;  //500ms delay for the number

const int buttonPin = 10; //button pin to display the dice

const int pinArray[] = { 2, 3, 4, 5, 6, 7, 8, 9};
const int pinCount = sizeof pinArray / sizeof pinArray[0];

void setup() {
for (int i=0; i<pinCount; i++)
pinMode(pinArray[i], OUTPUT);
}

void loop() {
if(digitalRead(buttonPin)) //checks if button is pressed
LED(); //call LED function
}

void LED() {
int num = random(1,9); //generate random number

for (int i=0; i<num; i++)
digitalWrite(pinArray[i], HIGH);
delay(displayInterval);
for (int i=0; i<pinCount; i++)
digitalWrite(pinArray[i], LOW);
}
``````

If you have any questions about the various design decisions, just ask.