Go Down

Topic: Hello, I am new here. I want to add switch to stop robot (Read 1 time) previous topic - next topic

DHTRoboticsNerd

Feb 14, 2012, 08:08 pm Last Edit: Feb 15, 2012, 01:30 pm by AWOL Reason: 1
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

Code: [Select]
// 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.

PaulS

Quote
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.

which you read here:
Code: [Select]
     sensorValue = analogRead(analogInPin);
Now, you need an if statement. If the value is above some value, do one thing (could be nothing). If it's below that value, do something else (could also be nothing).

In one case, that something could be an infinite loop that stops anything else from happening, until the reset button is pressed.
Code: [Select]
while(1)
{
}


You really need to go through loop(), putting each { on it's own line. The use Tools + Auto Format to reformat the code. Next, you should get rid of the useless { and } that you have in the code. It will be a good opportunity to learn which ones are really needed.

Go Up