Hey All- So I started teaching myself the arduino. I'm new to both the physics side and the code side, but I was able to create my own problem! (And solve it! Yay.)
I wanted to create an arduino that can perform sound signals used while boating and sailing in the fog. Depending on if one is using a ship's motor, is under sail, moving or anchored there are different sound patterns (ie. 1 long blast, 2 short blasts, delay for two minutes, 1 long blast, 2 short blasts...) Link to COLREGS Sound Patterns
I wanted to be able to break out of a pattern to step into the next one without having to wait for the code to finish playing out one signal.
Like I say I did get a test to work. However two things:
•I'm sure the code is inefficient and would love to know what I'm doing poorly
•Figuring out where to begin was kind of daunting
It took me quite a bit of time to figure out how to start. After a few times trying to think it through with arrays, I decided to start sketching out the workflow Draw.io Wireframing which led me to a switch case solution and a slew of functions, one each for each signal pattern.
I still think there would be a way to use one function and send it parameters such as (dash, dash, 1 minute) or (dash,dot,dot,2 minutes)
Anyway here is my solution:https://circuits.io/circuits/2545224-conav-foghorn
Thanks for your help and insight!
const int ledPin = 13; // will be used for testing button
const int hornPin = 6; // horn out (buzzer)
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
const int buttonPin=2; //digital pin from button
const int powerPin=11;
const int movingPin=10;
const int anchorPin=9;
// the setup routine runs once when you press reset:
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(hornPin, OUTPUT);
pinMode(powerPin, OUTPUT);
pinMode(movingPin, OUTPUT);
pinMode(anchorPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// read the pushbutton input pin:
readButton();
switch (buttonPushCounter)
{
case 0:
digitalWrite(powerPin,LOW);
digitalWrite(movingPin,LOW);
digitalWrite(anchorPin,LOW);
break;
case 1:
digitalWrite(powerPin,HIGH);
digitalWrite(movingPin,HIGH);
digitalWrite(anchorPin,LOW);
powerMake();
break;
case 2:
digitalWrite(powerPin,HIGH);
digitalWrite(movingPin,LOW);
digitalWrite(anchorPin,LOW);
powerStopped();
break;
case 3:
digitalWrite(powerPin,LOW);
digitalWrite(movingPin,HIGH);
digitalWrite(anchorPin,LOW);
sailingVessel();
break;
case 4:
digitalWrite(powerPin,LOW);
digitalWrite(movingPin,LOW);
digitalWrite(anchorPin,HIGH);
vesselAnchored();
break;
case 5:
digitalWrite(powerPin,HIGH);
digitalWrite(movingPin,HIGH);
digitalWrite(anchorPin,LOW);
break;
default:
digitalWrite(powerPin,HIGH);
digitalWrite(movingPin,HIGH);
digitalWrite(anchorPin,HIGH);
}
}
////////////////////////////////////functions
void readButton(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
buttonPushCounter++;
digitalWrite(ledPin,HIGH);
delay(500); //added to prevent double tapping
} else {
digitalWrite(ledPin,LOW);
delay(1);
}
if (buttonPushCounter >=5) buttonPushCounter=0; //reset the counter
}
void powerMake()
{ // read the pushbutton input pin
readButton();
while (buttonPushCounter == 1)
{
for (int i=0; i<=30 && buttonPushCounter == 1 ; i++)
{ //pause for 3 secs before first note
noTone(hornPin);
delay(100);
readButton();
if (buttonState == HIGH) i=111;
}
for (int i=0; i<=50 && buttonPushCounter == 1 ; i++)
{ // play 6 seconds while checking button
tone(hornPin,200);
delay(100);
readButton();
if (buttonState == HIGH) i=51;
}
for (int i=0; i<=90 && buttonPushCounter == 1 ; i++)
{ //pause for 2 minutes (or 9 secs) this is the length of time between signals
noTone(hornPin);
delay(100);
readButton();
if (buttonState == HIGH) i=111;
}
noTone(hornPin);
}
}
void powerStopped()
{ // read the pushbutton input pin
readButton();
while (buttonPushCounter == 2)
{
for (int i=0; i<=30 && buttonPushCounter == 2 ; i++)
{ //pause for 3 secs before playing note
noTone(hornPin);
delay(100);
readButton();
if (buttonState == HIGH) i=111;
}
for (int i=0; i<=50 && buttonPushCounter == 2 ; i++)
{ // play 6 seconds while checking button
tone(hornPin,200);
delay(100);
readButton();
if (buttonState == HIGH) i=51;
}
for (int i=0; i<=8 && buttonPushCounter == 2 ; i++)
{ //pause < 1 sec unless button pushed
noTone(hornPin);
delay(100);
readButton();
if (buttonState == HIGH) i=10;
}
for (int i=0; i<=50 && buttonPushCounter == 2 ; i++)
{ //play one Long
tone(hornPin,200);
delay(100);
readButton();
if (buttonState == HIGH) i=51;
}
for (int i=0; i<=90 && buttonPushCounter == 2 ; i++)
{ //pause for 2 minutes (or testing 9 secs)
noTone(hornPin);
delay(100);
readButton();
if (buttonState == HIGH) i=111;
}
noTone(hornPin); //in the result of button pushed, this cancels the horn.
}
}
void sailingVessel()
{ // read the pushbutton input pin
readButton();
while (buttonPushCounter == 3)
{
for (int i=0; i<=30 && buttonPushCounter == 3 ; i++)
{ //pause for 3 secs
noTone(hornPin);
delay(100);
readButton();
if (buttonState == HIGH) i=111;
}
for (int i=0; i<=50 && buttonPushCounter == 3 ; i++)
{ // play 6 seconds while checking button
tone(hornPin,200);
delay(100);
readButton();
if (buttonState == HIGH) i=51;
}
for (int i=0; i<=8 && buttonPushCounter == 3 ; i++)
{ //pause < 1 sec unless button pushed
noTone(hornPin);
delay(100);
readButton();
if (buttonState == HIGH) i=10;
}
for (int i=0; i<=8 && buttonPushCounter == 3 ; i++)
{ //play one short
tone(hornPin,200);
delay(100);
readButton();
if (buttonState == HIGH) i=11;
}
for (int i=0; i<=8 && buttonPushCounter == 3 ; i++)
{ //pause < 1 sec unless button pushed
noTone(hornPin);
delay(100);
readButton();
if (buttonState == HIGH) i=10;
}
for (int i=0; i<=8 && buttonPushCounter == 3 ; i++)
{ //play one short
tone(hornPin,200);
delay(100);
readButton();
if (buttonState == HIGH) i=11;
}
for (int i=0; i<=90 && buttonPushCounter == 3 ; i++)
{ //pause for 2 minutes (or 9 secs)
noTone(hornPin);
delay(100);
readButton();
if (buttonState == HIGH) i=111;
}
noTone(hornPin);
}
}
void vesselAnchored()
{ // read the pushbutton input pin
readButton();
while (buttonPushCounter == 4)
{
for (int i=0; i<=30 && buttonPushCounter == 4 ; i++)
{ //pause for 1 minutes (or 3 secs)
noTone(hornPin);
delay(100);
readButton();
if (buttonState == HIGH) i=51;
}
for (int i=0; i<=50 && buttonPushCounter == 4 ; i++)
{ // ring BELL 5 seconds (while checking buttonread)
// ideally instead of a piezo at high frequency this would rap a bell
tone(hornPin,850);
delay(70);
noTone(hornPin);
delay(24);
readButton();
if (buttonState == HIGH) i=51;
}
for (int i=0; i<=100 && buttonPushCounter == 4 ; i++)
{ //pause no sound for 1 minutes (or 10 secs)
noTone(hornPin);
delay(100);
readButton();
if (buttonState == HIGH) i=51;
}
noTone(hornPin);
}
}