Skip to content

Instantly share code, notes, and snippets.

@tabchas
Created June 24, 2012 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tabchas/2985303 to your computer and use it in GitHub Desktop.
Save tabchas/2985303 to your computer and use it in GitHub Desktop.
Wireless Management System for Shooting Competitions - Arduino Target Transmitter Code
#include <EEPROM.h>
#include <NewSoftSerial.h>
NewSoftSerial mySerial(2,3);
String readString = "";
String holdString = "";
String id_of_sensor = String(0);
String transmit_message = "<T%>";
String wireless_command = "<T%*>";
String strobe_command = "<T%!>";
String wireless_strobe_command = "<T%&>";
String game_mode_off_command = "<T%?>";
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);
wireless_command = wireless_command.replace("%", id_of_sensor);
strobe_command = strobe_command.replace("%", id_of_sensor);
wireless_strobe_command = wireless_strobe_command.replace("%", id_of_sensor);
game_mode_off_command = game_mode_off_command.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 == wireless_command)
vibration_sensor(1);
else if (holdString == strobe_command)
vibration_sensor(2);
else if (holdString == wireless_strobe_command)
vibration_sensor(3);
else if (holdString == ask_for_id_command)
{
config_mode(1);
holdString = "";
}
else if (holdString == switch_id_command)
{
config_mode(2);
holdString = "";
}
else if (holdString == game_mode_off_command)
{
}
readString = "";
}
int print_serial_message()
{
if (readString == wireless_command)
{
mySerial.print(readString);
return 1;
}
else if (readString == strobe_command)
{
mySerial.print(readString);
return 1;
}
else if (readString == wireless_strobe_command)
{
mySerial.print(readString);
return 1;
}
else if (readString == game_mode_off_command)
{
mySerial.print(readString);
return 1;
}
else
return 0;
}
void vibration_sensor(int x)
{
int piezoPin = 0;
int piezoThreshold = 8;
int piezoVal = 0;
piezoVal = analogRead(piezoPin);
if (piezoVal >= piezoThreshold)
{
switch(x)
{
case 1:
wireless();
break;
case 2:
strobe();
break;
case 3:
wireless_strobe();
break;
}
}
}
void wireless()
{
mySerial.print(transmit_message);
delay(250);
}
void wireless_strobe()
{
digitalWrite(4, HIGH);
mySerial.print(transmit_message);
delay(100);
digitalWrite(4, LOW);
}
void strobe()
{
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
}
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);
pinMode(4, OUTPUT);
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