118k views
4 votes
Light sequences You are tasked with the implementation of a light sequence to commence when a switch is used (Toggle switches). This is to be written in C code and have a structure diagram or a state machine diagram in the report.

Each of these switches must have a different light sequence linked to them. And must also continuously run until the switch is off. Until the switch is turned off, the other switches are ignored.

The first sequence must start from LEDO and go through all the LEDS to LED7, at an interval of 1/4 of a second. I,e. LEDO on, 1/4 second delay, LEDO off and LED1 on, 1/4 second delay, LED1 off and LED2 on...


The second sequence will start from the middle of the LEDS (LED3 and LED4) to each end of the LED line (LEDO and LED7).

The third sequence has been left to you to decide. (You may use either a state machine or if statements)


Use the Table below as a reference for your inputs and outputs in your code. A structure diagram or a state machine diagram must be included in the report.



Inputs
PINA0 Sequence1
PINA1 Sequence 2
PINA2 Sequence 3


Outputs
PORTC LED Lights

User Leeish
by
7.8k points

1 Answer

6 votes

Final answer:

To implement the light sequences with toggle switches in C code, you can use either a state machine or if statements. The code snippet provided demonstrates using if statements to control the LEDs based on the switch inputs, and the state machine diagram visually represents the different states and transitions.

Step-by-step explanation:

To implement the light sequences with toggle switches in C code, you can use either a state machine or if statements. Here's an example using if statements:

#include <avr/io.h>
#include <util/delay.h>

int main(void) {
DDRD = 0xFF; // Set PORTD as OUTPUT
PORTD = 0x00; // Initialize PORTD as LOW

while (1) {
if (PINA & (1 << PINA0)) { // If Switch 1 is pressed
for (int i = 0; i <= 7; i++) {
PORTD = (1 << i); // Turn ON LED
_delay_ms(250); // Delay for 1/4 second
PORTD = 0x00; // Turn OFF LED
}
}
// ... Implement other sequences for Switch 2 and Switch 3
}
return 0;
}

This code snippet shows how to control the LEDs connected to PORTD based on the state of the toggle switches connected to PINA. It uses bit shifting to select the appropriate LED and a delay function to control the timing between LED changes.

A state machine diagram visually represents the different states and transitions of a system. In this case, we can represent the light sequences as states and the switch inputs as transitions. Here's an example state machine diagram:

User Piroot
by
8.1k points