Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <AFMotor.h>
- #include <SPI.h>
- #include <nRF24L01.h>
- #include <RF24.h>
- #define enA 5 // Note: Pin 9 in previous video ( pin 10 is used for the SPI communication of the NRF24L01)
- #define in1 3
- #define in2 4
- #define enB 6 // Note: Pin 10 in previous video
- #define in3 7
- #define in4 8
- // Create one motor instance
- AF_DCMotor motor1(1,MOTOR12_64KHZ); // set up motors.
- AF_DCMotor motor2(2, MOTOR12_8KHZ);
- RF24 radio(7,8); // nRF24L01 (CE, CSN)
- const byte address[6] = "00001";
- unsigned long lastReceiveTime = 0;
- unsigned long currentTime = 0;
- // Max size of this struct is 32 bytes
- struct Data_Package {
- byte j1PotX;
- byte j1PotY;
- byte j2PotX;
- byte j2PotY;
- byte button1;
- byte button2;
- byte button3;
- byte button4;
- };
- Data_Package data; //Create a variable with the above structure
- void setup() {
- Serial.begin(115200);
- Serial.println("receiver");
- radio.begin();
- radio.openReadingPipe(0, address);
- radio.setAutoAck(false);
- radio.setPALevel(RF24_PA_LOW);
- radio.startListening(); // Set the module as receiver
- }
- void loop() {
- if (radio.available()) {
- radio.read(&data, sizeof(Data_Package));
- printdata();
- }
- int btnState1 = data.button1;
- int btnState2 = data.button2;
- int forward_back = map(data.j1PotY, 0, 1023, -128, 127);
- int left_right = map(data.j2PotX, 0, 1023, -128, 127);
- int left_motor = (forward_back + left_right);
- int right_motor = (forward_back - left_right);
- // Scale factor defaults to 1
- int scale_factor = 1;
- // Calculate scale factor
- if (abs(left_motor) > 100 or abs(right_motor) > 100) {
- // Find highest of the 2 values, since both could be above 100
- int x = max(abs(left_motor), abs(right_motor));
- // Calculate scale factor
- int scale_factor = 100.0 / x;
- }
- // Use scale factor, and turn values back into integers
- left_motor = round(left_motor * scale_factor);
- right_motor = round(right_motor * scale_factor);
- motor1.setSpeed(left_motor);
- motor2.setSpeed(right_motor);
- // Actually move the motors
- if (forward_back < 0) {
- motor1.run(BACKWARD);
- motor2.run(BACKWARD);
- }
- else {
- motor1.run(FORWARD);
- motor2.run(FORWARD);
- }
- /* int CurrentTime = millis();
- int motorspeed = constrain(motorspeed, 0, 255);
- motor1.setSpeed(motorspeed);
- motor2.setSpeed(motorspeed);
- if (btnState1 == 1) {
- int buttonpressed = millis();
- motorspeed = round((buttonpressed - CurrentTime) * .1);
- motor1.run(FORWARD);
- motor2.run(FORWARD);
- }
- if (btnState2 == 1){
- int button2pressed = millis();
- motorspeed = round((button2pressed - CurrentTime) * .1);
- motor1.run(BACKWARD);
- motor2.run(BACKWARD);
- }
- else {
- motorspeed = 0;
- }
- */
- }
- void printdata() {
- Serial.println(data.j1PotX);
- }
- void resetData() {
- // Reset the values when there is no radio connection - Set initial default values
- data.j1PotX = 127;
- data.j1PotY = 127;
- data.j2PotX = 127;
- data.j2PotY = 127;
- data.button1 = 1;
- data.button2 = 1;
- data.button3 = 1;
- data.button4 = 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement