Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this is an arduino question and my problem is with a loop function. i will inclu

ID: 639408 • Letter: T

Question

this is an arduino question and my problem is with a loop function. i will include the code i have done so far and my question is how to get the S.O.S portion to repeat till the button is pressed again

const int switchPin = 2; // The switch monitors pin 2

const int led1 = 13; // The LED is on pin 13
const int led2 = 12;
const int led3 = 11;
int buttonState; // Remembers the state of the button
int lightState = 0; // Remembers the state of the LED (starts off)

void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(led1, OUTPUT); // The LED pin is an output
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
Serial.begin(9600); // Set up serial communication at 9600 bps
buttonState = digitalRead(switchPin); // read the initial state
}
void loop() {
int val = digitalRead(switchPin); // read input value and store it in val
delay(10); // wait 10 ms to read the switch again
int val2 = digitalRead(switchPin); // second read of the switch
if (val == val2) { // ensure that it was really a press
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button has been released
if (lightState == 0) { // check if the LED is OFF
lightState = 1; // sets the state of the light to ON
digitalWrite(led1, HIGH); // turn the LED ON
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
Serial.println("Button just pressed, light on");
}
else if(buttonState == 0) { // the button it *NOT* pressed
lightState = 0; // sets the state if the LED is OFF
digitalWrite(led1, LOW); // turn the LED OFF
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
Serial.println("Button just pressed, light off");
}
else {
  
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(150);
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
delay(150); // wait for a second
digitalWrite(led1, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(150); // wait for a second
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
delay(150); // wait for a second
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(150);
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
delay(150); // wait for a second
digitalWrite(led1, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(150);   
  
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
delay(450); // wait for a second
digitalWrite(led1, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(150); // wait for a second
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
delay(450); // wait for a second
digitalWrite(led1, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(150); // wait for a second
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
delay(450); // wait for a second
digitalWrite(led1, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(150); // wait for a second
  
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
delay(150); // wait for a second
digitalWrite(led1, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(150);
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
delay(150); // wait for a second
digitalWrite(led1, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(150);
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
delay(150); // wait for a second
digitalWrite(led1, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(150);
  
}
}
}
}
buttonState = val; // save the new state in our variable
}
  

Explanation / Answer

int led = 13; //yj Integer representing dit (morse ' . ') int dit = 500; //yj Integer representing dah (morse ' - '>) //yj Testing a multiple of another constant integer to make the timing scalable. int dah = dit*3; int blankTime = 100; // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(dit); // wait digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(blankTime); // wait digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(dit); // wait digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(blankTime); // wait digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(dit); // wait digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(blankTime); // wait digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(dah); // wait digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(blankTime); // wait digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(dah); // wait digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(blankTime); // wait digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(dah); // wait digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(blankTime); // wait digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(dit); // wait digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(blankTime); // wait digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(dit); // wait digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(blankTime); // wait digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(dit); // wait digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(blankTime); // wait } //TODO: //Loop counter implementation for multiples (SOS = 3 dit, 3 dah, 3 dit.)