Created
February 2, 2018 12:20
-
-
Save jan-patrick/fda32966b0b85a0634364f51c39b3668 to your computer and use it in GitHub Desktop.
reel-withMaster
This file contains 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 <Servo.h> | |
#include <Wire.h> | |
Servo sbehind; | |
Servo sfright; | |
Servo sfleft; | |
byte balltarget; | |
int ledshooter = 2; | |
int ledtarget = 7; | |
int myPlayer=3; | |
int devicePlayer = 11; | |
int motorright_A=12; | |
int motorright_B=9; | |
int motorleft_A=13; | |
int motorleft_B=8; | |
int motorright_Speed=3; | |
int motorleft_Speed=11; | |
int senfront=A3; | |
int senback=A2; | |
bool ballbesitz = false; | |
byte statusPlayer = 0; //-1 = nicht bereit, 1 = bereit, 2 = target, 3 = shooterSensor, 4 = shoot | |
void setup() { | |
balltarget = 8; | |
Wire.begin(devicePlayer); // join i2c bus with address #11 | |
Wire.onRequest(requestEvent); // register request event | |
Wire.onReceive(receiveEvent); // register receive event | |
Serial.begin(9600); | |
//Setup Motor Rechts | |
pinMode(motorright_A, OUTPUT); //Initiates Motor Channel A pin | |
pinMode(motorright_B, OUTPUT); //Initiates Brake Channel B pin | |
//Setup Motor Links | |
pinMode(motorleft_A, OUTPUT); //Initiates Motor Channel A pin | |
pinMode(motorleft_B, OUTPUT); //Initiates Brake Channel B pin | |
pinMode(ledshooter, OUTPUT); //Initiates the green led as shooter led | |
pinMode(ledtarget, OUTPUT); //Initiates the red led as target led | |
sbehind.attach(10); //Initiates the servo in the back | |
sfright.attach(5); //Initiates the servo on the right front (seen from behind) | |
sfleft.attach(6); //Initiates the servo on the left front (seen from behind) | |
} | |
void loop(){ | |
int sensorWert = analogRead(senfront); | |
if (analogRead(senfront) >=550) { | |
ballbesitz = true; | |
} else { | |
ballbesitz = false; | |
} | |
// ICH BIN BEREIT | |
if (balltarget != myPlayer && statusPlayer == 0){ | |
Serial.println("ICH BIN BEREIT"); | |
analogWrite(motorright_Speed, 0); //Spins the motor on Channel A at full speed | |
analogWrite(motorleft_Speed, 0); //Spins the motor on Channel B at half speed | |
digitalWrite(motorright_B, HIGH); //Engage the Brake for Motor Rechts | |
digitalWrite(motorleft_B, HIGH); //Engage the Brake for Motor Links | |
digitalWrite(ledshooter, LOW); | |
digitalWrite(ledtarget, LOW); | |
sbehind.write(135); | |
sfright.write(50); | |
sfleft.write(120); | |
statusPlayer = 1; | |
requestEvent(); | |
Serial.print("StatusPlayer: "); | |
Serial.println(statusPlayer); | |
Serial.println(sensorWert); | |
} | |
// ICH BIN DAS ZIEL (BALLTARGET) | |
if (balltarget == myPlayer && statusPlayer == 1){ | |
Serial.println("ICH BIN DAS ZIEL: "); | |
Serial.println(balltarget); | |
digitalWrite(ledshooter, LOW); | |
digitalWrite(ledtarget, HIGH); | |
statusPlayer = 2; | |
requestEvent(); | |
Serial.print("statusPlayer: "); | |
Serial.println(statusPlayer); | |
Serial.println(sensorWert); | |
} | |
// ICH WURDE NICHT GETROFFEN | |
if (balltarget != myPlayer && statusPlayer == 2){ | |
Serial.print("NICHT GETROFFEN: "); | |
Serial.println(balltarget); | |
Serial.println(sensorWert); | |
for (int i=0; i <= 4; i++){ | |
digitalWrite(ledtarget, HIGH); | |
delay(100); | |
digitalWrite(ledtarget, LOW); | |
delay(100); | |
} | |
statusPlayer = 0; | |
requestEvent(); | |
} | |
// ICH BIN SHOOTER | |
int sensorStartzeit = millis(); | |
while (ballbesitz == true && statusPlayer < 3) { | |
if (analogRead(senfront) >= 550) { | |
ballbesitz = true; | |
} else { | |
ballbesitz = false; | |
} | |
//Serial.print("Ballbesitz: "); | |
//Serial.println(ballbesitz); | |
//Serial.println(getAbstand()); | |
if (millis() - sensorStartzeit > 4000 && ballbesitz == true) { | |
Serial.print("statusPlayer: "); | |
Serial.println(statusPlayer); | |
digitalWrite(ledshooter, HIGH); | |
digitalWrite(ledtarget, LOW); | |
Serial.print("statusPlayer: "); | |
Serial.println(statusPlayer); | |
Serial.println(sensorWert); | |
statusPlayer = 3; | |
requestEvent(); | |
statusPlayer = 4; | |
}} | |
// ICH SCHIESSE | |
if (statusPlayer == 4) { | |
balltoback(); | |
delay(100); | |
Serial.println(senfront); | |
if(analogRead(senback) >= 500){ | |
Serial.println(sensorWert); | |
switch (balltarget) { | |
case 0: //nach links hinten | |
sfright.write(100); | |
delay(50); | |
//Motor A forward @ high speed | |
digitalWrite(motorright_A, LOW); //Establishes forward direction of Channel A | |
digitalWrite(motorright_B, LOW); //Disengage the Brake for Channel A | |
analogWrite(motorright_Speed, 90); //Spins the motor on Channel A at full speed | |
//Motor B backward @ high speed | |
digitalWrite(motorleft_A, HIGH); //Establishes backward direction of Channel B | |
digitalWrite(motorleft_B, LOW); //Disengage the Brake for Channel B | |
analogWrite(motorleft_Speed, 70); //Spins the motor on Channel B at half speed | |
delay(500); | |
sbehind.write(115); | |
delay(700); | |
digitalWrite(motorright_B, HIGH); //Engage the Brake for Motor Rechts | |
digitalWrite(motorleft_B, HIGH); //Engage the Brake for Motor Links | |
delay(250); | |
break; | |
case 1: //nach rechts hinten | |
sfleft.write(70); | |
delay(50); | |
//Motor A forward @ low speed | |
digitalWrite(motorright_A, LOW); //Establishes forward direction of Channel A | |
digitalWrite(motorright_B, LOW); //Disengage the Brake for Channel A | |
analogWrite(motorright_Speed, 115); //Spins the motor on Channel A at full speed | |
//Motor B backward @ low speed | |
digitalWrite(motorleft_A, HIGH); //Establishes backward direction of Channel B | |
digitalWrite(motorleft_B, LOW); //Disengage the Brake for Channel B | |
analogWrite(motorleft_Speed, 85); //Spins the motor on Channel B at half speed | |
delay(500); | |
sbehind.write(115); | |
delay(700); | |
digitalWrite(motorright_B, HIGH); //Engage the Brake for Motor Rechts | |
digitalWrite(motorleft_B, HIGH); //Engage the Brake for Motor Links | |
delay(250); | |
break; | |
case 2: //nach rechts vorne | |
sfleft.write(70); | |
delay(50); | |
//Motor A forward @ low speed | |
digitalWrite(motorright_A, LOW); //Establishes forward direction of Channel A | |
digitalWrite(motorright_B, LOW); //Disengage the Brake for Channel A | |
analogWrite(motorright_Speed, 70); //Spins the motor on Channel A at full speed | |
//Motor B backward @ low speed | |
digitalWrite(motorleft_A, HIGH); //Establishes backward direction of Channel B | |
digitalWrite(motorleft_B, LOW); //Disengage the Brake for Channel B | |
analogWrite(motorleft_Speed, 55); //Spins the motor on Channel B at half speed | |
delay(500); | |
sbehind.write(115); | |
delay(700); | |
digitalWrite(motorright_B, HIGH); //Engage the Brake for Motor Rechts | |
digitalWrite(motorleft_B, HIGH); //Engage the Brake for Motor Links | |
delay(250); | |
break; | |
case 4: //nach links vorne | |
sfright.write(100); | |
delay(50); | |
//Motor A forward @ low speed | |
digitalWrite(motorright_A, LOW); //Establishes forward direction of Channel A | |
digitalWrite(motorright_B, LOW); //Disengage the Brake for Channel A | |
analogWrite(motorright_Speed, 60); //Spins the motor on Channel A at full speed | |
//Motor B backward @ low speed | |
digitalWrite(motorleft_A, HIGH); //Establishes backward direction of Channel B | |
digitalWrite(motorleft_B, LOW); //Disengage the Brake for Channel B | |
analogWrite(motorleft_Speed, 70); //Spins the motor on Channel B at half speed | |
delay(700); | |
sbehind.write(115); | |
delay(500); | |
digitalWrite(motorright_B, HIGH); //Engage the Brake for Motor Rechts | |
digitalWrite(motorleft_B, HIGH); //Engage the Brake for Motor Links | |
delay(250); | |
break; | |
default: | |
sbehind.write(80); | |
sfright.write(50); | |
sfleft.write(120); | |
analogWrite(motorright_Speed, 0); //Spins the motor on Channel A at full speed | |
analogWrite(motorleft_Speed, 0); //Spins the motor on Channel B at half speed | |
digitalWrite(motorright_B, HIGH); //Engage the Brake for Motor Rechts | |
digitalWrite(motorleft_B, HIGH); //Engage the Brake for Motor Links | |
delay(500); | |
break; | |
} | |
Serial.println("I shot."); | |
statusPlayer = 0; | |
delay(3000); | |
}else if(analogRead(senfront) <= 500 && analogRead(senback) <= 500){ | |
statusPlayer = 0; | |
Serial.println(sensorWert); | |
}else{ | |
balltoback(); | |
delay(100); | |
Serial.println(sensorWert); | |
} | |
} | |
} | |
void requestEvent() { | |
Wire.write(statusPlayer); // respond with variable ballinmachine (0 if noball 1 if ballinmachine) | |
delay (50); | |
} | |
void receiveEvent(int howMany) { | |
while (Wire.available()) { | |
balltarget = Wire.read(); | |
Serial.print("Receive target: "); | |
Serial.println(balltarget); | |
} | |
} | |
void balltoback(){ | |
//Ball to back | |
sbehind.write(135); | |
delay(250); | |
//Motor A backward @ low speed | |
digitalWrite(motorright_A, HIGH); //Establishes backward direction of Channel A | |
digitalWrite(motorright_B, LOW); //Disengage the Brake for Channel B | |
analogWrite(motorright_Speed, 60); //Spins the motor on Channel A at low speed | |
//Motor B backward @ low speed | |
digitalWrite(motorleft_A, LOW); //Establishes backward direction of Channel B | |
digitalWrite(motorleft_B, LOW); //Disengage the Brake for Channel B | |
analogWrite(motorleft_Speed, 60); //Spins the motor on Channel B at low speed | |
sbehind.write(80); | |
delay(2000); | |
analogWrite(motorright_Speed, 0); //Spins the motor on Channel A at full speed | |
analogWrite(motorleft_Speed, 0); //Spins the motor on Channel B at half speed | |
digitalWrite(motorright_B, HIGH); //Engage the Brake for Motor Rechts | |
digitalWrite(motorleft_B, HIGH); //Engage the Brake for Motor Links | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment