Robot freaking out about new code???
hey guys, robot walbot started out using 1 ultrasonic sensor navigate rooms, tried add 2 sharp ir sensors, , updated code work them if senses object on right turn left , vice versa, if 3 sensors blocked turn around. have gotten components work (i have used sharp ir sensors , give me correct values back) when put started getting bunch of whack false readings , became paralyzed. have gone on code couple of times see might causing react strangely can't seem find problem is. appreciated.
code: [select]
int motor1a = 10; // h-bridge input 1
int motor1b = 11; // h-bridge input 2
int enable = 3; // h-bridge motor 1 enable
int motor2a = 9; // h-bridge input 3
int motor2b = 8; // h-bridge input 4
int enable2 = 5; // h-bridge motor 2 enable
int infraan1 = 1; // select input pin infrared sensor
int infraan2 = 2; // select input pin infrared sensor
float infradist1 = 0; // variable recieves raw infrared analog data
float infradist2 = 0; // variable recieves raw infrared analog data
int ultraan = 0; // select input pin ultrasonic sensor
int ultraenable = 12; // sets enable pin 12
int ledpin = 13; // sets indicator led pin 13
float val[10]; // array store first 10 values coming sensor
float valsum=0; // sum of first 10 readings
float valavg=0; // average of first 10 readings
float dist = 0; // variable store converted distance in inches
void straight() // go straight function
{
digitalwrite(enable, high);
digitalwrite(motor1a, high);
digitalwrite(motor1b,low);
digitalwrite(enable2, high);
digitalwrite(motor2a, high);
digitalwrite(motor2b,low);
}
void turnright() // turn right function
{
digitalwrite(enable, high);
digitalwrite(motor1a, low);
digitalwrite(motor1b,high);
digitalwrite(enable2, high);
digitalwrite(motor2a, high);
digitalwrite(motor2b,low);
}
void turnleft() // turn right function
{
digitalwrite(enable, high);
digitalwrite(motor1a, high);
digitalwrite(motor1b,low);
digitalwrite(enable2, high);
digitalwrite(motor2a, low);
digitalwrite(motor2b,high);
}
float distcalc() // distance calculating function converts analog input inches
{
valsum = 0; // resets valsum 0 after every loop
for (int i=0; i<=9; i++) // loop gather first 10 int's
{
val[i] = analogread(ultraan); // read value sensor
valsum = (val[i]+valsum); // adds first 10 values averaging
}
digitalwrite(ultraenable, low); // disables sonar
valavg = (valsum/10); // calculates average
dist = ((valavg/204.8)/.009765625); // converts analog value distance in inches
return dist; // return distance values rest of program
}
void avoidewalls()
{
infradist1 = analogread(infraan1); // read value sensor
infradist2 = analogread(infraan2); // read value sensor
if(dist < 16) // if distance less 20 inches
{
digitalwrite(ledpin, high); // turn on led
turnright(); // turns walbot right using turnright function
delay(100);
}
else if(dist < 16 && infradist1 > 220) // if distance less 20 inches
{
digitalwrite(ledpin, high); // turn on led
turnright(); // turns walbot right using turnright function
delay(100);
}
else if(dist < 16 && infradist2 > 220) // if distance less 20 inches
{
digitalwrite(ledpin, high); // turn on led
turnleft(); // turns walbot right using turnright function
delay(100);
}
else if(dist < 16 && infradist2 > 220 && infradist1 > 220) // if distance less 20 inches
{
digitalwrite(ledpin, high); // turn on led
turnleft(); // turns walbot around using turnright function
delay(175); //delay longer turns 180 degrees
}
else // otherwise
{
digitalwrite(ledpin, low); // turn off led
straight(); // go straight using straight function
}
}
void setup()
{
pinmode(motor1a, output); // sets motor pin 1a output
pinmode(motor1b, output); // sets motor pin 1b output
pinmode(motor2a, output); // sets motor pin 2a output
pinmode(motor2b, output); // sets motor pin 2b output
pinmode(enable, output); // sets enable output
pinmode(enable2, output); // sets enable2 output
pinmode(ultraenable, output); // sets ultraenable output
pinmode(ledpin, output); // sets ledpin output
straight(); // initializes walbot go straight
}
void loop() {
digitalwrite(ultraenable, high); // turns on sonar
distcalc(); // gets distance value distcalc function
avoidewalls(); // tells walbot not run inanimate objects calling avoidewalls function
delay(20); // delay 20 milliseconds
}
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Robot freaking out about new code???
arduino
Comments
Post a Comment