*Office Supplies HEXAPOD Code: Difference between revisions
Jump to navigation
Jump to search
Created page with "#include <Servo.h> #define MAX_DEGREE 145 #define MIN_DEGREE 35 #define MID_DEGREE 90 #define BASIC_DELAY 250 Servo FL; // create servo object to control a servo Servo FR..." |
No edit summary |
||
| Line 1: | Line 1: | ||
#include <Servo.h> | #include <Servo.h> | ||
#define MAX_DEGREE 145 | #define MAX_DEGREE 145 | ||
#define MIN_DEGREE 35 | #define MIN_DEGREE 35 | ||
#define MID_DEGREE 90 | #define MID_DEGREE 90 | ||
#define BASIC_DELAY 250 | #define BASIC_DELAY 250 | ||
Servo FL; // create servo object to control a servo | Servo FL; // create servo object to control a servo | ||
Servo FR; | Servo FR; | ||
Servo ML; | Servo ML; | ||
Servo MR; | Servo MR; | ||
Servo RL; | Servo RL; | ||
Servo RR; | Servo RR; | ||
void setup() | void setup() | ||
{ | { | ||
// put your setup code here, to run once: | // put your setup code here, to run once: | ||
FL.attach(3); | FL.attach(3); | ||
| Line 23: | Line 23: | ||
RL.attach(10); | RL.attach(10); | ||
RR.attach(11); | RR.attach(11); | ||
} | } | ||
void Stand() | void Stand() | ||
{ | { | ||
FL.write(MID_DEGREE); | FL.write(MID_DEGREE); | ||
FR.write(MID_DEGREE); | FR.write(MID_DEGREE); | ||
| Line 33: | Line 33: | ||
RL.write(MID_DEGREE); | RL.write(MID_DEGREE); | ||
RR.write(MID_DEGREE); | RR.write(MID_DEGREE); | ||
} | } | ||
void Left_Position_Forward() | void Left_Position_Forward() | ||
{ | { | ||
FL.write(MIN_DEGREE); | FL.write(MIN_DEGREE); | ||
RL.write(MIN_DEGREE); | RL.write(MIN_DEGREE); | ||
} | } | ||
void Left_Position_Backward() | void Left_Position_Backward() | ||
{ | { | ||
FL.write(MAX_DEGREE); | FL.write(MAX_DEGREE); | ||
RL.write(MAX_DEGREE); | RL.write(MAX_DEGREE); | ||
} | } | ||
void Left_Position_Outward() | void Left_Position_Outward() | ||
{ | { | ||
FL.write(MIN_DEGREE); | FL.write(MIN_DEGREE); | ||
RL.write(MAX_DEGREE); | RL.write(MAX_DEGREE); | ||
} | } | ||
void Left_Position_Inward() | void Left_Position_Inward() | ||
{ | { | ||
FL.write(MAX_DEGREE); | FL.write(MAX_DEGREE); | ||
RL.write(MIN_DEGREE); | RL.write(MIN_DEGREE); | ||
} | } | ||
void Left_Position_Stand() | void Left_Position_Stand() | ||
{ | { | ||
FL.write(MID_DEGREE); | FL.write(MID_DEGREE); | ||
RL.write(MID_DEGREE); | RL.write(MID_DEGREE); | ||
} | } | ||
void Right_Position_Forward() | void Right_Position_Forward() | ||
{ | { | ||
FR.write(MAX_DEGREE); | FR.write(MAX_DEGREE); | ||
RR.write(MAX_DEGREE); | RR.write(MAX_DEGREE); | ||
} | } | ||
void Right_Position_Backward() | void Right_Position_Backward() | ||
{ | { | ||
FR.write(MIN_DEGREE); | FR.write(MIN_DEGREE); | ||
RR.write(MIN_DEGREE); | RR.write(MIN_DEGREE); | ||
} | } | ||
void Right_Position_Outward() | void Right_Position_Outward() | ||
{ | { | ||
FR.write(MAX_DEGREE); | FR.write(MAX_DEGREE); | ||
RR.write(MIN_DEGREE); | RR.write(MIN_DEGREE); | ||
} | } | ||
void Right_Position_Inward() | void Right_Position_Inward() | ||
{ | { | ||
FR.write(MIN_DEGREE); | FR.write(MIN_DEGREE); | ||
RR.write(MAX_DEGREE); | RR.write(MAX_DEGREE); | ||
} | } | ||
void Right_Position_Stand() | void Right_Position_Stand() | ||
{ | { | ||
FR.write(MID_DEGREE); | FR.write(MID_DEGREE); | ||
RR.write(MID_DEGREE); | RR.write(MID_DEGREE); | ||
} | } | ||
void Middle_Position_Left() | void Middle_Position_Left() | ||
{ | { | ||
ML.write(MAX_DEGREE); | ML.write(MAX_DEGREE); | ||
MR.write(MAX_DEGREE); | MR.write(MAX_DEGREE); | ||
} | } | ||
void Middle_Position_Right() | void Middle_Position_Right() | ||
{ | { | ||
ML.write(MIN_DEGREE); | ML.write(MIN_DEGREE); | ||
MR.write(MIN_DEGREE); | MR.write(MIN_DEGREE); | ||
} | } | ||
void Middle_Position_Stand() | void Middle_Position_Stand() | ||
{ | { | ||
ML.write(MID_DEGREE); | ML.write(MID_DEGREE); | ||
MR.write(MID_DEGREE); | MR.write(MID_DEGREE); | ||
} | } | ||
void Middle_Position_Outward() | void Middle_Position_Outward() | ||
{ | { | ||
ML.write(MAX_DEGREE); | ML.write(MAX_DEGREE); | ||
MR.write(MIN_DEGREE); | MR.write(MIN_DEGREE); | ||
} | } | ||
void Middle_Position_Inward() | void Middle_Position_Inward() | ||
{ | { | ||
ML.write(MIN_DEGREE); | ML.write(MIN_DEGREE); | ||
MR.write(MAX_DEGREE); | MR.write(MAX_DEGREE); | ||
} | } | ||
void Middle_Lean_Left() | void Middle_Lean_Left() | ||
{ | { | ||
ML.write(MIN_DEGREE); | ML.write(MIN_DEGREE); | ||
MR.write(MID_DEGREE); | MR.write(MID_DEGREE); | ||
Left_Position_Outward(); | Left_Position_Outward(); | ||
} | } | ||
void Middle_Lean_Right() | void Middle_Lean_Right() | ||
{ | { | ||
ML.write(MID_DEGREE); | ML.write(MID_DEGREE); | ||
MR.write(MAX_DEGREE); | MR.write(MAX_DEGREE); | ||
Right_Position_Outward(); | Right_Position_Outward(); | ||
} | } | ||
void Step_Forward() | void Step_Forward() | ||
{ | { | ||
Stand(); | Stand(); | ||
delay(BASIC_DELAY); | delay(BASIC_DELAY); | ||
| Line 164: | Line 164: | ||
Right_Position_Stand(); | Right_Position_Stand(); | ||
delay(BASIC_DELAY); | delay(BASIC_DELAY); | ||
} | } | ||
void Step_Backward() | void Step_Backward() | ||
{ | { | ||
Stand(); | Stand(); | ||
delay(BASIC_DELAY); | delay(BASIC_DELAY); | ||
| Line 190: | Line 190: | ||
Right_Position_Stand(); | Right_Position_Stand(); | ||
delay(BASIC_DELAY); | delay(BASIC_DELAY); | ||
} | } | ||
void Step_Left() | void Step_Left() | ||
{ | { | ||
Stand(); | Stand(); | ||
delay(BASIC_DELAY); | delay(BASIC_DELAY); | ||
| Line 207: | Line 207: | ||
delay(BASIC_DELAY); | delay(BASIC_DELAY); | ||
} | } | ||
void Step_Right() | void Step_Right() | ||
{ | { | ||
Stand(); | Stand(); | ||
delay(BASIC_DELAY); | delay(BASIC_DELAY); | ||
| Line 224: | Line 224: | ||
delay(BASIC_DELAY); | delay(BASIC_DELAY); | ||
} | } | ||
int Left_Sensor; | int Left_Sensor; | ||
| Line 231: | Line 231: | ||
int Rear_Sensor; | int Rear_Sensor; | ||
void Set_Sensors() | void Set_Sensors() | ||
{ | { | ||
Left_Sensor = analogRead(2); | Left_Sensor = analogRead(2); | ||
Right_Sensor = analogRead(1); | Right_Sensor = analogRead(1); | ||
Front_Sensor = analogRead(3); | Front_Sensor = analogRead(3); | ||
Rear_Sensor = analogRead(0); | Rear_Sensor = analogRead(0); | ||
} | } | ||
int Sensor_Approximation(int S, int V) | int Sensor_Approximation(int S, int V) | ||
{ | { | ||
if (V > S * 1.2) | if (V > S * 1.2) | ||
return (1); | return (1); | ||
| Line 246: | Line 246: | ||
return(0); | return(0); | ||
return(0); | return(0); | ||
} | } | ||
int Dark_Direction() | int Dark_Direction() | ||
{ | { | ||
} | } | ||
void loop() | void loop() | ||
{ | { | ||
int x; | int x; | ||
| Line 297: | Line 297: | ||
// put your main code here, to run repeatedly: | // put your main code here, to run repeatedly: | ||
} | } | ||
Revision as of 21:32, 27 March 2016
#include <Servo.h>
#define MAX_DEGREE 145 #define MIN_DEGREE 35 #define MID_DEGREE 90 #define BASIC_DELAY 250
Servo FL; // create servo object to control a servo Servo FR; Servo ML; Servo MR; Servo RL; Servo RR;
void setup()
{
// put your setup code here, to run once:
FL.attach(3);
FR.attach(5);
ML.attach(6);
MR.attach(9);
RL.attach(10);
RR.attach(11);
}
void Stand()
{
FL.write(MID_DEGREE);
FR.write(MID_DEGREE);
ML.write(MID_DEGREE);
MR.write(MID_DEGREE);
RL.write(MID_DEGREE);
RR.write(MID_DEGREE);
}
void Left_Position_Forward()
{
FL.write(MIN_DEGREE);
RL.write(MIN_DEGREE);
}
void Left_Position_Backward()
{
FL.write(MAX_DEGREE);
RL.write(MAX_DEGREE);
}
void Left_Position_Outward()
{
FL.write(MIN_DEGREE);
RL.write(MAX_DEGREE);
}
void Left_Position_Inward()
{
FL.write(MAX_DEGREE);
RL.write(MIN_DEGREE);
}
void Left_Position_Stand()
{
FL.write(MID_DEGREE);
RL.write(MID_DEGREE);
}
void Right_Position_Forward()
{
FR.write(MAX_DEGREE);
RR.write(MAX_DEGREE);
}
void Right_Position_Backward()
{
FR.write(MIN_DEGREE);
RR.write(MIN_DEGREE);
}
void Right_Position_Outward()
{
FR.write(MAX_DEGREE);
RR.write(MIN_DEGREE);
}
void Right_Position_Inward()
{
FR.write(MIN_DEGREE);
RR.write(MAX_DEGREE);
}
void Right_Position_Stand()
{
FR.write(MID_DEGREE);
RR.write(MID_DEGREE);
}
void Middle_Position_Left()
{
ML.write(MAX_DEGREE);
MR.write(MAX_DEGREE);
}
void Middle_Position_Right()
{
ML.write(MIN_DEGREE);
MR.write(MIN_DEGREE);
}
void Middle_Position_Stand()
{
ML.write(MID_DEGREE);
MR.write(MID_DEGREE);
}
void Middle_Position_Outward()
{
ML.write(MAX_DEGREE);
MR.write(MIN_DEGREE);
}
void Middle_Position_Inward()
{
ML.write(MIN_DEGREE);
MR.write(MAX_DEGREE);
}
void Middle_Lean_Left()
{
ML.write(MIN_DEGREE);
MR.write(MID_DEGREE);
Left_Position_Outward();
}
void Middle_Lean_Right()
{
ML.write(MID_DEGREE);
MR.write(MAX_DEGREE);
Right_Position_Outward();
}
void Step_Forward()
{
Stand();
delay(BASIC_DELAY);
Left_Position_Forward();
delay(BASIC_DELAY);
Middle_Position_Inward();
delay(BASIC_DELAY);
Left_Position_Backward();
delay(BASIC_DELAY);
Middle_Position_Stand();
delay(BASIC_DELAY);
Left_Position_Stand();
delay(BASIC_DELAY);
Right_Position_Forward();
delay(BASIC_DELAY);
Middle_Position_Inward();
delay(BASIC_DELAY);
Right_Position_Backward();
delay(BASIC_DELAY);
Middle_Position_Stand();
delay(BASIC_DELAY);
Right_Position_Stand();
delay(BASIC_DELAY);
}
void Step_Backward()
{
Stand();
delay(BASIC_DELAY);
Left_Position_Backward();
delay(BASIC_DELAY);
Middle_Position_Inward();
delay(BASIC_DELAY);
Left_Position_Forward();
delay(BASIC_DELAY);
Middle_Position_Stand();
delay(BASIC_DELAY);
Left_Position_Stand();
delay(BASIC_DELAY);
Right_Position_Backward();
delay(BASIC_DELAY);
Middle_Position_Inward();
delay(BASIC_DELAY);
Right_Position_Forward();
delay(BASIC_DELAY);
Middle_Position_Stand();
delay(BASIC_DELAY);
Right_Position_Stand();
delay(BASIC_DELAY);
}
void Step_Left()
{
Stand();
delay(BASIC_DELAY);
Right_Position_Forward();
delay(BASIC_DELAY);
Middle_Position_Inward();
delay(BASIC_DELAY);
Right_Position_Backward();
delay(BASIC_DELAY);
Middle_Position_Stand();
delay(BASIC_DELAY);
Right_Position_Stand();
delay(BASIC_DELAY);
}
void Step_Right()
{
Stand();
delay(BASIC_DELAY);
Left_Position_Forward();
delay(BASIC_DELAY);
Middle_Position_Inward();
delay(BASIC_DELAY);
Left_Position_Backward();
delay(BASIC_DELAY);
Middle_Position_Stand();
delay(BASIC_DELAY);
Left_Position_Stand();
delay(BASIC_DELAY);
}
int Left_Sensor; int Right_Sensor; int Front_Sensor; int Rear_Sensor;
void Set_Sensors()
{
Left_Sensor = analogRead(2);
Right_Sensor = analogRead(1);
Front_Sensor = analogRead(3);
Rear_Sensor = analogRead(0);
}
int Sensor_Approximation(int S, int V)
{
if (V > S * 1.2)
return (1);
if (V < S * 0.8)
return(0);
return(0);
}
int Dark_Direction()
{
}
void loop()
{
int x;
Stand();
delay(2000);
Set_Sensors();
while(1)
{
if (Sensor_Approximation(Left_Sensor, analogRead(2)))
{
for(x=0;x<5;++x)
Step_Right();
Set_Sensors();
}
if (Sensor_Approximation(Right_Sensor, analogRead(1)))
{
for(x=0;x<5;++x)
Step_Left();
Set_Sensors();
}
if (Sensor_Approximation(Front_Sensor, analogRead(3)))
{
for(x=0;x<5;++x)
Step_Backward();
Set_Sensors();
}
if (Sensor_Approximation(Rear_Sensor, analogRead(0)))
{
for(x=0;x<5;++x)
Step_Forward();
Set_Sensors();
}
}
delay(2000);
// put your main code here, to run repeatedly:
}