duminică, 25 septembrie 2011

CD Bot

Astazi am gasit timp sa termin robotelul pe care l-am facut "printre picaturi". Este un robot care ocoleste obstacolele, iar in drumetia pe care o parcurge afiseaza pe un LCD intensitatea luminii. Are 2 LED-uri unul galben si unul RGB, cel galben licareste cand detecteaza un obstacol iar cel RGB isi schimba culoarea in functie de activitatea curenta a robotului.
Din titlu cred ca banuiti din ce este construit robotul, anume din cd-uri, forma sa estetica nu este cea finala; mie imi pare destul de dragut si imi place sa il privesc in actiune, doar e creatia mea. Actual nu are prea multe intrebuintari si multi probabil ar socoti acest robot nefolositor, momentan este distractiv si acesta e scopul lui, sa amuze intr-o mica masura.
In urmatoarele upgrade-uri robotul va cauta cele mai luminoase locuri, va afisa temeratura pe LCD, o sa ii atasez si un difuzor sa fie mai comic, va mai primi in dotare un receptor si un emitator IR, si in functie de forma estetica poate si un brat robotizat.

Mai jos afisez codu-l robotului dupa cum am obisnuit, dar din cauza ca nu stiu cum sa il pun altfel cum intalniti pe alte sitte-uri si din cauza ca ocupa foarte mult spatiu, acesta este ultimul cod afisat(cele scurte vor face exceptie), pentru viitoarele cod-uri trebuie doar sa imi lasati un comentariu cu adresa de e-mail iar eu voi trimite codu-l sursa in cel mai scurt timp.



COD:


/*
* Robt obsticle evasion prgoram Version 2.3
* By Musat Cristian
* 19/2/2010
*/

#include // Servo library

int dir = 1; // declare variables
int pos = 0;
int pingPin = 8;
int pwm_a = 3; //PWM control for motor outputs 1 and 2 is on digital pin 3
int pwm_b = 11; //PWM control for motor outputs 3 and 4 is on digital pin 11
int dir_a = 12; //direction control for motor outputs 1 and 2 is on digital pin 12
int dir_b = 13; //direction control for motor outputs 3 and 4 is on digital pin 13
Servo headservo;

void setup()
{
pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);

pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(2, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(2, LOW);
digitalWrite(9, LOW);
headservo.attach(7);

Serial.begin(9600); // attach LCD to com port

wait(1); // stop the robot

ledWrite(LOW,HIGH,HIGH); // Flash LED
delay(500);
ledWrite(HIGH,LOW,HIGH);
delay(500);
ledWrite(HIGH,HIGH,LOW);
delay(500);
}


void loop (void) { // Main body of the program

int inches;

while(inches>8) // while nothing is within 10 inches do the following
{
if (pos == 0)
{
dir = 3; // if it is looking left direction is positive
headservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3);
}

else if (pos == 180)
{
dir = -3; // if it is looking right direction is negative
headservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 3ms for the servo to reach the position
}
headservo.write(pos);
inches = ping (); // anybody there?
pos += dir; // recalculate position variable
forward(1); // Move forward

}

int x = headservo.read();
wait(0); // stop the robot

if(inches>5) // for objects further away turn gradually
{
if (x>=0 && x<90) { digitalWrite(pwm_a, 80); //set both motors to run at (100/255 = 39)% duty cycle (slow) digitalWrite(pwm_b, 230); delay(50); } if (x>=90 && x<=180) { digitalWrite(pwm_a, 230); //set both motors to run at (100/255 = 39)% duty cycle (slow) digitalWrite(pwm_b, 80); delay(50); } // if (x>=75 && x<105) // { // backward(100); // right(50); // } } else // for close objects turn hard { if (x>=0 && x<90) { right(100); } if (x>=90 && x<=180) { left(100); } // if (x>=75 && x<105)
// {
// backward(100);
// left(50);
// }

}

blinck();
}

// This function sends a signal to the Ping semsor and reterns a value in inches

int ping ()
{
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW); // make sure the signal pin starts low
delayMicroseconds(2);
digitalWrite(pingPin, HIGH); // hello, ping)))
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
return pulseIn(pingPin, HIGH) / 148; // wait for echo and return inches
}

// Movement functions

void forward(int fsec)
{

Serial.print("forward");
digitalWrite(pwm_a, 240); //set both motors to run at (100/255 = 39)% duty cycle (slow)
digitalWrite(pwm_b, 240); // display direction
digitalWrite(dir_a, LOW); //Set motor direction, 1 low, 2 high
digitalWrite(dir_b, LOW); //Set motor direction, 3 high, 4 low
ledWrite(LOW,LOW,LOW); // set LED colour

delay(fsec); // delay period defined in function call
}

void left(int lsec)
{

Serial.print("left");
digitalWrite(pwm_a, 240); //set both motors to run at (100/255 = 39)% duty cycle (slow)
digitalWrite(pwm_b, 240);
digitalWrite(dir_a, LOW); //Set motor direction, 1 low, 2 high
digitalWrite(dir_b, HIGH); //Set motor direction, 3 high, 4 low
ledWrite(HIGH,LOW,HIGH);

delay(lsec);
}

void right(int rsec)
{

Serial.print("right");
digitalWrite(pwm_a, 240); //set both motors to run at (100/255 = 39)% duty cycle (slow)
digitalWrite(pwm_b, 240);
digitalWrite(dir_a, HIGH); //Set motor direction, 1 low, 2 high
digitalWrite(dir_b, LOW); //Set motor direction, 3 high, 4 low
ledWrite(HIGH,HIGH,LOW);

delay(rsec);
}

void backward(int bsec)
{

Serial.print("backward");
digitalWrite(pwm_a, 0); //set both motors to run at (100/255 = 39)% duty cycle (slow)
digitalWrite(pwm_b, 0);
digitalWrite(dir_a, HIGH); //Set motor direction, 1 low, 2 high
digitalWrite(dir_b, HIGH); //Set motor direction, 3 high, 4 low
ledWrite(LOW,LOW,HIGH);

delay(bsec);
}

void wait(int wsec)
{

Serial.print("stop");
digitalWrite(pwm_a, 0); //set both motors to run at (100/255 = 39)% duty cycle (slow)
digitalWrite(pwm_b, 0);
digitalWrite(dir_a, LOW); //Set motor direction, 1 low, 2 high
digitalWrite(dir_b, LOW); //Set motor direction, 3 high, 4 low
ledWrite(LOW,HIGH,HIGH);

delay(wsec);
}

// LED write function

void ledWrite(boolean r, boolean g, boolean b)
{
digitalWrite(4, r);
digitalWrite(6, g);
digitalWrite(5, b);
}

void blinck()
{
digitalWrite(10, HIGH);
delay(40);
digitalWrite(10, LOW);
delay(140);
}