LIGHT FOLLOWER WITH DC MOTOR

Light follower DC

int dir1PinA = 2;

int dir2PinA = 3;

int speedPinA = 9;

int d;

void setup() {

Serial.begin(9600);

//Define L298N Dual H-Bridge Motor Controller Pins

pinMode(dir1PinA,OUTPUT);

pinMode(dir2PinA,OUTPUT);

pinMode(speedPinA,OUTPUT);

}//–(end setup )—

void loop() {

int sensorValue0 = analogRead(A0);

int sensorValue1 = analogRead(A1);

d=sensorValue0-sensorValue1;

Serial.print(sensorValue0);

Serial.print(” “);

Serial.print(sensorValue1);

Serial.print(” “);

Serial.println(d);

if (d > 15) {

maju();

}

if (d < -15) {

mundur();

}

if ((d <= 15)&&(d >= -15)) {

henti();

}

if ((sensorValue0 <= 50 )&&(sensorValue1 <= 50 )) {

henti();

}

}

void maju(){

analogWrite(speedPinA, 255);//Sets speed variable via PWM

digitalWrite(dir1PinA, LOW);

digitalWrite(dir2PinA, HIGH);

}

void mundur(){

analogWrite(speedPinA, 255);

digitalWrite(dir1PinA, HIGH);

digitalWrite(dir2PinA, LOW);

}

void henti(){

analogWrite(speedPinA, 0);

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s