Digital electronics Week 8 assignment

BY BENNA SU

Here is my code!

bool sequence[3][4] = {
  {HIGH, LOW, HIGH, LOW},
  {LOW, HIGH, HIGH, LOW},
  {HIGH, LOW, HIGH, HIGH}

};
int sequenceLedPins[4] = {28, 29, 30, 31};
int currentChannel = 3;
int ButtonPin = 33;
int channelLedPins[3] = {36, 35, 34};
bool buttonState = LOW;
bool lastButtonState = LOW;

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 4; i++) {
    pinMode(sequenceLedPins[i], OUTPUT);
  }
  for (int i = 0; i < 3; i++) {
    pinMode(sequenceLedPins[i], OUTPUT);
  }
  pinMode(ButtonPin, INPUT);
}

void loop() {
  updateLeds();
  checkButton();
}

void checkButton() {
  lastButtonState = buttonState;
  buttonState = digitalRead(ButtonPin);
  if (lastButtonState == LOW and buttonState == HIGH) {
    countUp();
    delay(5);
  } else if (lastButtonState == HIGH and buttonState == LOW) {
    delay(5);
  }
}

void countUp() {
  currentChannel++;
  if (currentChannel >= 3) {
    currentChannel = 0;
  }
}


void updateLeds() {
  updateSequenceLeds();
  updateChannelLeds();
}

void updateSequenceLeds() {
  for (int i = 0; i < 4; i++) {
    digitalWrite(sequenceLedPins[i], sequence[currentChannel][i]);
  }

}

void updateChannelLeds() {
  for (int i = 0; i < 3; i++) {
    if (i == currentChannel) {
      digitalWrite(channelLedPins[i], HIGH);
    } else {
      digitalWrite(channelLedPins[i], LOW);
    }
  }
}


Here is the vedio!

留下评论

通过 WordPress.com 设计一个这样的站点
从这里开始