Show Posts
Pages: [1]
1  Topics / Robotics / Re: DF Robot Shop Motor Commands on: June 24, 2011, 08:39:22 am
Thanks PaulS, this code you provided works perfectly!
2  Topics / Robotics / Re: DF Robot Shop Motor Commands on: June 23, 2011, 01:01:44 am
@ PaulS-

I tried adding another "while" statement incorporating millis() to make the rover stop turning left after 300 milliseconds. I eventually got the compiler to run through the code without errors, but the rover doesn't actually behave differently. It still turns in endless counter-clockwise circles. I think I need some way to start the millis() counter once I send the "left" command over the serial port.

Here's my code below:

@ Schmidtn

Thanks for your suggestion about the motor speed. I do have the motors set at different speeds. They are pretty cheap and the rover tends to turn right when both motors are running at full speed. I set the left speed motor a bit slower so that the rover basically goes straight in the forwards and backwards modes.

//Control the Rover with four over-serial commands: left, right, forward, backwards.
//Modified the forward, left, right and backward commands to make the rover go
//in the desired direction.

int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control


void setup (void)
{
  int i;
  for(i=5;i<=8;i++)
  pinMode(i, OUTPUT);
  Serial.begin(9600);
}

void loop(void)
{
  while (Serial.available() < 1) {} // Wait until a character is received
  char val = Serial.read();
  int leftspeed = 250; //255 is maximum speed
  int rightspeed = 255;
  switch(val) // Perform an action depending on the command
  {
    case 'f'://Move Forward
     forward (leftspeed,rightspeed);
     break;
    case 'b'://Move Backwards
     reverse (leftspeed,rightspeed);
     break;
    case 'l'://Turn Left
     left (leftspeed,rightspeed);
     break;
    case 'r'://Turn Right
     right (leftspeed,rightspeed);
     break;
    default:
     stop();
     break;
    case 's'://stop
     halt (leftspeed,rightspeed);
     break;
  }
}

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

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

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


void left (char a,char b)
{
  int x = millis();
 
  while (x < 300)

{
  analogWrite (E1,a);
  digitalWrite(M1,LOW);
  analogWrite (E2,b);
  digitalWrite(M2,HIGH);
}

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

void halt (char a,char b)
{
  digitalWrite(E1,LOW);
  digitalWrite(E2,LOW);
}
3  Topics / Robotics / Re: DF Robot Shop Motor Commands on: June 22, 2011, 08:56:19 am
Thanks, I'll give that a shot.
4  Topics / Robotics / DF Robot Shop Motor Commands on: June 22, 2011, 12:20:08 am
I'm playing around with the rover kit from DF Robot Shop (http://www.robotshop.com/productinfo.aspx?pc=rb-rbo-33). I'm running the code essentially as described in the rover's instruction manual.

I notice that when I give the command for the rover to turn right or left, it spins around in clockwise or counterclockwise circles, respectively. Rather than spinning around endlessly, I really want it to spin 30 degrees left or right, stop, and wait for the next command. 

The kit comes with what look like pretty basic 5V DC motors. I think I need to re-write the code so that the motors only spin for 300 or 400 milliseconds before stopping and waiting for the next command. I'm not sure how to do that. I've looked at some of the "timer" libraries on the Arduino forum but they dont' really appear to be what I'm looking for.

Here's the code I'm running. Any general suggestions would be great!

//Control the Rover with four over-serial commands: left, right, forward, backwards.
//Modified the forward, left, right and backward commands to make the rover go
//in the desired direction.

int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control

void setup (void)
{
  int i;
  for(i=5;i<=8;i++)
  pinMode(i, OUTPUT);
  Serial.begin(9600);
}

void loop(void)
{
  while (Serial.available() < 1) {} // Wait until a character is received
  char val = Serial.read();
  int leftspeed = 250; //255 is maximum speed
  int rightspeed = 255;
  switch(val) // Perform an action depending on the command
  {
    case 'f'://Move Forward
     forward (leftspeed,rightspeed);
     break;
    case 'b'://Move Backwards
     reverse (leftspeed,rightspeed);
     break;
    case 'l'://Turn Left
     left (leftspeed,rightspeed);
     break;
    case 'r'://Turn Right
     right (leftspeed,rightspeed);
     break;
    default:
     stop();
     break;
    case 's'://stop
     halt (leftspeed,rightspeed);
     break;
  }
}

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

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

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

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

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

void halt (char a,char b)
{
  digitalWrite(E1,LOW);
  digitalWrite(E2,LOW);
}
5  Forum 2005-2010 (read only) / Troubleshooting / Re: Example 08A Picture on: January 20, 2011, 02:21:31 am
Thanks very much!
6  Forum 2005-2010 (read only) / Troubleshooting / Example 08A Picture on: January 18, 2011, 11:43:47 pm
Does anybody know where I could find the hand drawn picture of the "networked lamp" in the Getting Started with arduino book?

I left my book at home...
7  Forum 2005-2010 (read only) / Troubleshooting / Re: Simple Analogue Input Question on: December 14, 2010, 12:48:34 am
I tried messing with the delay(val) commands. I changed the sequence so that they came before the digitalWrite commands; added a negative sign in front of the val and also tried adding a negative sign in front of the zero in the analogRead(0) command. That didn't change anything.
8  Forum 2005-2010 (read only) / Troubleshooting / Simple Analogue Input Question on: December 14, 2010, 12:19:53 am
I'm working through some of the examples in the "Getting Started with Arduino" book. The first analogue input sketch is very simple. It uses an LED as the actuator and a photoresistor as the analogue sensor. The brighter the light, the slower the LED blinks. This is the sketch:

//Example 07a: Blink an LED at a rate specified by the value of the analogue input.

#define LED 13 // the pin for the LED

int val = 0;   // variable used to store the value coming from the sensor

void setup() {
  pinMode(LED, OUTPUT); // LED is the output
  
}

void loop (){
  
  val = analogRead (0); // read the value from the sensor
  
  digitalWrite(13, HIGH);  // turn the LED on
  
  delay(val); // stop the program for some time
  
  digitalWrite(13, LOW); // turn the LED off
  
  delay(val); // stop the program for some time
  
}

Is it possible to get the process reversed, so that the LED blinks faster as the amount of light increases? I tried reversing the digitalWrite commands so that the first was digitalWrite(13, LOW) and vice versa but that didn't do anything.

The sketch is so simple, I don't know what else to change.
Pages: [1]