0% found this document useful (0 votes)
97 views

Sdadads

The document defines functions and code to control two motors based on the input from four light sensors. It sets the input and output pins on a microcontroller, then uses repeated if/else statements to check the sensor values and set the motor directions and speeds accordingly by writing to the output ports. The aim is to control the two motors independently or together based on which sensors detect light or movement.

Uploaded by

adapa1234
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Sdadads

The document defines functions and code to control two motors based on the input from four light sensors. It sets the input and output pins on a microcontroller, then uses repeated if/else statements to check the sensor values and set the motor directions and speeds accordingly by writing to the output ports. The aim is to control the two motors independently or together based on which sensors detect light or movement.

Uploaded by

adapa1234
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <avr/io.

h>
#define F_CPU 8000000UL
#include <util/delay.h>
void delay_ms(unsigned int ms)
{
while(ms)
{
_delay_ms(1.000);
ms--;
}
}
int main()
{
DDRD=0b11110000; //Input PIN 14,15,16,17
DDRB=0xFF; //PORTB is output, PIN 1-8
while(1)
{
if((PIND & 0b00000001) && (PIND & 0b00000010)
&& (PIND & 0b00000100) && (PIND & 0b00001000))
{ // All 4 sensors have Equal Light,No Movement
PORTB=0b00010001;
}
else if(((PIND & 0b00000001) && (!(PIND & 0b00001110)))||
((PIND & 0b00000001) && (PIND & 0b00000100) && (PIND & 0b00001000)))
{ //Motor 1 Forward
//Motor 2 Stop
if(((PIND & 0b00000001) && (!(PIND & 0b00001110)))||
((PIND & 0b00000001) && (PIND & 0b00000100) && (PIND & 0b00001000)))
{
PORTB=0b00010001;
delay_ms(500);
}
if(((PIND & 0b00000001) && (!(PIND & 0b00001110)))||
((PIND & 0b00000001) && (PIND & 0b00000100) && (PIND & 0b00001000)))
{
PORTB=0b00010010;
delay_ms(500);
}
if(((PIND & 0b00000001) && (!(PIND & 0b00001110)))||
((PIND & 0b00000001) && (PIND & 0b00000100) && (PIND & 0b00001000)))
{
PORTB=0b00010100;
delay_ms(500);
}
if(((PIND & 0b00000001) && (!(PIND & 0b00001110)))||
((PIND & 0b00000001) && (PIND & 0b00000100) && (PIND & 0b00001000)))
{
PORTB=0b00011000;
delay_ms(500);
}
}else if(((PIND & 0b00000100) && (!(PIND & 0b00001011)))||
((PIND & 0b00000001) && (PIND & 0b00000010) && (PIND & 0b00000100)))
{ //Motor 1 Stop
//Motor 2 Forward
if(((PIND & 0b00000100) && (!(PIND & 0b00001011)))||
((PIND & 0b00000001) && (PIND & 0b00000010) && (PIND & 0b00000100)))
{
PORTB=0b00010001;
delay_ms(500);

}
if(((PIND & 0b00000100) && (!(PIND & 0b00001011)))||
((PIND & 0b00000001) && (PIND & 0b00000010) && (PIND & 0b00000100)))
{
PORTB=0b00100001;
delay_ms(500);
}
if(((PIND & 0b00000100) && (!(PIND & 0b00001011)))||
((PIND & 0b00000001) && (PIND & 0b00000010) && (PIND & 0b00000100)))
{
PORTB=0b01000001;
delay_ms(500);
}
if(((PIND & 0b00000100) && (!(PIND & 0b00001011)))||
((PIND & 0b00000001) && (PIND & 0b00000010) && (PIND & 0b00000100)))
{
PORTB=0b10000001;
delay_ms(500);
}
}else if(((PIND & 0b00000010) && (!(PIND & 0b00001101)))||
((PIND & 0b00000010) && (PIND & 0b00000100) && (PIND & 0b00001000)))
{ //Motor 1 Reverse
//Motor 2 Stop
if(((PIND & 0b00000010) && (!(PIND & 0b00001101)))||
((PIND & 0b00000010) && (PIND & 0b00000100) && (PIND & 0b00001000)))
{
PORTB=0b00011000;
delay_ms(500);
}
if(((PIND & 0b00000010) && (!(PIND & 0b00001101)))||
((PIND & 0b00000010) && (PIND & 0b00000100) && (PIND & 0b00001000)))
{
PORTB=0b00010100;
delay_ms(500);
}
if(((PIND & 0b00000010) && (!(PIND & 0b00001101)))||
((PIND & 0b00000010) && (PIND & 0b00000100) && (PIND & 0b00001000)))
{
PORTB=0b00010010;
delay_ms(500);
}
if(((PIND & 0b00000010) && (!(PIND & 0b00001101)))||
((PIND & 0b00000010) && (PIND & 0b00000100) && (PIND & 0b00001000)))
{
PORTB=0b00010001;
delay_ms(500);
}
}else if(((PIND & 0b00001000) && (!(PIND & 0b00000111)))||
((PIND & 0b00000001) && (PIND & 0b00000010) && (PIND & 0b00001000)))
{ //Motor 1 Stop
//Motor 2 Reverse
if(((PIND & 0b00001000) && (!(PIND & 0b00000111)))||
((PIND & 0b00000001) && (PIND & 0b00000010) && (PIND & 0b00001000)))
{
PORTB=0b10000001;
delay_ms(500);
}
if(((PIND & 0b00001000) && (!(PIND & 0b00000111)))||
((PIND & 0b00000001) && (PIND & 0b00000010) && (PIND & 0b00001000)))
{

PORTB=0b01000001;
delay_ms(500);
}
if(((PIND & 0b00001000) && (!(PIND & 0b00000111)))||
((PIND & 0b00000001) && (PIND & 0b00000010) && (PIND & 0b00001000)))
{
PORTB=0b00100001;
delay_ms(500);
}
if(((PIND & 0b00001000) && (!(PIND & 0b00000111)))||
((PIND & 0b00000001) && (PIND & 0b00000010) && (PIND & 0b00001000)))
{
PORTB=0b00010001;
delay_ms(500);
}
}else if((PIND & 0b00000001) && (PIND & 0b00000100))
{ //Motor 1 Foward
//Motor 2 Forward
if((PIND & 0b00000001) && (PIND & 0b00000100))
{
PORTB=0b00010001;
delay_ms(500);
}
if((PIND & 0b00000001) && (PIND & 0b00000100))
{
PORTB=0b00100010;
delay_ms(500);
}
if((PIND & 0b00000001) && (PIND & 0b00000100))
{
PORTB=0b01000100;
delay_ms(500);
}
if((PIND & 0b00000001) && (PIND & 0b00000100))
{
PORTB=0b10001000;
delay_ms(500);
}
}else if((PIND & 0b00000010) && (PIND & 0b00000100))
{ //Motor 1 Reverse
//Motor 2 Forward
if((PIND & 0b00000010) && (PIND & 0b00000100))
{
PORTB=0b00011000;
delay_ms(500);
}
if((PIND & 0b00000010) && (PIND & 0b00000100))
{
PORTB=0b00100100;
delay_ms(500);
}
if((PIND & 0b00000010) && (PIND & 0b00000100))
{
PORTB=0b01000010;
delay_ms(500);
}
if((PIND & 0b00000010) && (PIND & 0b00000100))
{
PORTB=0b10000001;
delay_ms(500);

}
}else if((PIND & 0b00000001) &&
{ //Motor 1 Forward
//Motor 2 Reverse
if((PIND & 0b00000001) && (PIND
{
PORTB=0b10000001;
delay_ms(500);
}
if((PIND & 0b00000001) && (PIND
{
PORTB=0b01000010;
delay_ms(500);
}
if((PIND & 0b00000001) && (PIND
{
PORTB=0b00100100;
delay_ms(500);
}
if((PIND & 0b00000001) && (PIND
{
PORTB=0b00011000;
}else if((PIND & 0b00000010) &&
{ //Motor 1 Reverse
//Motor 2 Reverse
if((PIND & 0b00000010) && (PIND
{
PORTB=0b10001000;
delay_ms(500);
}
if((PIND & 0b00000010) && (PIND
{
PORTB=0b01000100;
delay_ms(500);
}
if((PIND & 0b00000010) && (PIND
{
PORTB=0b00100010;
delay_ms(500);
}
if((PIND & 0b00000010) && (PIND
{
PORTB=0b00010001;
delay_ms(500);
}
}else{
PORTB=0b00010001;
}
} // End while
return 0;
}// End main

(PIND & 0b00001000))


& 0b00001000))

& 0b00001000))

& 0b00001000))

& 0b00001000))
(PIND & 0b00001000))
& 0b00001000))

& 0b00001000))

& 0b00001000))

& 0b00001000))

You might also like