#include <Servo.h>
Servo myservo1, myservo2, myservo3;
unsigned long trun1=1350;
unsigned long trun2=425;
unsigned long tback1=1350;
unsigned long tback2=425;
unsigned long tturn1=900;
unsigned long tturn2=735;
unsigned long trest=100;
unsigned long tclose=100;
unsigned long topen=100;
unsigned long treset=100;int leftwheel=11;
int rightwheel=10;
int claw=9;
void setup()
{
myservo1.attach(leftwheel);
myservo2.attach(rightwheel);
pinMode(leftwheel, OUTPUT);
pinMode(rightwheel, OUTPUT);
myservo3.attach(claw);
pinMode(claw,OUTPUT);
}
void armreset(unsigned long tym)
{
unsigned long tnow=millis();
while (millis()-tnow <= tym)
{
myservo3.write(0);
}
}
void armclose(unsigned long tym)
{
unsigned long tnow=millis();
while (millis()-tnow <= tym)
{
myservo3.write(180);
delay(750);
}
}
void armopen(unsigned long tym)
{
unsigned long tnow=millis();
while (millis()-tnow <= tym)
{
myservo3.write(0);
delay(500);
}
}
void forward(unsigned long tym)
{
unsigned long tnow=millis();
while (millis()-tnow <= tym)
{
myservo1.writeMicroseconds(1700);
myservo2.writeMicroseconds(1300);
}
}
void turn1(unsigned long tturn)
{
unsigned long next=millis();while (millis()-next <=tturn1)
{
myservo2.writeMicroseconds(1300);
}
}
void turn2(unsigned long tturn)
{
unsigned long next=millis();while (millis()-next <=tturn2)
{
myservo2.writeMicroseconds(1700);
}
}void rest(unsigned long tym)
{
unsigned long tnow;
tnow=millis();
while (millis() - tnow <= tym)
{
myservo1.writeMicroseconds(1500);
myservo2.writeMicroseconds(1500);}
}
void back(unsigned long tym)
{
unsigned long tnow=millis();
while (millis()-tnow <= tym)
{
myservo1.writeMicroseconds(1300);
myservo2.writeMicroseconds(1700);
}
}
void block()
{
while(millis() > 1)
{}
}
void loop()
{
armreset(treset);
rest(trest);
forward(trun1);
rest(trest);
armclose(tclose);
rest(trest);
turn1(tturn1);
rest(trest);
forward(trun2);
rest(trest);
armopen(topen);
rest(trest);
back(tback2);
rest(trest);
turn2(tturn2);
rest(trest);
back(tback1);
rest(trest);
block();
}
///Basic line tracker.
#include <Servo.h> // call the Arduino servo library
Servo leftWheel; //Create an object for the left motor of type "servo"
Servo rightWheel; //Create an object for the right motor of type "servo"
Servo gripper; //Create an object for the standard servo
int leftlite = A2; //Pin used for the left phototransistor
int rightlite = A1; //Pin used for the right phototransistor
int bumppin = 6; //pin used to light up the big LED
int led=8; // pin used for the LED that flashes at 18 inches
float sa=0; //storage variable a
float sb=0; //storage variable b
float litediff=0; //a variable for light difference
float leftliteval; //a variable for the left light phototransistor
float rightliteval; //a variable for the right phototransistor
int sensorPin = A3;
float dist = 0;
float s = 0;
void setup()
{
leftWheel.attach(12); //syntax for assigning object "leftWheel" to pin 7
rightWheel.attach(11); //Syntax for assigning object "rightWheel" to pin 4
gripper.attach(9); //Syntax for assigning the standard servo to pin 8
pinMode(bumppin,INPUT); // Assigning the biglight to be a digital output
//gripper.attach(9);
Serial.begin(9600); // COM speed setup for serial monitor
pinMode(led,OUTPUT);
}
float flipflopL()
{
sa = 0;
for(int i = 1; i <= 25; i++)
{
leftliteval = analogRead(leftlite);
sa = sa + leftliteval;
}
leftliteval = (sa/(1023*5));
return leftliteval;
}
float flipflopR()
{
sb = 0;
for(int i = 1; i <= 25; i++)
{
rightliteval = analogRead(rightlite);
sb = sb + rightliteval;
}
rightliteval = (sb/(1023*5));
return rightliteval;
}
void Blink()
{
digitalWrite(led,HIGH);
delay(500);
digitalWrite(led,LOW);
delay(1);
}
void Linetrack()
{
leftliteval = flipflopL();//convert to voltage
rightliteval = flipflopR(); // compute equivalent voltage
litediff=leftliteval-rightliteval; // Take the difference of the left and right phototransistors
Serial.print(leftliteval,4); //print the value of the left PT to the serial monitor
Serial.print(" "); //add a space
Serial.print(rightliteval,4); //Print value of right PT
Serial.print(" "); //add a space
Serial.println(litediff,4); // display the difference between the two PT
if (litediff >= 1)//for my set up when the right PT was on black I got a difference of about .080 volts so I picked .06 to be safe will need to tweak in the future
turnRight(1); //turn right if the right PT is on black
else if (litediff <= -2) //I got a value of 0.030 when the left PT was on black so I picked 0.04 this can be tweaked
turnLeft(1); //turn left if the left PT is on black
else //otherwise....
driveForward(1); //go forward
}
void driveForward(float delta)
{
leftWheel.writeMicroseconds(1700);
rightWheel.writeMicroseconds(1425);
delay (delta);
}
void driveBackward(float delta)
{
leftWheel.writeMicroseconds(1425);
rightWheel.writeMicroseconds(1700);
delay(delta);
}
void turnLeft(float delta)
{
rightWheel.writeMicroseconds(1300);
leftWheel.writeMicroseconds(1500);
delay(delta);
}
void turnRight(float delta)
{
leftWheel.writeMicroseconds(1700);
rightWheel.writeMicroseconds(1500);
delay(delta);
}
void halt(float delta) //
{
leftWheel.writeMicroseconds(1500);
rightWheel.writeMicroseconds(1500);
delay(delta);
}
boolean bumpstate()
{
return digitalRead(bumppin);
}
float getdist()
{ // repeatedly do the following:
s = 0; // initialize accumulator
for(int i = 1; i <= 200; i++) // get the sum of 50 samples
{
s = s + analogRead(sensorPin); //get bit-equivalent voltage value from input pin
}
s=s/200;
s = (s/1023)*5; // compute equivalent voltage
dist = pow(s, -1.154) * 11.44;
Serial.println(dist);
return dist;
}
void block()
{
while(1)
{
halt(1);
}
}
void start()
{
gripper.write(60);
}
void END()
{
while(millis() > 1)
{
}
}
void grasp()
{
gripper.write(0);
delay(500);
}
void relinquish()
{
gripper.write(60);
delay(1000);
grasp();
}
void forwardTrackDist(int delta)
{
while(bumpstate()==LOW && getdist() > delta)
{
Linetrack();
}
halt(1);
}
void forwardBump()
{
while(bumpstate()==LOW)
{
driveForward(1);
}
halt(1);
}
void loop()
{
start();
forwardBump();
grasp();
driveBackward(900);
turnLeft(600);// about 85 degrees
forwardTrackDist(4.2); //is this distance right?
relinquish();
driveBackward(550); //dont like hard numbers
turnLeft(1500); //dont like hard numbers
driveForward(600);
forwardTrackDist(6.2);//4.2 set as distance
Blink();
forwardTrackDist(4.2);
driveBackward(1500); // change time
turnLeft(2300); //90 degrees
driveForward(2500);
halt(99999);
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
///Basic line tracker.
#include <Servo.h> // call the Arduino servo library
Servo leftWheel; //Create an object for the left motor of type "servo"
Servo rightWheel; //Create an object for the right motor of type "servo"
Servo gripper; //Create an object for the standard servoint leftlite = A2; //Pin used for the left phototransistor
int rightlite = A1; //Pin used for the right phototransistor
int bumppin = 6; //pin used to light up the big LED
int led=8; // pin used for the LED that flashes at 18 inches
float sa=0; //storage variable a
float sb=0; //storage variable b
float litediff=0; //a variable for light difference
float leftliteval; //a variable for the left light phototransistor
float rightliteval; //a variable for the right phototransistor
int sensorPin = A3;
float dist = 0;
float s = 0;void setup()
{
leftWheel.attach(12); //syntax for assigning object "leftWheel" to pin 7
rightWheel.attach(11); //Syntax for assigning object "rightWheel" to pin 4
gripper.attach(9); //Syntax for assigning the standard servo to pin 8
pinMode(bumppin,INPUT); // Assigning the biglight to be a digital output
//gripper.attach(9);
Serial.begin(9600); // COM speed setup for serial monitor
pinMode(led,OUTPUT);
}float flipflopL()
{
sa = 0;
for(int i = 1; i <= 25; i++)
{
leftliteval = analogRead(leftlite);
sa = sa + leftliteval;
}
leftliteval = (sa/(1023*5));
return leftliteval;
}float flipflopR()
{
sb = 0;
for(int i = 1; i <= 25; i++)
{
rightliteval = analogRead(rightlite);sb = sb + rightliteval;
}
rightliteval = (sb/(1023*5));
return rightliteval;
}void Blink()
{
digitalWrite(led,HIGH);
delay(500);
digitalWrite(led,LOW);
delay(1);}
void Linetrack()
{
leftliteval = flipflopL();//convert to voltage
rightliteval = flipflopR(); // compute equivalent voltage
litediff=leftliteval-rightliteval; // Take the difference of the left and right phototransistors
Serial.print(leftliteval,4); //print the value of the left PT to the serial monitor
Serial.print(" "); //add a space
Serial.print(rightliteval,4); //Print value of right PT
Serial.print(" "); //add a space
Serial.println(litediff,4); // display the difference between the two PT
if (litediff >= 1)//for my set up when the right PT was on black I got a difference of about .080 volts so I picked .06 to be safe will need to tweak in the future
turnRight(1); //turn right if the right PT is on black
else if (litediff <= -2) //I got a value of 0.030 when the left PT was on black so I picked 0.04 this can be tweaked
turnLeft(1); //turn left if the left PT is on black
else //otherwise....
driveForward(1); //go forward
}void driveForward(float delta)
{
leftWheel.writeMicroseconds(1700);
rightWheel.writeMicroseconds(1425);
delay (delta);
}void driveBackward(float delta)
{
leftWheel.writeMicroseconds(1425);
rightWheel.writeMicroseconds(1700);
delay(delta);}
void turnLeft(float delta)
{
rightWheel.writeMicroseconds(1300);
leftWheel.writeMicroseconds(1500);
delay(delta);
}void turnRight(float delta)
{
leftWheel.writeMicroseconds(1700);
rightWheel.writeMicroseconds(1500);
delay(delta);
}void halt(float delta) //
{
leftWheel.writeMicroseconds(1500);
rightWheel.writeMicroseconds(1500);
delay(delta);
}boolean bumpstate()
{
return digitalRead(bumppin);
}float getdist()
{ // repeatedly do the following:
s = 0; // initialize accumulator
for(int i = 1; i <= 200; i++) // get the sum of 50 samples
{
s = s + analogRead(sensorPin); //get bit-equivalent voltage value from input pin
}
s=s/200;
s = (s/1023)*5; // compute equivalent voltage
dist = pow(s, -1.154) * 11.44;
Serial.println(dist);
return dist;
}void block()
{
while(1)
{
halt(1);
}
}void start()
{
gripper.write(60);
}
void END()
{
while(millis() > 1)
{}
}
void grasp()
{
gripper.write(0);
delay(500);
}void relinquish()
{
gripper.write(60);
delay(1000);
grasp();
}void forwardTrackDist(int delta)
{
while(bumpstate()==LOW && getdist() > delta)
{
Linetrack();}
halt(1);
}void forwardBump()
{
while(bumpstate()==LOW)
{
driveForward(1);}
halt(1);
}void loop()
{
start();
forwardBump();
grasp();
driveBackward(900);
turnLeft(600);// about 85 degrees
forwardTrackDist(4.2); //is this distance right?
relinquish();
driveBackward(550); //dont like hard numbers
turnLeft(1500); //dont like hard numbers
driveForward(600);
forwardTrackDist(6.2);//4.2 set as distance
Blink();
forwardTrackDist(4.2);
driveBackward(1500); // change time
turnLeft(2300); //90 degrees
driveForward(2500);
halt(99999);
@dougcastellano...
Do not cross-post again.
Please do not use copy-for-forum.
Please use
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags when posting code.