This is my first Arduino project, it is an insect robot from the book "make Arduino bots and gadgets and i am relatively new to raw code. I completed it and wanted to add a feature so the bot shys from the light, i think via a light sensor. I have no idea on how to incorporate the sensor and affect the bots actions coding wise. The full code is below
/
/ walkerForwardComplete.pde - Two servo walker.
// Complete code with obstacle avoidance
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
// Updated by Joe Saavedra, 2010
#include <Servo.h>
Servo frontServo;
Servo rearServo;
/* Servo motors - global variables */
int centerPos = 90;
int frontRightUp = 72;
int frontLeftUp = 108;
int backRightForward = 75;
int backLeftForward = 105;
int walkSpeed = 150; // How long to wait between steps in milliseconds
int centerTurnPos = 81;
int frontTurnRightUp = 63;
int frontTurnLeftUp = 117;
int backTurnRightForward = 66;
int backTurnLeftForward = 96;
/* Ping distance measurement - global variables */
int pingPin = 4;
long int duration, distanceInches;
long distanceFront=0; //cm
int startAvoidanceDistance=20; //cm
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
long distanceCm(){
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
distanceInches = microsecondsToInches(duration);
return microsecondsToCentimeters(duration);
}
void center()
{
frontServo.write(centerPos);
rearServo.write(centerPos);
}
void moveForward()
{
frontServo.write(frontRightUp);
rearServo.write(backLeftForward);
delay(125);
frontServo.write(centerPos);
rearServo.write(centerPos);
delay(65);
frontServo.write(frontLeftUp);
rearServo.write(backRightForward);
delay(125);
frontServo.write(centerPos);
rearServo.write(centerPos);
delay(65);
}
void moveBackRight()
{
frontServo.write(frontRightUp);
rearServo.write(backRightForward-6);
delay(125);
frontServo.write(centerPos);
rearServo.write(centerPos-6);
delay(65);
frontServo.write(frontLeftUp+9);
rearServo.write(backLeftForward-6);
delay(125);
frontServo.write(centerPos);
rearServo.write(centerPos);
delay(65);
}
void moveTurnLeft()
{
frontServo.write(frontTurnRightUp);
rearServo.write(backTurnLeftForward);
delay(125);
frontServo.write(centerTurnPos);
rearServo.write(centerTurnPos);
delay(65);
frontServo.write(frontTurnLeftUp);
rearServo.write(backTurnRightForward);
delay(125);
frontServo.write(centerTurnPos);
rearServo.write(centerTurnPos);
delay(65);
}
void setup()
{
frontServo.attach(2);
rearServo.attach(3);
pinMode(pingPin, OUTPUT);
}
void loop()
{
distanceFront=distanceCm();
if (distanceFront > 1){ // Filters out any stray 0.00 error readings
if (distanceFront<startAvoidanceDistance) {
for(int i=0; i<=8; i++) {
moveBackRight();
delay(walkSpeed);
}
for(int i=0; i<=10; i++) {
moveTurnLeft();
delay(walkSpeed);
}
} else {
moveForward();
delay(walkSpeed);
}
}
}
Please can you suggest what code to add to the sketch and what light sensor to use( i live in the UK so no paid deliveries from the us if possible)
Thanks
Daniel
A simple Light Dependent Resistor may do the job.
From Ebay they are cheap. From Maplin not so cheap. You will also need some 1K resistors to make the other half of the voltage divider to connect to arduino analogue pin.
danield123--:
Thanks for the link.
q1 why analogue pin not a digital one
You can get digital light detectors but how do you determine the light source with only yes/now answers (digital).
q2 what would the additional code look like
if (analogRead(A0) > threshold) {
// It's bright
}
else {
//it's dark
}
threshold would be a value you define or it could be the last reading you took so you know if it's getting brighter or darker.
q3 why do you need a 1k resistor
Arduino analogue input measures voltage not current so you need to set it up as a [voltage divider](http://en.wikipedia.org/wiki/Voltage_divider) to detect changes.
How do you define the threshold and and if light was light how would i get the servos pointing in that direction.Or the opposite for dark.
Thanks
d
The whole code
// walkerForwardComplete.pde - Two servo walker.
// Complete code with obstacle avoidance
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
// Updated by Joe Saavedra, 2010
#include <Servo.h>
Servo frontServo;
Servo rearServo;
/* Servo motors - global variables */
int centerPos = 90;
int frontRightUp = 72;
int frontLeftUp = 108;
int backRightForward = 75;
int backLeftForward = 105;
int walkSpeed = 150; // How long to wait between steps in milliseconds
int centerTurnPos = 81;
int frontTurnRightUp = 63;
int frontTurnLeftUp = 117;
int backTurnRightForward = 66;
int backTurnLeftForward = 96;
int BatteryCHECK;
/* Ping distance measurement - global variables */
int pingPin = 4;
long int duration, distanceInches;
long distanceFront=0; //cm
int startAvoidanceDistance=20; //cm
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
long distanceCm(){
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
distanceInches = microsecondsToInches(duration);
return microsecondsToCentimeters(duration);
}
void center()
{
frontServo.write(centerPos);
rearServo.write(centerPos);
}
void moveForward()
{
frontServo.write(frontRightUp);
rearServo.write(backLeftForward);
delay(125);
frontServo.write(centerPos);
rearServo.write(centerPos);
delay(65);
frontServo.write(frontLeftUp);
rearServo.write(backRightForward);
delay(125);
frontServo.write(centerPos);
rearServo.write(centerPos);
delay(65);
}
void moveBackRight()
{
frontServo.write(frontRightUp);
rearServo.write(backRightForward-6);
delay(125);
frontServo.write(centerPos);
rearServo.write(centerPos-6);
delay(65);
frontServo.write(frontLeftUp+9);
rearServo.write(backLeftForward-6);
delay(125);
frontServo.write(centerPos);
rearServo.write(centerPos);
delay(65);
}
void moveTurnLeft()
{
frontServo.write(frontTurnRightUp);
rearServo.write(backTurnLeftForward);
delay(125);
frontServo.write(centerTurnPos);
rearServo.write(centerTurnPos);
delay(65);
frontServo.write(frontTurnLeftUp);
rearServo.write(backTurnRightForward);
delay(125);
frontServo.write(centerTurnPos);
rearServo.write(centerTurnPos);
delay(65);
}
void setup()
{
frontServo.attach(2);
rearServo.attach(3);
pinMode(pingPin, OUTPUT);
}
void loop()
{
distanceFront=distanceCm();
if (distanceFront > 1){ // Filters out any stray 0.00 error readings
if (distanceFront<startAvoidanceDistance) {
for(int i=0; i<=8; i++) {
moveBackRight();
delay(walkSpeed);
}
for(int i=0; i<=10; i++) {
moveTurnLeft();
delay(walkSpeed);
}
} else {
moveForward();
delay(walkSpeed);
}
}
}
danield123--:
How do you define the threshold and and if light was light how would i get the servos pointing in that direction.Or the opposite for dark.
I suspect from your questions that you have not written your insect robot code yourself.
You should study several of the examples that come with the Arduino IDE to get a good idea of what you can do with short pieces of code.
Rather than try to insert new stuff that you are unfamiliar with into a complex existing program you should first figure out how to write short sketches that ( 1 ) move a servo the way you want ( 2 ) read data from a light dependent resistor ( 3 ) use the data from the LDR to move the servo.
When you can do all that it will be time to consider how to make changes to your robot code.
And, just to reassure you, this step by step approach is how any experienced programmer would approach something new.