-
-
Save emhe3766/0ff75577614b6173cb56227205cdecd5 to your computer and use it in GitHub Desktop.
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
/ libray for motors | |
#include <AFMotor.h> | |
// librays for the transmitter/receiver | |
#include <SPI.h> | |
#include <nRF24L01.h> | |
#include <RF24.h> | |
// declares all the motors | |
AF_DCMotor motor1(1); | |
AF_DCMotor motor2(2); | |
AF_DCMotor motor3(3); | |
AF_DCMotor motor4(4); | |
// int Xpot; | |
// int Ypot; | |
// sets the pins for the radio | |
RF24 radio(8, 9); // CE, CSN | |
const byte Address[6] = "00001"; | |
// sets the max speed and the joystick values | |
int speed = 255; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
radio.begin(); | |
radio.openReadingPipe(1, Address); | |
// the motors will be stopped | |
motor1.setSpeed(0); | |
motor2.setSpeed(0); | |
motor3.setSpeed(0); | |
motor4.setSpeed(0); | |
} | |
// might need an update function for joystick, example can be found in Lab 4 Part 5 | |
void loop() { | |
// put your main code here, to run repeatedly: | |
radio.startListening(); | |
// when the radio is range4 | |
if (radio.available()) { | |
while (radio.available()) { | |
// radio.read(Xpot, sizeof(Xpot)); | |
// radio.read(Ypot, sizeof(Ypot)); | |
int values[2]; | |
radio.read(values, sizeof(values)); | |
int Xpot = values[0]; | |
int Ypot = values[1]; | |
int XPOS = map(Xpot, 2, 1018, 0, 2 ); | |
int YPOS = map(Ypot, 2, 1018, 0, 2 ); | |
// Serial.print(Xpot); | |
// Serial.println(Ypot); | |
Serial.print(XPOS); | |
Serial.print (YPOS); | |
// forward | |
if (XPOS == 1 && YPOS == 0){ | |
motor1.setSpeed(speed); | |
motor1.run(FORWARD); | |
motor2.setSpeed(speed); | |
motor2.run(FORWARD); | |
motor3.setSpeed(speed); | |
motor3.run(FORWARD); | |
motor4.setSpeed(speed); | |
motor4.run(FORWARD); | |
Serial.println("forward"); | |
} | |
// backwards | |
if (XPOS == 1 && YPOS == 2){ | |
motor1.setSpeed(speed); | |
motor1.run(BACKWARD); | |
motor2.setSpeed(speed); | |
motor2.run(BACKWARD); | |
motor3.setSpeed(speed); | |
motor3.run(BACKWARD); | |
motor4.setSpeed(speed); | |
motor4.run(BACKWARD); | |
Serial.println("backwards"); | |
} | |
//right | |
if (XPOS == 2 && YPOS == 1){ | |
motor1.setSpeed(speed); | |
motor1.run(BACKWARD); | |
motor2.setSpeed(speed); | |
motor2.run(BACKWARD); | |
motor3.setSpeed(speed); | |
motor3.run(FORWARD); | |
motor4.setSpeed(speed); | |
motor4.run(FORWARD); | |
Serial.println("right"); | |
} | |
//left | |
if (XPOS == 0 && YPOS == 1){ | |
motor1.setSpeed(speed); | |
motor1.run(FORWARD); | |
motor2.setSpeed(speed); | |
motor2.run(FORWARD); | |
motor3.setSpeed(speed); | |
motor3.run(BACKWARD); | |
motor4.setSpeed(speed); | |
motor4.run(BACKWARD); | |
Serial.println("left"); | |
} | |
//topleft | |
if (XPOS == 0 && YPOS == 0){ | |
motor1.setSpeed(speed); | |
motor1.run(FORWARD); | |
motor2.setSpeed(speed); | |
motor2.run(FORWARD); | |
motor3.setSpeed(speed/3); | |
motor3.run(FORWARD); | |
motor4.setSpeed(speed/3); | |
motor4.run(FORWARD); | |
Serial.println("topleft"); | |
} | |
//topright | |
if (XPOS == 2 && YPOS == 0){ | |
motor1.setSpeed(speed/3); | |
motor1.run(FORWARD); | |
motor2.setSpeed(speed/3); | |
motor2.run(FORWARD); | |
motor3.setSpeed(speed); | |
motor3.run(FORWARD); | |
motor4.setSpeed(speed); | |
motor4.run(FORWARD); | |
Serial.println("topright"); | |
} | |
//bottomleft | |
if (XPOS == 0 && YPOS == 2){ | |
motor1.setSpeed(speed); | |
motor1.run(BACKWARD); | |
motor2.setSpeed(speed); | |
motor2.run(BACKWARD); | |
motor3.setSpeed(speed/3); | |
motor3.run(BACKWARD); | |
motor4.setSpeed(speed/3); | |
motor4.run(BACKWARD); | |
Serial.println("bottomleft"); | |
} | |
//bottomright | |
if (XPOS == 2 && YPOS == 2){ | |
motor1.setSpeed(speed/3); | |
motor1.run(BACKWARD); | |
motor2.setSpeed(speed/3); | |
motor2.run(BACKWARD); | |
motor3.setSpeed(speed); | |
motor3.run(BACKWARD); | |
motor4.setSpeed(speed); | |
motor4.run(BACKWARD); | |
Serial.println("bottomright"); | |
} | |
// stop | |
if (XPOS == 1 && YPOS == 1){ | |
motor1.setSpeed(0); | |
motor1.run(RELEASE); | |
motor2.setSpeed(0); | |
motor1.run(RELEASE); | |
motor3.setSpeed(0); | |
motor1.run(RELEASE); | |
motor4.setSpeed(0); | |
motor1.run(RELEASE); | |
Serial.println("stop"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment