Show Posts
Pages: [1]
1  Using Arduino / Project Guidance / Re: Motor control after making a turn on: June 02, 2012, 01:47:57 am
yup were testing it this coming week
2  Using Arduino / Project Guidance / Re: Motor control after making a turn on: June 01, 2012, 10:55:29 am
the ball drop will be controlled either by a relay or by some mechanical latch or sorts, depending on the budget.

Well i reckon the movement part is done, Cheers again for the help guys, much appreciated smiley

Hopefully this works on the actual robot
3  Using Arduino / Project Guidance / Re: Motor control after making a turn on: May 31, 2012, 02:24:42 am
i dont quite follow. Correct me if im wrong, but you mean that, rather than have state = ... after each case, i simply delete each of these, then at the end of the switch statement, add in state ++ ?

Basically for this project, the bot is split into 2 parts. The first part, containing all the electronics, transports a ball though an path with obstacles till it gets to a bucket, whereby the bot drops the ball into the bucket. Within the bucket lies the 2nd part, purely mechanical, and is supposed to climb a certain length rope while holding onto the ball, then stop and hold it up there indefinitely.

...easy right?
4  Using Arduino / Project Guidance / Re: Motor control after making a turn on: May 31, 2012, 01:29:55 am
How about this then?

Code:
// Define the sensor and motors as variables
int IRsensor = 0;
int leftmotor = 1;
int rightmotor = 2;

// This variable holds the state the system is in. It will start at 0 and count upwards for
// how every many state the system will have.
int state = 0;
//Create Flag Variable for State
int flag = 0;

// This long variable will hold the previous number of milli seconds that have ellapsed since
// the board was turned on. It is used for timing delays.
long previousMillis;

// This long variable will hold the current amount of ellapsed millis seconds since the board was turned on
long currentMillis;

// This variable will hold the desired delay time in millis seconds
long interval;

void setup()
{
  Serial.begin(9600);
 
  //Define the inputs and outputs
  pinMode(IRsensor,INPUT);
  pinMode(leftmotor,OUTPUT);
  pinMode(rightmotor,OUTPUT);

}

void loop()
{
  //Create variable 'val', and print sensor value into it
  int sensvalue = analogRead(IRsensor);
 
   // Update the current milli seconds. Millis() returns the number
  // of milli seconds that have ellapsed sincethe board was turned on
  currentMillis = millis();
 
  //Define the variosu cases depending on IR sensor input
  switch(state){
 
  case 0: //wait four seconds before moving
    previousMillis = currentMillis;
    interval = 4000;
    state = 1;
    break;
   
       
    case 1: // check if 4 sec has finished then move forwards.
    if(currentMillis - previousMillis > interval){
      Forwards(); // This function is defined at the bottom.
      state = 2;
    }
    break;
   
   
   
    case 2: // continue moving forward untill the FIRST DEAD END is close, then turn right
    if(sensvalue < 135 && state == 2){
      RightTurn(); // This function is defined at the bottom.
      previousMillis = currentMillis;
      interval = 435;
      state = 3;
    }
    break;
   
   
   
    case 3: //Keep moving after first turn
    if(currentMillis - previousMillis > interval && state == 3){
      Forwards(); // This function is defined at the bottom.
      flag = 1;
      state = 4;
    }
    break;
   
   
    case 4: // Keep moving forward till OBSTACLE, then turn Right
    if(sensvalue < 135 && flag == 1){
      RightTurn(); // This function is defined at the bottom.
      previousMillis = currentMillis;
      interval = 435;
      state = 5;
    }
   
    break;
   
    case 5: //Keep moving after 2nd turn
    if(currentMillis - previousMillis > interval && state == 5){
      Forwards(); // This function is defined at the bottom.
      flag = 2;
      state = 6;
    }
   
    break;
   
    case 6: // Keep moving forward till DED END, then turn Left
    if(sensvalue < 135 && flag == 2){
      LeftTurn(); // This function is defined at the bottom.
      previousMillis = currentMillis;
      interval = 435;
      state = 7;
    }
   
    break;
   
    case 7: //Keep moving after DEAD END
    if(currentMillis - previousMillis > interval && state == 7){
      Forwards(); // This function is defined at the bottom.
      flag = 3;
      state = 8;
      interval = 1500;
    }
   
    break;
   
    case 8: //Turn 15 degrees Left
    if(currentMillis - previousMillis > interval && flag == 8){
      LeftTurn(); // This function is defined at the bottom.
      flag = 4;
      state = 9;
      interval = 110;
    }
   
    break;
   
    case 9: //Keep moving after Turning 15 Degrees for 5 seconds
    if(currentMillis - previousMillis > interval && state == 9){
      Forwards(); // This function is defined at the bottom.
      flag = 5;
      state = 10;
      interval = 5000;
    }
   
    break;
   
    case 10: //Stop after 5 seconds
    if(currentMillis - previousMillis > interval && flag == 5){
      Stop(); // This function is defined at the bottom.
      flag = 6;
      state = 11;
      }
     
      break;
         
    default: // if no cases match then default is executed.
    //Do nothing
    break;
   
  }
 


// Motor control programs, depending on the case
void Forwards(){
  digitalWrite(leftmotor,HIGH);
  digitalWrite(rightmotor,HIGH);
}

void LeftTurn(){
  digitalWrite(leftmotor,LOW);
  digitalWrite(rightmotor,HIGH);
}

void RightTurn(){
  digitalWrite(leftmotor,HIGH);
  digitalWrite(rightmotor,LOW);
}

void Stop(){
  digitalWrite(leftmotor,LOW);
  digitalWrite(rightmotor,LOW);
}
 
5  Using Arduino / Project Guidance / Re: Motor control after making a turn on: May 30, 2012, 08:39:38 am
So then my program would run case 0, then halt? I thought it looped  and ran whichever case had the conditions satisfied?
How would i be able to make it run those steps in sequence?

Thanks again for the help guys  smiley
6  Using Arduino / Project Guidance / Re: Motor control after making a turn on: May 30, 2012, 04:24:29 am
my idea was to set a value to teh state variable, then use that as a condition for the next step. Im trying to get multiple conditions before each case is run e.g for case 2, it will only occur if the sensor value is below some value, and when state = 2, which is after case 1 has occured, thus forcing each step to happen in sequence.
Isnt the program run in a step structure, i.e. case 0, case 1, case 2, etc ? Im an undergrad in engineering so i've done some statement list programming on PLC's and i was trying to apply that to this.
7  Using Arduino / Project Guidance / Re: Motor control after making a turn on: May 29, 2012, 09:02:36 am
here is my program code so far. Does it seem correct? Note that i have purposely left the sensor values (sensvalue) blank in the if statements. The intervals are also not correctly timed so ignore the actual value


Code:
// Define the sensor and motors as variables
int IRsensor = 0;
int leftmotor = 1;
int rightmotor = 2;

// This variable holds the state the system is in. It will start at 0 and count upwards for
// how every many state the system will have.
int state = 0;
//Create Flag Variable for State
int flag = 0;

// This long variable will hold the previous number of milli seconds that have ellapsed since
// the board was turned on. It is used for timing delays.
long previousMillis;

// This long variable will hold the current amount of ellapsed millis seconds since the board was turned on
long currentMillis;

// This variable will hold the desired delay time in millis seconds
long interval;

void setup()
{
  Serial.begin(9600);
  
  //Define the inputs and outputs
  pinMode(IRsensor,INPUT);
  pinMode(leftmotor,OUTPUT);
  pinMode(rightmotor,OUTPUT);

}

void loop()
{
  //Create variable 'val', and print sensor value into it
  int sensval = analogRead(IRsensor);
  
   // Update the current milli seconds. Millis() returns the number
  // of milli seconds that have ellapsed sincethe board was turned on
  currentMillis = millis();
  
  //Define the variosu cases depending on IR sensor input
  switch(state)
  
  case 0: //wait two seconds before moving
    previousMillis = currentMillis;
    interval = 3000;
    state = 1;
    break;
    
        
    case 1: // check if wait time has finished then move forwards.
    if(currentMillis - previousMillis > interval){
      Forwards(); // This function is defined at the bottom.
      state = 2;
    }
    break;
    
    
    
    case 2: // continue moving forward untill the 1st wall is close, then turn right
    if(sensvalue < && state == 2){
      RightTurn(); // This function is defined at the bottom.
      previousMillis = currentMillis;
      interval = 3000;
      state = 3;
    }
    break;
    
    
    
    case 3: //Keep moving after first turn
    if(currentMillis - previousMillis > interval && state == 3){
      Forwards(); // This function is defined at the bottom.
      flag = 1
    }
    break;
    
    case 4: // Keep moving forward till 2nd wall, then turn Right
    if(sensvalue < && flag == 1){
      RightTurn(); // This function is defined at the bottom.
      previousMillis = currentMillis;
      interval = 3000;
      state = 4;
    }
    break;
    
    case 5: //Keep moving after 2nd turn
    if(currentMillis - previousMillis > interval && state == 4){
      Forwards(); // This function is defined at the bottom.
      flag = 2
    }
    break;
    
    case 6: // Keep moving forward till 3rd wall, then turn Left
    if(sensvalue < && flag == 2){
      LeftTurn(); // This function is defined at the bottom.
      previousMillis = currentMillis;
      interval = 3000;
      state = 5;
    }
    break;
    
    case 7: //Keep moving after 3rd turn
    if(currentMillis - previousMillis > interval && state == 5){
      Forwards(); // This function is defined at the bottom.
      flag = 2
    }
    break;
    
    default: // if no cases match then default is executed.
    //Do nothing
    break;
    
  }

  
}  

// Motor control programs, depending on the case
void Forwards(){
  digitalWrite(leftmotor,HIGH);
  digitalWrite(rightMotor,HIGH);
}

void LeftTurn(){
  digitalWrite(leftmotor,LOW);
  digitalWrite(rightmotor,HIGH);
}

void RightTurn(){
  digitalWrite(leftmotor,HIGH);
  digitalWrite(rightmotor,LOW);
}

void Stop(){
  digitalWrite(leftmotor,LOW);
  digitalWrite(rightmotor,LOW);
}


Moderator edit: [code] ... [/code] tags added. (Nick Gammon)
8  Using Arduino / Project Guidance / Motor control after making a turn on: May 29, 2012, 07:48:29 am
Hey guys hows it going!

so i've just started looking into arduino programming. Im trying to help my little brother with some programming for a school project but we've run into a dead end.
Basically the project requires you control a robot through an obstacle course. What sort of code would you need to run this?

Were using an IR sensor to measure distance (were only using the analog values so no need to convert it to physical distance) till the bot is close enough to a wall, then it turns right or left, and follows this procedure through a series of turns. Problem we have is, its a 2 wheeled bot, so i know how to make it stop and turn e.g. left by turning left motor off or reverse and right motor forwards, but then after that, how do you make it move both motors? I know it seems like a easy problem but i cant figure it out smiley-sad

Any help is appreciated smiley
Pages: [1]