Now b*****ing leds with arrays…..!

Well, I have at leat learnt how to make arrays well, so that is good, but the actual functionality seems to be exactly the same as the first rough version! Any comments welcome.

 

// select the input pin for the potentiometer
int potPin = A5;
// variable to store the value coming from the pot
int potVal = 0;
// variable for tempo rate
int tempo;
// on-board LED blinks each time through the main loop
int loopLED = 13;
int loopLEDState = LOW;

// Tempo LEDs on analog pins
const int TempoLed1 =  A0;      
const int TempoLed2 =  A1;
const int TempoLed3 =  A2;
const int TempoLed4 =  A3;

// LED indicates the button toggle state
const int buttonLED1 = 8;
const int buttonLED2 =  9;
const int buttonLED3 =  10;
const int buttonLED4 =  11;

// pushbutton to toggle the LED
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;

// variables for reading the led & pushbutton status
int buttonState1 = LOW;         
int buttonState2 = LOW;
int buttonState3 = LOW;
int buttonState4 = LOW;

// variable for previous button state
int prevButtonState1 = LOW;
int prevButtonState2 = LOW;
int prevButtonState3 = LOW;
int prevButtonState4 = LOW;

// initial state and saved last state for button LED
int buttonLEDState1 = LOW;
int buttonLEDState2 = LOW;
int buttonLEDState3 = LOW;
int buttonLEDState4 = LOW;
int prevButtonLEDState1;
int prevButtonLEDState2;
int prevButtonLEDState3;
int prevButtonLEDState4;

// initial state and saved last state for tempo LED
int tempoLEDState1 = LOW;
int tempoLEDState2 = LOW;
int tempoLEDState3 = LOW;
int tempoLEDState4 = LOW;

// will store last time LED was updated
long previousMillis = 0;

// array to store the button toggle states
int buttonStateArray[4] = {
  buttonState1, buttonState2, 
  buttonState3, buttonState4};
  
// array to store previous button states
int prevButtonStateArray[4] = {
  prevButtonState1, prevButtonState2,
  prevButtonState3, prevButtonState4};
  
// array to store button pin numbers  
int buttonPinArray[4] = {
  buttonPin1, buttonPin2,
  buttonPin3, buttonPin4};
  
// array to store button indicator LEDs 
int buttonLEDArray[4] = {
    buttonLED1, buttonLED2,
    buttonLED3, buttonLED4};
    
// array to store button indicator LED States 
int buttonLEDStateArray[4] = {
    buttonLEDState1, buttonLEDState2,
    buttonLEDState3, buttonLEDState4};  
  
// array to store Previous button indicator LED  States 
int prevButtonLEDStateArray[4] = {
    prevButtonLEDState1, prevButtonLEDState2,
    prevButtonLEDState3, prevButtonLEDState4};      
    
// array to store tempo LED states
int tempoLEDStateArray[4] = {
  tempoLEDState1, tempoLEDState2,
  tempoLEDState3, tempoLEDState4};

// array to store the tempo LEDs
int tempoLEDArray[4] = {
  TempoLed1, TempoLed2, 
  TempoLed3, TempoLed4};

void setup() {
//  Serial.begin(9600);
 // declare the pins as either INPUT or OUTPUT:
 pinMode(loopLED, OUTPUT);
 for(int i=0;i<4;i++){
 pinMode(tempoLEDArray[i], OUTPUT);
 pinMode(buttonLEDArray[i], OUTPUT);
 pinMode(buttonPinArray[i], INPUT);
 digitalWrite(buttonPinArray[i], LOW);
 }

}

void loop() {
for (int i=0;i<4;i++){ 
 // read the value from the potentiometer
 // (every time we go through the loop)
 potVal = analogRead(potPin);
 // map the min and max vals from the pot to the tempo rate
 tempo = map(potVal, 1023, 0, 500, 1);
 // get the current time since arduino started running the program
 unsigned long currentMillis = millis();
 // is the difference now and the last time we blinked the LED bigger
 // than the potentiometer value?
 if(currentMillis - previousMillis > tempo) {
  // save the last time we blinked the LED
  previousMillis = currentMillis;

    // if the loopLED is off turn it on and vice-versa:
    if (loopLEDState == LOW){
      loopLEDState = HIGH;}
    else{
      loopLEDState = LOW;}
      // blink the onboard looper LED
      digitalWrite(loopLED, loopLEDState);
 }
 
 
 // check if button [i] is pressed
   buttonStateArray[i] = digitalRead(buttonPinArray[i]);

 // button [i]
 if ((buttonStateArray[i] == 0) && (buttonStateArray[i] != prevButtonStateArray[i])) {
  buttonLEDStateArray[i] = (buttonLEDStateArray[i] == LOW) ? HIGH : LOW;
  digitalWrite(buttonLEDArray[i], buttonLEDStateArray[i]);
  }
  prevButtonLEDStateArray[i] = buttonLEDStateArray[i]; 
 // set a new previous button state
 prevButtonStateArray[i] = buttonStateArray[i];
 
 // turn off all tempo LEDs 
 for (int c=0;c<4;c++){
   digitalWrite(tempoLEDArray[c], LOW);
 }
 // play the tone
 if (buttonLEDStateArray[i] == HIGH){tone(7,300,tempo);}
 // blink the correct tempo led
 digitalWrite(tempoLEDArray[i], HIGH);
 delay(tempo);

 
}
}


3 thoughts on “Now b*****ing leds with arrays…..!

  1. Code does seem somewhat complicated and so far I can’t see how it would make it more useful. Of course you got rid of delays() which might be good. Can you tell what are the problems right now? Is the problem still in the lag between button press and it’s activation?
    I would suggest that you attach the mp3 board onto your rig and start to work from there. Let’s see how that works for now.

  2. Arrays! Well done. 🙂

    Problem is that code is checking button_state only of the button where for-loop currently is. So if count is in 1, then only button__state1 is checked. Without testing with hardware, this I assume to be problem.

    For fixing this you need to write functionatily inside for-loop which checks states of all four buttons, not depending of for-loops counter state.

    It can be easier to write separate function for this and use count of “button_state for-loop” as argument for the function call. So something like this:

    void loop ()

    for loop
    ….

    for (int i=0;i<4;i++){
    check_state_button(i)
    }

    ….
    // end of "big" for loop
    // end of void loop()

    // your new function
    state_button(int i) {

    // here your button_state check code

    // doesn't return anything, only check state of the button(i)
    }

Comments are closed.