Digital Electronics assignment 2

BY BENNA SU

First part 1.

1)The first mistake is that the set up does not include the Ledpin2 and buttonpin2 so the program will not recognize those two. I add the pinmode for both.

2)When you press the button pin 1 the ledpin2 should be off but the code does not include that and I add it on.

3)Same as for button pin 2, when you press it the led pin 1 should be off and led pin 2 should be on.

4)no “;” after 7 in the first step of int

5)forgot to put the loop of Button pin 2 into the void loop.

Here is the correct code

int ledPin = 8;
int buttonPin = 7;
int ledPin2 = 9;
int buttonPin2 = 10;

//this is a comment
void setup() {
	Serial.begin(9600);
	pinMode(ledPin, OUTPUT);
  	pinMode(buttonPin, INPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(buttonPin2, INPUT);
}

void loop() {
	checkButton1();
    checkButton2();
}

void checkButton1() {
    if(digitalRead(buttonPin) == HIGH) {
    Serial.println("button pressed");    //to see what this does click the "Serial Monitor" button below this window and then Start Simulation
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
    digitalWrite(ledPin2, LOW);
  } else {
    Serial.println("button not pressed"); //see comment above ^^^
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin2, LOW);
  }
}





void checkButton2() {
  if(digitalRead(buttonPin2) == HIGH) {
    Serial.println("button pressed");    //to see what this does click the "Serial Monitor" button below this window and then Start Simulation
    digitalWrite(ledPin2, HIGH);
    delay(1000);
    digitalWrite(ledPin2, LOW);
    delay(1000);
    digitalWrite(ledPin, LOW);
  } else {
    Serial.println("button not pressed"); //see comment above ^^^
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin, LOW);
  }
}

here is the circuit

https://www.tinkercad.com/things/jaq5fHWyaLK

Part 2!

So 3 functions all perform different ways to show the led. For function 1, when all the switch are “High”, which means all switches to the right side, the Led start to shining in the delay of 500 gradually. After the first one shining, the second one starts to shining, so as the 3rd one and the 4th one. For function 2, only each switch can trigger corresponding Led, for example, the first switch only makes the first Led shining, and other switches won’t work while the any other one is working. For function 3, when yu turn on all the switches all the Led go “high” and Low” at the same time with the same delay time.

Part3!!

I tried to replace code much by using blinkLED, but I do not know if it works well.

Here is the code

int switchPin1 = 4;
int switchPin2 = 5;
int switchPin3 = 6;
int switchPin4 = 7;

int ledPin1 = 8;
int ledPin2 = 9;
int ledPin3 = 10;
int ledPin4 = 11;

void setup()
{
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);

  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);
  pinMode(switchPin3, INPUT);
  pinMode(switchPin4, INPUT);
}

void loop() {
  if (digitalRead(switchPin1) == HIGH) {
    blinkLED(500);
  } else {
    blinkLED(500);



  }
  if (digitalRead(switchPin2) == HIGH) {
    blinkLED(500);
  } else {
    blinkLED(500);
  }
  if (digitalRead(switchPin3) == HIGH) {
    blinkLED(500);
  } else {
    blinkLED(500);
  }

  if (digitalRead(switchPin4) == HIGH) {
    blinkLED(500);
  } else {
    blinkLED(500);

  }

}

void blinkLED(int milliseconds) {
  digitalWrite(ledPIN, HIGH);
  delay(milliseconds);
  digitalWrite(ledPin, LOW);
  delay(milliseconds);
}

void function2() {

  if (digitalRead(switchPin1) == HIGH) {
    digitalWrite(ledPin1, HIGH);
    delay(500);
    digitalWrite(ledPin1, LOW);
    delay(500);
  } else if (digitalRead(switchPin2) == HIGH) {
    digitalWrite(ledPin2, HIGH);
    delay(500);
    digitalWrite(ledPin2, LOW);
    delay(500);
  } else if (digitalRead(switchPin3) == HIGH) {
    digitalWrite(ledPin3, HIGH);
    delay(500);
    digitalWrite(ledPin3, LOW);
    delay(500);
  } else if (digitalRead(switchPin4) == HIGH) {
    digitalWrite(ledPin4, HIGH);
    delay(500);
    digitalWrite(ledPin4, LOW);
    delay(500);
  }
}

void function3() {
  if (digitalRead(switchPin1) == HIGH) {
    digitalWrite(ledPin1, HIGH);
  }

  if (digitalRead(switchPin2) == HIGH) {
    digitalWrite(ledPin2, HIGH);
  }

  if (digitalRead(switchPin3) == HIGH) {
    digitalWrite(ledPin3, HIGH);
  }

  if (digitalRead(switchPin4) == HIGH) {
    digitalWrite(ledPin4, HIGH);
  }

  delay(500);

  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);

  delay(500);

}

here is the circuit

https://www.tinkercad.com/things/hon6ZFdW2On

留下评论

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