Jump to content

light sensing robot


chitrangda

Recommended Posts

I was recently working on a light sensing robot. Some how i managed to come up with it. I programed the robot with following program. I just wanted to know if there is some better way of doing the same, or is it fine when it comes on good programming.

 

#define F_CPU 12000000UL // define cpu frequency for delay function
#include <avr/io.h> // includes input/output header file     
#include <util/delay.h> // includes delay header file  
//#include"lcd.h"
//#include"lcd.c"
/* Program for Line follower */

//******************************************************************************
/* Connection of 3 PIN Sensor PORTs with atmega 8 
    PC4 Port is connected with PIN27 of Atmega 8
 PC5 Port is connected with PIN28 of Atmega 8
 PC3 Port is connected with PIN26 of Atmega 8(DTMF D3 is also sent to this pin) 
 PC2 Port is connected with PIN25 of Atmega 8(DTMF D2 is also sent to this pin)
 PC1 Port is connected with PIN24 of Atmega 8(DTMF D1 is also sent to this pin)
 PC0 Port is connected with PIN23 of Atmega 8(DTMF D0 is also sent to this pin)
 PC6 Port is connected with PIN 1(Reset Pin) of Atmega 8    
*******************************************************************************
Connection of L293D IC  with atmega 8  
Pin 2(A1) of L293D is Connected with PB2(Pin 16 of Atmega8)
Pin 7(B1) of L293D is Connected with PB3(MOSI)(Pin 17 of Atmega8)
**A1 and B1 are the input pins of Left side H-Bridge of L293D which is driving the Left Motor
Pin 15 (A2) of L293D is Connected with PB0(Pin 14of Atmega8)
Pin 10 (B2) of L293D is Connected with PB1(Pin 15 of Atmega8)
Pin 1(EN1) and pin 9(EN2) is connected Via motor enable switch.
when we switch on Enable Switch Both EN1 and EN2 are given
5 Volt supply to enalbe both H-Bridges of L293D
Pin 3 and pin6 of L293D is Connected with Left Motor Connector
Pin 14 and Pin 11 of L293D is connected with Right Motor Connector */  

//Connect Light Sensor on PC5 Port


int main(void)
{
DDRB=0b11111111; //PORTB as output Port connected to motors
DDRC=0b0000000; //PORTC Input port connected to Sensors
//lcd_init(LCD_DISP_ON);
//lcd_puts("AMIT SHIROHA\n");
   //lcd_puts("SBIT, SONIPAT");
int light_sensor=0;
while(1) // infinite loop
{
    light_sensor=PINC&0b0100000; // mask PC5 bit of Port C
if(light_sensor==0b0100000) //if LED1 is your right light sensor "
    PORTB=0b00000001; // move left
    else
    PORTB=0b00001000; // move right
}//while closed
}//main closed

Link to comment
Share on other sites

  • 3 weeks later...

I think, code will be much, much more readable, if you use interface macros for things like

PORTB=0b00000001; // move left

 

for example, you can use:

#define MoveLeft() PORTB=0b00000001

 

and then use the macro in your code.

Or even better, you can use SetBit and ClrBit macro, so you are sure that you are not mistaking bit masks:

#define SetBit(Byte, BitNb)  Byte = (unsigned char)Byte ^ (unsigned char)(1 << BitNb)
#define SetBit(Byte, BitNb)  Byte = (unsigned char)Byte & (unsigned char)~(1 << BitNb)

 

and then

 

#define MoveLeft()  ClrBit(PORTB, 4); SetBit(PORTB, 1)
#define MoveRight() ClrBit(PORTB, 1); SetBit(PORTB, 4)

 

Or even better differentiate moving and stop moving commands:

 

#define MoveLeft() SetBit(PORTB, 1)
#define StopMovingLeft() ClrBit(PORTB, 1)
#define MoveRight() SetBit(PORTB, 4)
#define StopMovingRight() ClrBit(PORTB, 4)

 

This is about code readability. Another thing - how does that light sensor work? Is it digital - i.e. you are sure that when light is "sensed" it sets PINC's 6th bit to 1?

 

If it is analog, you would probably need filtering of the sensor's value. When you say you've programmed the robot, does that mean it is working correctly?

Edited by vordhosbn
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.