Created
June 24, 2012 22:31
-
-
Save mactabish/2985292 to your computer and use it in GitHub Desktop.
Wireless Management System for Shooting Competitions - Arduino Shooter Transmitter Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <EEPROM.h> | |
#include <NewSoftSerial.h> | |
NewSoftSerial mySerial(2,3); | |
String readString = ""; | |
String holdString = ""; | |
String id_of_sensor = "0"; | |
String transmit_message = "<S%>"; | |
String shooter_on = "<S%*>"; | |
String shooter_off = "<S%!>"; | |
String ask_for_id_command = "<C?>"; | |
String switch_id_command = "<C*>"; | |
void refactor() | |
{ | |
int id = id_of_sensor.toInt(); | |
EEPROM.write(0, id); | |
transmit_message = transmit_message.replace("%", id_of_sensor); | |
shooter_on = shooter_on.replace("%", id_of_sensor); | |
shooter_off = shooter_off.replace("%", id_of_sensor); | |
} | |
void set_commands() | |
{ | |
int x = EEPROM.read(0); | |
if (x == 0) | |
{ | |
} | |
else | |
{ | |
id_of_sensor = x; | |
refactor(); | |
} | |
} | |
void config_mode(int x) | |
{ | |
if (x == 1) | |
Serial.print(id_of_sensor); | |
else if (x == 2) | |
{ | |
int i = 0; | |
while (i == 0) | |
{ | |
if (Serial.available() > 0) | |
{ | |
id_of_sensor = Serial.read() - 48; | |
refactor(); | |
Serial.print(id_of_sensor); | |
i = 1; | |
} | |
} | |
} | |
} | |
void processString() | |
{ | |
if (holdString == shooter_on) | |
sound_sensor(1); | |
else if (holdString == shooter_off) | |
{ | |
} | |
else if (holdString == ask_for_id_command) | |
{ | |
config_mode(1); | |
holdString = ""; | |
} | |
else if (holdString == switch_id_command) | |
{ | |
config_mode(2); | |
holdString = ""; | |
} | |
readString = ""; | |
} | |
int print_serial_message() | |
{ | |
if (readString == shooter_on) | |
{ | |
mySerial.print(readString); | |
return 1; | |
} | |
else if (readString == shooter_off) | |
{ | |
mySerial.print(readString); | |
return 1; | |
} | |
else | |
return 0; | |
} | |
void sound_sensor(int x) | |
{ | |
int soundPin = 4; | |
int soundThreshold = 1; | |
int soundVal = 0; | |
soundVal = digitalRead(soundPin); | |
if (soundVal == soundThreshold) | |
wireless(); | |
} | |
void wireless() | |
{ | |
mySerial.print(transmit_message); | |
delay(250); | |
} | |
void read_message_Xbee() | |
{ | |
char c = mySerial.read(); | |
if (c == '<') | |
{ | |
readString = c; | |
while (c != '>') | |
{ | |
if (mySerial.available() > 0) | |
{ | |
c = mySerial.read(); | |
readString += c; | |
} | |
} | |
if (print_serial_message() == 1) | |
{ | |
holdString = ""; | |
holdString = readString; | |
} | |
else | |
readString = ""; | |
} | |
processString(); | |
} | |
void read_message_USB() | |
{ | |
char c = Serial.read(); | |
if (c == '<') | |
{ | |
readString = c; | |
while (c != '>') | |
{ | |
if (Serial.available() > 0) | |
{ | |
c = Serial.read(); | |
readString += c; | |
} | |
} | |
holdString = ""; | |
holdString = readString; | |
} | |
Serial.print(holdString); | |
processString(); | |
} | |
void setup() | |
{ | |
Serial.begin(9600); | |
mySerial.begin(9600); | |
set_commands(); | |
} | |
void loop() | |
{ | |
if (Serial.available() > 0) | |
{ | |
read_message_USB(); | |
} | |
else if (mySerial.available() > 0) | |
{ | |
read_message_Xbee(); | |
} | |
processString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment