I'm building a sumobot that with an IR proximity sensor and line tracing sensor.
This is my the first program to test the proximity sensor. I'm having difficulties with it, whenever the sensor detects any object, it doesn't ATTACKS but finishes hunt. I tried doing the turnLeft only without the delay code and it instantly ATTACK whenever it detects objects. I need a code to make the robot turn/stop for a certain time without using delay syntax.
#include <Servo.h>
/* create servo object to control a servo
a maximum of eight servo objects can be created */
Servo servoR;
Servo servoL;
int pos = 90, // variable to store the servo position
IRpin = 2, //digital pin 2
IR;
void setup()
{
pinMode(IRpin, INPUT);
servoR.attach(9); // attaches the servo on pin 9 to the servo object
servoL.attach(10); //attaches the servo on pin 10 to the servo object
}
void loop()
{
if (int sensor = digitalRead(IRpin))
{
//HUNTS OPPONENTS
sensor == LOW;
turnRight();
turnLeft();
stopBot();
turnLeft();
turnRight();
} else {
//ATTACKS
forward();
}
}
void forward()
{
servoR.write(80);
servoL.write(100);
}
void turnRight()
{
servoR.write(95);
servoL.write(95);
delay (2000); //turns right for 2 secs
}
void turnLeft()
{
servoR.write(85);
servoL.write(85);
delay(2000); //turns left for 2 secs
}
void stopBot()
{
servoR.write(90);
servoL.write(90);
delay(2000); //stops the robot for 2 seconds
}
^I'm sorry about that. I am very new to this. Tried programming for arduino because of a project at school. Can you suggest me any alternate code for what I am trying to aim for?
Generally, when one performs an equality test, one cares about the result.
can you explain what did I do wrong? Sorry about that.
This is one of those times where it does compile, even though it's wrong.
int pos = 90, // variable to store the servo position
IRsensor = digitalRead(2), //proximity sensor digital pin 2
LTsensor = digitalRead(4); //line tracer digital pin 4
As the other guys indicated, use of delay(2000) is not very efficient. The
cpu does NOTHING "useful" for 2 seconds, just wastes cycles, so it will not
read your IR inputs or do anything else during that time.
Do a search on "blink without delay" threads to see how to not lock up the
cpu while you're waiting for something to occur.
So if I remove delay, how can I rotate the bot clockwise then counter clockwise for 2 seconds each? Sorry for so many questions but I can't figure out blink without delay and how can I apply it with MY delay. Sorry.
I forgot to say that this is a modified servo, the one where the locking mechanism is manually removed so that it can rotate continuously.
So if I remove delay, how can I rotate the bot clockwise then counter clockwise for 2 seconds each?
You really, really need to understand the blink without delay example.
It's only got about four significant lines of code.
Think about what it is doing, transitioning the state of the LED, and going off and potentially doing something else.
Well, transitioning a LED is a bit like starting a motor.
Can you really sense an Obstacle Avoidance? Just what the heck IS an Obstacle Avoidance?
The sensors detect obstacles, and the information is used to avoid obstacles.
There are something like 723541864864864864 examples on the internet of using the sensors to avoid obstacles. Surely one of them could provide you with a clue or two.