I am new here but I have been an Arduino nerd for about two years.
I like information on upgrading a robot.
You see I built that robot from the book called Arduino Bots and Gadgets, the insect bot with the wire hanger legs. Well I have it working but I want to add a touch switch to the shell I am building. I know the basic method of connecting the switch to Analog pin AO and using that. It's the coding I am having problems with. You see I want the robot to stop when this button is pressed. I have an idea for how this would be done. The servos would be centered to simulate the robot knowing it's being touched. I've tried this code I added to the bottom but the servos won't center.
This is the robot from the book
Here is the code with the hack
// Walker main programming
// Walker will use ping sensor for obstacle advoidance.
// Dennis Toy Jr 2011
// Will mod program for future upgrades like side touch sensors and
// infrated sensor
#include <Servo.h>
Servo frontServo;
Servo rearServo;
const int analogInPin = A0;
int sensorValue = 0;
int centerPos = 90;
int frontRightUp = 72;
int frontLeftUp = 108;
int backRightForward = 75;
int backLeftForward = 105;
int walkSpeed = 150; // how long to wait between steps
int centerTurnPos = 81;
int frontTurnRightUp = 63;
int frontTurnLeftUp = 117;
int backTurnRightForward = 66;
int backTurnLeftForward = 96;
// Ping Distance Measure
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(centerPos);
rearServo.write(centerPos-6);
delay(65);
frontServo.write(frontLeftUp-+9);
rearServo.write(backLeftForward-6);
delay(125);
frontServo.write(centerTurnPos);
rearServo.write(centerTurnPos);
delay(65);
}
void setup()
{
frontServo.attach(2);
rearServo.attach(3);
pinMode(pingPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
distanceFront=distanceCm();
if(distanceFront > 1){
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);
}
}
{
sensorValue = analogRead(analogInPin);
frontServo.write(centerPos);
rearServo.write(centerPos);
delay(100);
}
}
Moderator edit: CODE TAGS.
Any ideas on how this would be done.

