// https://forum.arduino.cc/t/help-the-universal-robotics-challenge-is-coming-up-in-1-week-and-i-need-help-with-my-coding/1469292
// https://forum.arduino.cc/t/i-need-help-with-my-studuino-coding-i-need-my-robot-to-follow-a-thick-black-line/1468410
// https://www.urc21.org/en/pdf/2026_rulebook_online_advance_en.pdf
#include <Servo.h>
Servo servo; // steering
Servo M1pwm; // sim of motor 1
Servo M2pwm; // sim of motor 2
int M1dioFWD = 2;
int M1pwmPIN = 3;
int M1dioREV = 4;
int M2dioFWD = 7;
int M2pwmPIN = 6;
int M2dioREV = 8;
int steering = 12;
int IRphotrefA1 = 15; // A1
int IRphotrefA2 = 16; // A2
int buttonPin = 17; // A3
int IRphotrefA6 = 20; // A6
int IRphotrefA7 = 21; // A7
int outputPin[] = {2, 3, 4, 6, 7, 8};
void setup() {
Serial.begin(115200);
servo.attach(steering); // simulate steering
M1pwm.attach(M1pwmPIN); // simulate motor speed
M2pwm.attach(M2pwmPIN); // simulate motor speed
for (byte i = 0; i < 6; i++)
pinMode(outputPin[i], OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
while (digitalRead(buttonPin) != LOW) {
Serial.print("Set A2 (around 25). Press button to store: ");
Serial.println(analogRead(A2));
}
delay(200); // debounce
while (digitalRead(buttonPin) != LOW) {
Serial.print("Set A7 (around 30). Press button to store: ");
Serial.println(analogRead(A7));
}
delay(200); // debounce
Serial.println("\nPress button to start.");
// start of blocks
whenClicked();
Serial.println("\nStarted.");
setServoD12(0);
mainBlock();
stop();
wait(1);
mainBlock();
go("CC");
wait(1);
mainBlock();
stop();
wait(1);
mainBlock();
go("CC");
wait(5);
stop();
setServoD12(90); // "push"
wait(5);
go("CW");
wait(1);
while (analogRead(IRphotrefA7) >= 30) {
go("CC");
}
go("CW");
wait(1);
stop();
}
void loop() {
}
void wait(unsigned long duration) {
delay(duration * 1000UL);
}
void go(const char *direction) {
DCpwm("M1", 100);
DCpwm("M2", 100);
DCmot("M1", direction);
DCmot("M2", direction);
}
void stop() {
DCpwm("M1", 0);
DCpwm("M2", 0);
}
void mainBlock() {
while (analogRead(IRphotrefA7) >= 30) {
if (analogRead(IRphotrefA2) >= 25) {
DCpwm("M1", 100);
DCpwm("M2", 50);
DCmot("M1", "CC");
DCmot("M2", "CC");
} else {
DCpwm("M2", 100);
DCpwm("M1", 50);
DCmot("M2", "CC");
}
}
}
void DCpwm(const char *motor, int value) {
if (motor == "M1")
analogWrite(M1pwmPIN, value);
if (motor == "M2")
analogWrite(M2pwmPIN, value);
}
void DCmot(const char *motor, const char *direction) {
int motf, motr; // motor pin directions
if (motor == "M1") {
motf = M1dioFWD;
motr = M1dioREV;
}
if (motor == "M2") {
motf = M2dioFWD;
motr = M2dioREV;
}
if (direction == "CC") {
digitalWrite(motf, HIGH);
digitalWrite(motr, LOW);
}
if (direction == "CW") {
digitalWrite(motf, LOW);
digitalWrite(motr, HIGH);
}
}
void setServoD12(int angle) {
servo.write(angle + 90); // different servo orientation
}
void whenClicked() {
while (digitalRead(17) == 0) {} // stay here until LOW = PRESSED
}M1pwm
M2pwm
Steering
M1fwd
M1rev
M2fwd
M2rev
Button
0
255
51
102
204
153
127
0
255
51
102
204
153
127
Button
A2
A7