int delayLength = 200;
int notes[4] = {50, 55, 60, 65};
int LEDPins[4] = { 7, 8, 9, 10 };

int currentStep = 0;

unsigned long lastStepTime;

void setup() {
  Serial.begin(9600);

  //Set pinMode for outputs (Pins 7,8,9,10)
  
  for (int i = 0; i < sizeof(LEDPins); i++) {

    pinMode(LEDPins[i], OUTPUT); //+7 offsets the pin number to start at 7
    
  }

  //Set inputs' pinMode
  for (int i = 0; i < 2; i++) {

    pinMode(i+37, INPUT); //Same offset but for 37 for setting inputs
    
  } 

}

void loop() {

  //Get delayLength by reading from pin 13 and mapping it between 100 and 1000
  delayLength = map(analogRead(13), 0, 1023, 100, 1000);

  if (digitalRead(37) == HIGH) {
    stepForwards();
  } else {
    stepBackwards();
  }

}

void stepForwards() {
  
  if (millis() > lastStepTime + delayLength) {

    lastStepTime = millis();
    
    digitalWrite(LEDPins[currentStep], LOW);
    
    currentStep++;
    if (currentStep >= 4) {
      currentStep = 0;
    }

    digitalWrite(LEDPins[currentStep], HIGH);
    
  }
  
}

void stepBackwards() {
  
  if (millis() > lastStepTime + delayLength) {

    lastStepTime = millis();
    
    digitalWrite(LEDPins[currentStep], LOW);
    
    currentStep--;
    if (currentStep < 0) {
      currentStep = 3;
    }

    digitalWrite(LEDPins[currentStep], HIGH);
    
  }
  
}



0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.