Pages: [1]   Go Down
Author Topic: Robot Programming Problems!  (Read 259 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm rather new at Arduino programming and am having problems with my code... The goal of the program is to immediately run a "standby" function that waits for the "Power" command from an IR Remote. It then calls a "Startup" function to test all sensors, motors, and such. However for some reason or another my function call to the startup function in the standby function is completely overlooked by the program. The next line prints text to the serial monitor and that works correctly. Also, when I bypass the standby function, the startup function only half works, it only operates the right motor and only moves the servo holding a range sensor to the right. I'm fairly certain this is a programming error and not a physical (Wiring) error but I can't be sure! The code has no compiler errors. Any advice you have would be much appreciated!

I'm running Arduino 1.05
Arduino Uno with:
Adafruit Motor Shield on seperate power source
2 DC Motors
1 5v Servo with 5v Ultrasonic Sensor mounted
1 IR Reciever

Please disregard all empty functions that have not been coded, thanks!

Code:
#include <AFMotor.h>
#include <IRremote.h>
#include <Servo.h>
AF_DCMotor left(2);
AF_DCMotor right(3);
#define RIGHTSPEED 180
#define LEFTSPEED 150
const int rangepin = 7;

#define MOTOR1_PWM 10  // Setup Servo
Servo servo_1;           
const int lookingleft = 45;
const int lookingright = 135;
const int lookingforward = 90;

int irpin = 6;              //IR Reciever Setup
IRrecv irrecv(irpin);
decode_results results;
#define POWER 0x10EFD827


void setup()
{
Serial.begin(9600);
Serial.println("Rover 1.0 Starting Up!");

right.setSpeed(RIGHTSPEED);   //Motor Setup
left.setSpeed(LEFTSPEED);

irrecv.enableIRIn();           //IR Reciever Start
irrecv.blink13(true);

servo_1.attach(MOTOR1_PWM);    //Attaches servo to Pin 10

standby();
}

void loop()
{

startup();
delay(1000);
}



void standby()                          //Waits For Command
{
if (irrecv.decode(&results))
{
  if (results.value == POWER)
  {
  startup();                            //SKIPS THIS FUNCTION CALL
  Serial.println("Powering Up!");        //This line executed normally
  }
irrecv.resume();
}
}




void startup()                            //Only runs right motor and turns servo to right
{
servo_1.write(lookingleft);
delay(100);
servo_1.write(lookingright);
delay(100);
servo_1.write(lookingforward);
delay(50);
left.run(FORWARD);
right.run(BACKWARD);
delay(150);
left.run(BACKWARD);
right.run(FORWARD);
delay(150);
left.run(RELEASE);
right.run(RELEASE);

}

void forward()
{




}



void frontdistance()
{
long duration, cm;
pinMode(rangepin, OUTPUT);
digitalWrite(rangepin,LOW);
delayMicroseconds(2);
digitalWrite(rangepin, HIGH);
delayMicroseconds(5);
digitalWrite(rangepin, LOW);
pinMode(rangepin, INPUT);
duration = pulseIn(rangepin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.println("cm in front");
}


long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}


void remotecontrol()
{
 
 
 
}


void turnleft()
{
 
 
}


void turnright()
{
 
 
}
Logged

London
Offline Offline
Edison Member
*
Karma: 23
Posts: 1062
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try calling standby() from inside loop() instead of from setup().
Does it work now?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, that's how I "bypassed" the standby function. I just can't figure out why it wont work within the loop but not the standby...
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3467
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The goal of the program is to immediately run a "standby" function that waits for the "Power" command from an IR Remote.
What makes the standby function wait for the POWER command ?

As it stands, if there is no IR command the function will exit immediately and if there is a command and it is not POWER then it will exit immediately.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

London
Offline Offline
Edison Member
*
Karma: 23
Posts: 1062
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, that's how I "bypassed" the standby function. I just can't figure out why it wont work within the loop but not the standby...

If standby works within loop() then try:
Code:
int state = 0; //0 = power off, 1 = power on (put this before setup())
while (results.value != POWER && state ==0){
//waits for POWER button to be pressed
//keep reading input
}
state = 1;
//do your stuff if POWER button pressed
//change state back to 0 to go back into standby.
Logged

Pages: [1]   Go Up
Jump to: