// defining the pins const int green_led = 3;const int red_led = 5;const int yellow_led = 6;const int buzzer = 4;const int button_status = 12;const int button_fall = 11;const int button_safe = 10;const int potentiometer = A0; int pot_value;int button_fall_state;int button_safe_state;int last_button_fall_state;int buzzer_state;int threshold = 512; int button_status_state = 0; void setup() { // put your setup code here, to run once:Serial.begin(9600);// Initializing digital pins as outputpinMode(green_led, OUTPUT);pinMode(red_led, OUTPUT);pinMode(yellow_led, OUTPUT);pinMode(buzzer, OUTPUT); // Initializing digital pins as inputspinMode(button_status, INPUT);pinMode(button_fall, INPUT);pinMode(button_safe, INPUT);// initializing analog pin as inputpinMode(potentiometer, INPUT); digitalWrite(buzzer, LOW);digitalWrite(yellow_led, LOW); } void loop() { // read current state of the status button button_status_state = digitalRead(button_status); if (button_status_state == HIGH) { digitalWrite (green_led, HIGH); } else { digitalWrite (green_led, LOW);} pot_value = analogRead(potentiometer); if (pot_value > threshold){ digitalWrite(red_led, HIGH);}else { digitalWrite(red_led, LOW);} // fall detection button_fall_state = digitalRead(button_fall);button_safe_state = digitalRead(button_safe); if (button_fall_state == HIGH && last_button_fall_state == LOW){ buzzer_state = HIGH; digitalWrite(buzzer, HIGH); digitalWrite(yellow_led, LOW); delay(100);} if(button_safe == HIGH){ buzzer_state = LOW; digitalWrite(buzzer, LOW); digitalWrite(yellow_led, HIGH); delay(100); } last_button_fall_state = button_fall_state; } I have come up with the above code, for a device with these conditions: 1. green_led is the power indicator, should be switched on by button_status: when button_status is pressed, green_led go ON, to supply power to the rest of the components. The grren_led should only go OFF when the same button_status is pressed again. 2. button_fall, when this button is pressed, the audio alarm from the buzzer is heard. This audio alarm should be silenced by button_safe only. 3. button_safe, when pressed, yellow_led should go ON, and at the same time the buzzer be silenced. The yellow_led should stay ON until the button_status is pressed to swich off the device. 4. potentimeter, a threshold is set, whenever the potentiometer value goes above the set threshhold, the red_led go ON. When potentiometer value is below the set threshold, the red_led go OFF. I need help in improving my code because it does not meet all the requirements above.
// defining the pins
const int green_led = 3;
const int red_led = 5;
const int yellow_led = 6;
const int buzzer = 4;
const int button_status = 12;
const int button_fall = 11;
const int button_safe = 10;
const int potentiometer = A0;
int pot_value;
int button_fall_state;
int button_safe_state;
int last_button_fall_state;
int buzzer_state;
int threshold = 512;
int button_status_state = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// Initializing digital pins as output
pinMode(green_led, OUTPUT);
pinMode(red_led, OUTPUT);
pinMode(yellow_led, OUTPUT);
pinMode(buzzer, OUTPUT);
// Initializing digital pins as inputs
pinMode(button_status, INPUT);
pinMode(button_fall, INPUT);
pinMode(button_safe, INPUT);
// initializing analog pin as input
pinMode(potentiometer, INPUT);
digitalWrite(buzzer, LOW);
digitalWrite(yellow_led, LOW);
}
void loop() {
// read current state of the status button
button_status_state = digitalRead(button_status);
if (button_status_state == HIGH) {
digitalWrite (green_led, HIGH);
}
else {
digitalWrite (green_led, LOW);
}
pot_value = analogRead(potentiometer);
if (pot_value > threshold){
digitalWrite(red_led, HIGH);
}
else {
digitalWrite(red_led, LOW);
}
// fall detection
button_fall_state = digitalRead(button_fall);
button_safe_state = digitalRead(button_safe);
if (button_fall_state == HIGH && last_button_fall_state == LOW){
buzzer_state = HIGH;
digitalWrite(buzzer, HIGH);
digitalWrite(yellow_led, LOW);
delay(100);
}
if(button_safe == HIGH){
buzzer_state = LOW;
digitalWrite(buzzer, LOW);
digitalWrite(yellow_led, HIGH);
delay(100);
}
last_button_fall_state = button_fall_state;
}
I have come up with the above code, for a device with these conditions:
1. green_led is the power indicator, should be switched on by button_status: when button_status is pressed, green_led go ON, to supply power to the rest of the components. The grren_led should only go OFF when the same button_status is pressed again.
2. button_fall, when this button is pressed, the audio alarm from the buzzer is heard. This audio alarm should be silenced by button_safe only.
3. button_safe, when pressed, yellow_led should go ON, and at the same time the buzzer be silenced. The yellow_led should stay ON until the button_status is pressed to swich off the device.
4. potentimeter, a threshold is set, whenever the potentiometer value goes above the set threshhold, the red_led go ON. When potentiometer value is below the set threshold, the red_led go OFF.
I need help in improving my code because it does not meet all the requirements above.
Step by step
Solved in 2 steps