ciao, posso chiedervi una mano per sistemare il mio codice? il 3 luglio ho la tesina e non riesco proprio a farlo funzionare!
grazie mille in anticipo. vi allego qui sotto lo sketch
// --------------------------------------------------------------------------- Motors
int sensor_left[] = {3};
int sensor_right[] = {4};
int motor_left[] = {2};
int motor_right[] = {7};
// --------------------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);
// Setup motors
int i;
for(i = 0; i < 2; i++)
{
pinMode(sensor_left[i], INPUT);
pinMode(sensor_right[i], INPUT);
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
}
// --------------------------------------------------------------------------- Loop
void loop() {
drive_forward();
delay(10000);
motor_stop();
Serial.println("1");
drive_backward();
delay(10000);
motor_stop();
Serial.println("2");
turn_left();
delay(10);
motor_stop();
Serial.println("3");
turn_right();
delay(10000);
motor_stop();
Serial.println("4");
motor_stop();
delay(10000);
motor_stop();
Serial.println("5");
}
// --------------------------------------------------------------------------- Drive
void motor_stop() {
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_right[0], LOW);
delay(25);
}
void drive_forward(){
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[1], HIGH);
}
void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_right[0], LOW);
}
void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_right[0], HIGH);
}
void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_right[0], LOW);
}
// if on the line drive left and right at the same speed (left is CCW / right is CW)
motor_left = -SPEED;
motor_right = SPEED;
// if the line is under the right sensor, adjust relative speeds to turn to the right
leftSpeed = -(SPEED + 50);
rightSpeed = SPEED - 50;
}
// if the line is under the left sensor, adjust relative speeds to turn to the left
leftSpeed = -(SPEED - 50);
rightSpeed = SPEED + 50;
}
// if all sensors are on black or up in the air, stop the motors.
// otherwise, run motors given the control speeds above.
motors.stop();
}
else
{
motors.leftMotor(leftSpeed);
motors.rightMotor(rightSpeed);
}
delay(0); // add a delay to decrease sensitivity.
}