Hello internet,
I am an old coder learning the new ways for arduino and it is coming back to me quickly (albeit with growing pains). My project is designed to replace the ignition on my Yamaha 1300CC motorcycle as the key ignition was destroyed when someone tried to steal it with a screwdriver.
I have already proven the wiring for the bike is correct as I had a 433 MHz wireless transmitter in place for conceptual purposes only. I am now upgrading to the arduino since the 433Mhz fried (not meant to run 24 hours a day 7 days a week and blew after 4 months)
Below is my code, if I remove the void Credentials() subroutine the program will compile correctly. However I don’t want to allow terminal commands to be sent from a laptop unless the Logon flag is tripped so no one can plug in and start the bike, instead only seeing status updates from Bluetooth voice control(I haven’t built in the override commands I had planned until this part works).
The error I get reads: Exit status 1
a function-definition is not allowed here before ‘{’ token
and is flagged from the void terminalcmd() during its call for the credential check. I have checked over and over and cant find my fault… help? ( I have removed the username and password temporarily since this is on internet and tin-foil hat theories LOL)
/* Bluetooth Control for Motorcycle
Pin 0 not used so hardware serial available
Pin 1 not used so hardware serial available
Pin 8 used for Software serial RX pin for Bluetooth to send commands
pin 9 used for Software serial TX pin for Bluetooth to send commands
Pin 2 used for main power relay logic
Pin 3 used for Stereo power relay logic
Pin 4 used for Starter Relay logic
*/
//Software serial
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(8, 9); //BTSerial variable will hold incoming commands from BT Module
//Defines and includables
String Voice; //Define voice string to be used later
String Terminal; //Define terminal string to be used later
boolean Main = false; //Set main power status logic to 0 showing off
boolean Stereo = false; //Set stereo power status logic to 0 showing off
boolean Logon = false; //Set logon logic to 0 for false
const int Relay1 = 2; //Set Relay1 to be on pin 4 for logic
const int Relay2 = 3; //Set Relay2 to be on pin 5 for logic
const int Relay3 = 4; //Set Relay3 to be on pin 6 for logic
//Voice Subroutines
void Voicecmd(){
if(Voice == "*shut down") {ShutdownBT(); return;} //Turn off all relays (Call Function)
if(Voice == "*warm up") {WarmupBT(); return;} //Warmup bike (Call Function)
if(Voice == "*startup") {StartupBT(); return;} //Start the bike (Call Function)
if(Voice == "*stereo") {StereoBT(); return;} //Toggle Stereo (Call Function)
}
void ShutdownBT(){
Serial.println("Shutdown sequence started");
Serial.println("Shutting down Main Power");
digitalWrite(Relay1, HIGH);
Main = false; //Set main power variable off
Serial.println("Shutting down Stereo");
digitalWrite(Relay2, HIGH);
Stereo = false; //Set Stereo variable to off
Serial.println("Ensuring Starter Shutdown");
digitalWrite(Relay3, HIGH);
Serial.println("Shut down complete");
Serial.println("");
// Statusterm();
Serial.println("");
}
void WarmupBT(){
Serial.println("Warm-up sequence triggered");
Serial.println("Main power on");
digitalWrite(Relay1, LOW); //Turn on main power
Main = true; //Set main variable to on
delay(1000);
Serial.println("Starting engine");
digitalWrite(Relay3, LOW); //Turn on starter
Serial.println("Waiting a second to ensure engine started");
delay(1500); //Wait 1.5 seconds
Serial.println("Stopping starter");
digitalWrite(Relay3, HIGH); //Turn off Starter
Serial.println("Warmup complete");
Serial.println("");
}
void StartupBT(){
Serial.println("Start sequence triggered");
Serial.println("Main power on");
digitalWrite(Relay1, LOW); //Turn on main power
Main = true; //Set main variable to on
Serial.println("Trigger Stereo Subroutine");
Serial.println("");
delay(100);
StereoBT();
delay(1000);
Serial.println("Starting engine");
digitalWrite(Relay3, LOW); //Turn on starter
Serial.println("Waiting a second to ensure engine started");
delay(1500); //Wait 1.5 seconds
Serial.println("Stopping starter");
digitalWrite(Relay3, HIGH); //Turn off Starter
Serial.println("Startup complete");
Serial.println("");
}
void StereoBT(){
Serial.println("Stereo Command triggered");
Serial.println("Checking if stereo is on or off");
if (Stereo == false) { //Look if stereo is off
Serial.println("Turning stereo on");
digitalWrite(Relay2, LOW); //Turn on stereo
}
else { //If stereo is on
Serial.println("Turning Stereo off");
digitalWrite(Relay2, HIGH); //Turn off stereo
}
Serial.print("Toggling Stereo variable to ");
Stereo = !Stereo;
if(Stereo == HIGH) {Serial.println("On");} else Serial.println("Off");
Serial.println("Stereo command complete");
Serial.println("");
}
void Credentials(){
if(Terminal == "HAHA") {
Serial.println("5 seconds to enter password:");
delay(5000);
Terminal = Serial.read();
if(Terminal == "LOL") {
Logon = true;
Serial.println("Logged on");
}
else {Serial.print("Not logged in, please enter username");}
}
void Terminalcmd(){
if (Logon == false) {Credentials();} //Check for credentials
if (Terminal == "status") {Statusterm();} // Print status of arduino
}
void Statusterm(){
Serial.print("Main power: "); if(Main == HIGH) {Serial.println("On");} else Serial.println("Off");
Serial.print("Stereo: "); if(Stereo == HIGH) {Serial.println("On");} else Serial.println("Off");
Serial.print("Starter: "); if(Relay3 == LOW) {Serial.println("On");} else Serial.println("Off");
Serial.println("");
}
void(* resetFunc) (void) = 0; //Allow terminal to reset board if failure occurs
//Board Setup
void setup() {
BTSerial.begin(9600); //Begin Software Serial communications
Serial.begin(9600); //Begin Normal Serial communications for troubleshooting
pinMode(Relay1, OUTPUT); digitalWrite(Relay1, HIGH); //Set Main power relay pin to output and toggle it high to leave relay off
pinMode(Relay2, OUTPUT); digitalWrite(Relay2, HIGH); //Set Stereo relay pin to output and toggle it high to leave relay off
pinMode(Relay3, OUTPUT); digitalWrite(Relay3, HIGH); //Set Starter power relay pin to output and toggle it high to leave relay off
}
void loop() {
//Terminal side
if(Serial.available()){ //Check if terminal active
delay(10); //Stability
Terminal = Serial.read(); //Put the terminal command into terminal variable
if(Terminal.length() > 0) {Terminalcmd();}}; //if Terminal variable has something in it, goto Terminalcmd sub routine
//Bluetooth side
if(BTSerial.available()){ //Check if Bluetooth has sent a command
delay(10); //Stability
char c = BTSerial.read(); //Read Bluetooth command
if (c =='#') {Voice += c;}; //Look if it ended with # and move into Voice Variable if it did
if (Voice.length() > 0) {Voicecmd();}}; //If Voice variable has something in it, goto Voicecmd sub routine
Voice=""; //Reset Voice variable before cycling loop
Terminal=""; //Reset Terminal variable before cycling loop
}