Hi everybody, I was wondering if anyone of you would know how to programm a SRF05 to stop two motors that i have in a robot chassis with the code that I have implemented already.
this is the code:
#include <Servo.h>
Servo servoloco; // create servo object to control a servo
char puertoserie; // variable to receive data from the serial port
int motor_izquierda[] = {3, 5};
int motor_derecha[] = {6, 9};
int val = 0; // variable to store the value coming from the sensor
void setup() {
servoloco.attach(11); // attaches the servo on pin 11 to the servo object v
Serial.begin(9600); // start serial communication at 9600bps
int i;
for(i = 0; i < 2; i++){
pinMode(motor_izquierda[i], OUTPUT);
pinMode(motor_derecha[i], OUTPUT);
}
}
void loop() {
if( Serial.available() ) // if data is available to read
{
puertoserie = Serial.read(); // read it and store it in 'val'
}
if( puertoserie == 'w' )
{
go_forward_both_motors_on();
}
if( puertoserie == 's' )
{
go_backwards_both_motors_on();
}
if( puertoserie == 'd' )
{
go_right();
}
if( puertoserie == 'a' )
{
go_left();
}
if( puertoserie == 'q' )
{
stop_motors_both_motors_off();
}
if ( puertoserie == 'o' )
{
}
if ( puertoserie == 'z' )
{
stop_motors_both_motors_off();
}
{
servoloco.write(10); // sets the servo position according to the scaled value
delay(600); // waits for the servo to get there
servoloco.write(170); // sets the servo position according to the scaled value
delay(600); // waits for the servo to get there
}
}
void stop_motors_both_motors_off(){
digitalWrite(motor_izquierda[0], LOW);
digitalWrite(motor_izquierda[1], LOW);
digitalWrite(motor_derecha[0], LOW);
digitalWrite(motor_derecha[1], LOW);
}
void go_left(){
digitalWrite(motor_izquierda[0], HIGH);
digitalWrite(motor_izquierda[1], LOW);
digitalWrite(motor_derecha[0], HIGH);
digitalWrite(motor_derecha[1], LOW);
}
void go_right(){
digitalWrite(motor_izquierda[0], LOW);
digitalWrite(motor_izquierda[1], HIGH);
digitalWrite(motor_derecha[0], LOW);
digitalWrite(motor_derecha[1], HIGH);
}
void go_forward_both_motors_on(){
digitalWrite(motor_izquierda[0], LOW);
digitalWrite(motor_izquierda[1], HIGH);
digitalWrite(motor_derecha[0], HIGH);
digitalWrite(motor_derecha[1], LOW);
}
void go_backwards_both_motors_on(){
digitalWrite(motor_izquierda[0], HIGH);
digitalWrite(motor_izquierda[1], LOW);
digitalWrite(motor_derecha[0], LOW);
digitalWrite(motor_derecha[1], HIGH);
}
here a picture of how the robot looks like at the moment. also could anyone modify my code so it is better structured? im a newbie and my proframming skills are not that good.
thanks in advance