Forum >Help with code
General

Help with code

userHead moses58 2011-02-24 13:24:37 4852 Views2 Replies
Hi I found this code on your forum.
Could someone help me add a few features?
I have a Romeo All in One, running two motors.

I would like both motors to run forward for 10 seconds.
Then stop for 60 seconds.
Then Reverse for 10 seconds

I'm new to code so this is ifficult for me.

Here is the code:

int E1 = 5;
int E2 = 6;
int M1 = 4;
int M2 = 7;

void advance(char a,char b)
{
analogWrite(E1,a);
digitalWrite(M1,HIGH);
analogWrite(E2,b);
digitalWrite(M2,HIGH);
}

void setup()
{
for(int i=4;i<=7;i++)
pinMode(i, OUTPUT);
}

void loop()
{
int value = 250;
advance(value,value);
delay(500);
if(value <= 240) value += 10;
else value = 150;
}
2011-02-26 11:33:01 Thank you to everyone that offered advice and code. I made some slight modifications to add right and left turns, as well as longer stop times. Everything is running fine and as expected. Now this is for the HCR platform with Romeo All in One from DFrobots. It has 3 bumper switchs  and 5 IR sensors. Could someone offer advice on adding these sensors to the code? Example:

Robot moves forward and bumps into a wall. Stop, back up for 2 seconds, turn right for 3 seconds, continue with loop. I know how to stop and turn. I don't know the code to trigger the correction.

With the sensors it is similar but avoidence at 3 or 4 inches from wall.

Here is my sketch it is working great:

[code]int E1 = 5;
int E2 = 6;
int M1 = 4;
int M2 = 7; 

void forward(char a,char b) //Move forward
        {
          analogWrite(E1,a);
          digitalWrite(M1,HIGH);   
          analogWrite(E2,b);   
          digitalWrite(M2,HIGH);
        } 

void stop(void)                    //Stop
        {
          digitalWrite(E1,LOW); 
          digitalWrite(E2,LOW);     
        }

void backward (char a,char b) //Move backward
        {
          analogWrite (E1,a);
          digitalWrite(M1,LOW); 
          analogWrite (E2,b);   
          digitalWrite(M2,LOW);
}


void Left (char a,char b) //Turn left
        {
          analogWrite (E1,a);
          digitalWrite(M1,LOW);   
          analogWrite (E2,b);   
          digitalWrite(M2,HIGH);
        }
void Right (char a,char b) //Turn right
        {
          analogWrite (E1,a);
          digitalWrite(M1,HIGH);   
          analogWrite (E2,b);   
          digitalWrite(M2,LOW);
        }

void setup()

{
    for(int i=4;i<=7;i++)
    pinMode(i, OUTPUT);
}

void loop()

{

  forward(100,100); //Forward at 50% full speed
  delay(10000); //delay for 10 seconds
  stop(); //Stop
  delay(600000); //delay for 60 seconds
  backward(100,100); //backward at 50% full speed
  delay(10000); //delay for 10 seconds
  stop(); //Stop
  delay(600000); //delay for 60 seconds
  Left(100,100);  //Left at 50% full speed
  delay(10000); //delay for 10 seconds
  stop(); //Stop
  delay(600000); //delay for 60 seconds
  Right(100,100);  //Left at 50% full speed
  delay(10000); //delay for 10 seconds
  stop(); //Stop
  delay(600000); //delay for 60 seconds

}
[/code]
userHeadPic moses58
2011-02-24 20:43:34 [code]int E1 = 5;
int E2 = 6;
int M1 = 4;
int M2 = 7; 

void advance(char a,char b)
        {
          analogWrite(E1,a);
          digitalWrite(M1,HIGH);   
          analogWrite(E2,b);   
          digitalWrite(M2,HIGH);
        } 

void backward (char a,char b)          //Move backward
        {
          analogWrite (E1,a);
          digitalWrite(M1,LOW); 
          analogWrite (E2,b);   
          digitalWrite(M2,LOW);
}

void stop(void)                    //Stop
        {
          digitalWrite(E1,LOW); 
          digitalWrite(E2,LOW);     
        }

void setup()
{
    for(int i=4;i<=7;i++)
    pinMode(i, OUTPUT);
}

void loop()
{
 
  advance(200,200); //Forward at 78% full speed
  delay(10000); //delay for 10 seconds
  stop(); //Stop
  delay(60000); //delay for 60 seconds
  backward(200,200)//backward at 78% full speed
  delay(10000); //delay for 10 seconds
 
}
[/code]
userHeadPic R2D2C3PO