Makerspace at the Fulton School
this week of Makerspace Summer camp I coded my arduinos on arduino cloud.
here’s some of the code I made.
RF24 radio(2, 4); // Replace with CE, CSN pins on robot
const byte address[6] = “00001”;
int AIN1 = 6;
int AIN2 = 7;
int PWMA = 5;
int BIN1 = 9;
int BIN2 = 10;
int PWMB = 11;
int STBY = 8;
void setup() {
Serial.begin(9600);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openReadingPipe(0, address);
radio.startListening();
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(STBY, OUTPUT);
digitalWrite(STBY, HIGH); // Enable motor driver
}
void loop() {
if (radio.available()) {
int speeds[2];
radio.read(&speeds, sizeof(speeds));int left = speeds[0]; int right = speeds[1]; // Debug print Serial.print(“Received! Left: “); Serial.print(left); Serial.print(” | Right: “); Serial.println(right); setMotor(left, AIN1, AIN2, PWMA); setMotor(right, BIN1, BIN2, PWMB);
}
}
void setMotor(int speed, int IN1, int IN2, int PWM) {
bool reverse = false;
if (speed < 0) {
reverse = true;
speed = -speed;
}
speed = constrain(speed, 0, 255);
analogWrite(PWM, speed);
digitalWrite(IN1, !reverse);
digitalWrite(IN2, reverse);
}
This was to test the signal between the controller and the robot.
Recent Comments