Question

Microcontroller programming exercises by practice on Arduino: Write a program that lights a green LED attached...

Microcontroller programming exercises by practice on Arduino:

Write a program that lights a green LED attached to pin 3. The green LED should turn off after a button attached to pin4 has been pushed and released 3 times. Each time the button is pushed it will turn on a red LED attached to pin 7 and turn off a yellow LED attached to pin 9. When the button is not pushed the red LED will be off and the yellow LED will turn on. Assume the button is wired active low.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
/* sketch 1 
turn on a LED when the button is pressed
turn it off when the button is not pressed (or released)
*/
int pinButton = 4; //the pin where we connect the button
int LED = 3; //the pin we connect the LED

void setup()
 {
  pinMode(pinButton, INPUT); //set the button pin as INPUT
  pinMode(LED, OUTPUT); //set the LED pin as OUTPUT
}

void loop() 
{
  int stateButton = digitalRead(pinButton); //read the state of the button
  if(stateButton == 1) { //if is pressed
     digitalWrite(LED, HIGH); //write 1 or HIGH to led pin
  } else { //if not pressed
     digitalWrite(LED, LOW);  //write 0 or low to led pin
  }
}
/* sketch 2 
turn on a LED when the button is pressed and let it on when the button is released
*/
int pinButton = 4; //the pin where we connect the button
int LED = 3; //the pin we connect the LED

void setup() {
  pinMode(pinButton, INPUT); //set the button pin as INPUT
  pinMode(LED, OUTPUT); //set the LED pin as OUTPUT
}

void loop() {
  int stateButton = digitalRead(pinButton); //read the state of the button
  if(stateButton == 1) { //if is pressed
     digitalWrite(LED, HIGH); //write 1 or HIGH to led pin
  }
}
/* sketch 3 
turn on a LED when the button is pressed and let it on 
until the button is pressed again
*/
int LED = 7;
int stateLED = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 200;

int LED = 9; 
int stateLED = LOW; 
int stateButton; 
int previous = LOW; 
long time = 0; 
long debounce = 200;

void setup() {
  pinMode(pinButton, INPUT);
  pinMode(LED, OUTPUT);
}

void loop() {
  stateButton = digitalRead(pinButton);  
  if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
    if(stateLED == HIGH){
      stateLED = LOW; 
    } else {
       stateLED = HIGH; 
    }
    time = millis();
  }
  digitalWrite(LED, stateLED);
  previous == stateButton;
}
Add a comment
Know the answer?
Add Answer to:
Microcontroller programming exercises by practice on Arduino: Write a program that lights a green LED attached...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write an Arduino program to continuously read the value of pin A1. The program lights up...

    Write an Arduino program to continuously read the value of pin A1. The program lights up an LED connected to pin 5 if the value of A1 is greater than 300. If the value of A1 is less than 50, the program lights up an LED connected to pin 10.

  • Connect NodeMCU to Arduino, write codes on Arduino to make the LED on NodeMCU do the...

    Connect NodeMCU to Arduino, write codes on Arduino to make the LED on NodeMCU do the followings: Using your NodeMCU, write a program to blink the onboard LED in a particular pattern: First, turn the LED on for one second, then off for one second. Then, blink the LED five times – on for 200ms, and off for 200ms. (Use a for loop.) Finally, turn the LED on for half a second, then off for half a second. Loop this...

  • C CODE LANGUAGE. MSP430. #include <msp430fr6989.h> #define redLED BIT0 // Red LED at P1.0 #define greenLED...

    C CODE LANGUAGE. MSP430. #include <msp430fr6989.h> #define redLED BIT0 // Red LED at P1.0 #define greenLED BIT7 // Green LED at P9.7 #define BUT1 BIT1 // Button S1 at P1.1 #define BUT2 BIT2 // Button S2 at P1.2 void main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop the Watchdog timer PM5CTL0 &= ~LOCKLPM5; // Enable the GPIO pins // Configure and initialize LEDs P1DIR |= redLED; // Direct pin as output P9DIR |= greenLED; // Direct pin as output...

  • Start ARDUINO SOFTWARE and Write a program that checks the voltage value in the circuit of...

    Start ARDUINO SOFTWARE and Write a program that checks the voltage value in the circuit of three resistors. i. If the voltage is higher than 4V, turn ON red LED ii. If the voltage is higher than 2V and less than 3V, turn ON yellow LED iii. If the voltage is less than 2V turn ON green LED. Note that analog input is used in this example.

  • I have a program for Arduino written such that when I hold a button down, it...

    I have a program for Arduino written such that when I hold a button down, it will turn an LED off. I want to be able to print to the serial monitor how long the button was pressed for after I let go of the button. Does anybody have any ideas? Below is the code I have written thus far: Text of Code: #define LED RED_LED #define buttonPIN PUSH2 const int buttonPin = PUSH2; // the number of the pushbutton...

  • Write a program for PIC18F4580 microcontroller to blink the LED at the pin RC0 with the...

    Write a program for PIC18F4580 microcontroller to blink the LED at the pin RC0 with the frequency of 8Hz using the Timer1 overflow interrupt and the LED at the pin RC7 with the frequency of 0.8Hz using the Timer0 overflow interrupt concurrently. (Assume that Fosc = 8MHz.) (1) Choose the prescaler and the initial value for TMR1, respectively. (Round off the numbers.) (2) Choose the prescaler and the initial value for TMR0, respectively. (Round off the numbers.) (3) Write the...

  • Write an Arduino code to read the current temperature using the LM35 temperature sensor. The code...

    Write an Arduino code to read the current temperature using the LM35 temperature sensor. The code should include the following if the temperature is more than 45 "C: • Turn ON red LED, and after 1 second turn ON the yellow LED, and after another 2 seconds tum On the Green LED. One second later the three LEDS should start blinking (all LEDs OFF for 0.5 seconds, then all LEDS ON for 1 minute), and turn OFF all LEDs after...

  • 3. Write an Arduino program to do the following function: When a switch connected to digital...

    3. Write an Arduino program to do the following function: When a switch connected to digital pin 4 is pressed, three LEDs connected to digital pins 5, 7, and 9 will blink ON and OFF sequentially (i.e. one after the other) with half a second delay between when an LED turns ON and turns OFF.

  • Problem 10 Use the following circuit to answer the next four questions. Recall that the resistor...

    Problem 10 Use the following circuit to answer the next four questions. Recall that the resistor enclosed in a circle with two arrows is a photoresistor/photocell. The photoresistor will be used to control the circuit's behavior. If the room is dark, the red LED will illuminate. In moderate light, the yellow LED will illuminate. In bright light, the green LED will illuminate. The reading from the photoresistor at pin AO is assigned to the integer variable, "photo_value. ☆ ☆ Red...

  • Lab 1-Lab Portion Part I Copy the Blink program from Figure 1-13 (page 17) in the textbook and run it on your Arduino. After verifying that the Blink program works, modify the program to make the...

    Lab 1-Lab Portion Part I Copy the Blink program from Figure 1-13 (page 17) in the textbook and run it on your Arduino. After verifying that the Blink program works, modify the program to make the on-board LED attached to pin #13 blink out "SOS" in Morse code. Use a 2 750 millisecond for long pulses. 50 millisecond for short pulses and a Part 2, Write a program that uses two pushbuttons to increase or decrease the brightness of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT