-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShockGame_new.ino
62 lines (50 loc) · 1.74 KB
/
ShockGame_new.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#define RELAY_ON 1
#define RELAY_OFF 0
int relayPins[] = {
2, 3, 4, 5, 6, 7, 8, 9 }; // an array of pin numbers to which relays are attached
int relayCount = 8; // the number of pins (i.e. the length of the array)
void setup() /****** SETUP: RUNS ONCE ******/
{
//-------( Initialize Pins so relays are inactive at reset)----
// the array elements are numbered from 0 to (pinCount - 1).
for (int thisPin = 0; thisPin < relayCount; thisPin++) {
pinMode(relayPins[thisPin], RELAY_OFF);
}
// initialize serial:
Serial.begin(9600);
while (! Serial); // Wait until Serial is ready - Leonardo Arduino
delay(4000); //Check that all relays are inactive at Reset
Serial.println("Ready to start");
//---( THEN set pins as outputs )----
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < relayCount; thisPin++) {
pinMode(relayPins[thisPin], OUTPUT);
}
}//--(end setup )---
void loop()
{
pinsOff(); //make sure everything is still off
if (Serial.available())
{
char ch = Serial.read(); //looking for characters on the serial port from the python script
int convertInt = ch - '1'; //easy way to convert char to int and get to array numbering
pinsOn(convertInt); // function to turn on relays
delay(100); //this is how long the shock lasts
pinsOff();
}
}
void pinsOn (int shockNumber){
for (int thisPin = 0; thisPin < relayCount; thisPin++) {
if (shockNumber == (thisPin))
{ //do nothing if this is the right number
}
else {
pinMode(relayPins[thisPin], RELAY_ON);
}
}
}
void pinsOff(){
for (int thisPin = 0; thisPin < relayCount; thisPin++) {
pinMode(relayPins[thisPin], RELAY_OFF);
}
}