Society of Robots - Robot Forum

Software => Software => Topic started by: HSG on June 17, 2012, 11:07:09 AM

Title: $50 robot: Everything look good, but PD4 wont go low/ LED is not lightning
Post by: HSG on June 17, 2012, 11:07:09 AM
Hi there, Experts!

I have decided to make the $50 robot, and started by laying out the electronics on a breadboard (not connecting servos and photoresistors), and downloading , building and uploading the photovore /$50 robot code supplied by admin.

I use atmega8 uC, AVR ISP mk2 programmer,  and AVR studio 5.1

There are no error messages when I build the code, or when I program the atMega8 uC (memories -> Program)

However, the LED does not come on as PD4 port does not go low. :(

If anyone has any ideas about how I can troubleshoot this, it would be highly, highly appreciated!

Regards,
HSG

Title: Re: $50 robot: Everything look good, but PD4 wont go low/ LED is not lightning
Post by: Webbot on June 17, 2012, 05:15:29 PM
Have you written software to tell it to go low - as nothing else will.
Title: Re: $50 robot: Everything look good, but PD4 wont go low/ LED is not lightning
Post by: HSG on June 21, 2012, 03:10:08 AM
Hi Webbot!

Thanks for the reply.

I am using the photovore code, and I have tried to uncomment both the LED_on() and LED_off() calls.
From SoR_Utils.h, it seems like LED_on(), should do the trick by making PD4 go low.

Is there something fundamentally I haven't understood? :S

Regards,
HSG
Title: Re: $50 robot: Everything look good, but PD4 wont go low/ LED is not lightning
Post by: adanvasco on June 21, 2012, 11:08:27 PM
I think that when I made the $50 robot, I had to chance the code a bit for the LED to work. Check this post:  http://www.societyofrobots.com/robotforum/index.php?topic=13795.msg102029#msg102029 (http://www.societyofrobots.com/robotforum/index.php?topic=13795.msg102029#msg102029)
Title: Re: $50 robot: Everything look good, but PD4 wont go low/ LED is not lightning
Post by: HSG on June 22, 2012, 08:36:08 AM
Hi Advanvasco!

Thanks for your reply.

Changing from LED_off() to LED_on()  (by commenting/uncommenting) was actually one of the first things I tried.

Or is it more to the changes you did than this?

Regards,
HSG
Title: Re: $50 robot: Everything look good, but PD4 wont go low/ LED is not lightning
Post by: adanvasco on June 22, 2012, 01:00:29 PM
Can you post your source code? Both you .c file (Photovore_v1.c I think) and SoR_Utils.h.

I understand that you have checked the voltage on the pin with a voltmeter of some sort.
Title: Re: $50 robot: Everything look good, but PD4 wont go low/ LED is not lightning
Post by: Webbot on June 23, 2012, 09:00:07 AM
The other common cause is that folk have soldered the LED the wrong way around so it will never light.

At your own risk:

Take the mcu out of the socket to prevent damage and remove any power supply from the board

Take a battery 4v to 9v say .... and connect the leads from the battery:

(+ve)-----resistor-----led----(-ve)

since the $50 robot has nothing else connected to these pins then its safe to try the battery leads the other way ie
(-ve)-----resistor-----led----(+ve)

If neither combination works then you have either soldered the LED in the wrong way around or the LED is dead.


Title: Re: $50 robot: Everything look good, but PD4 wont go low/ LED is not lightning
Post by: HSG on July 12, 2012, 06:08:18 AM
Hi Adanvasco!

Yes, I measured the voltage between the PD4 and GND using a multimeter, and its 4.9V.

The code is found below - its essentially unmodified from the original, except for the LED_off being changed to LED_on()

Regards,
HSG

Photovore_v1.c

//SoR Include
#include "SoR_Utils.h" //includes all the technical stuff


int main(void)
   {
   //declare variables here
   //int i=250;//a 'whatever' variable
   int sensor_left=0;//left photoresistor
   int sensor_right=0;//right photoresistor
   int threshold=8;//the larger this number, the more likely your robot will drive straight
      
   configure_ports(); // configure which ports are analog, digital, etc.
   a2dInit(); // initialize analog to digital converter (ADC)
   a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling
   a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage


   
   /*********ADD YOUR CODE BELOW THIS LINE **********/
   configure_ports();
   LED_on();//turn LED on

   while(1)
      {
      //store sensor data
      sensor_left=a2dConvert8bit(5);
      sensor_right=a2dConvert8bit(4);


      //detects more light on left side of robot
      if(sensor_left > sensor_right && (sensor_left - sensor_right) > threshold)
         {//go left
         servo_left(44);
         servo_right(44);
         }

      //detects more light on right side of robot
      else if(sensor_right > sensor_left && (sensor_right - sensor_left) > threshold)
         {//go right
         servo_left(25);
         servo_right(25);
         }

      //light is about equal on both sides
      else
         {//go straight
         servo_left(25);
         servo_right(44);
         }


      /* Servo Test Code
      i=250;
      while(i>0)
         {
         servo_left(40);
         i--;
         }

      i=250;
      while(i>0)
         {
         servo_left(24);
         i--;
         }
      */

      //rprintf("Initialization Complete\r\n");
      
      //output message to serial (use hyperterminal)
      //print("Hello, World! Read My Analog: %u\r\n", sensor_0);

      delay_cycles(500);//a small delay to prevent crazy oscillations
      }
   /*********ADD YOUR CODE ABOVE THIS LINE **********/

   return 0;
   }





Sor_utils :


//AVR includes
#include <avr/io.h>          // include I/O definitions (port names, pin names, etc)
#include <avr/interrupt.h>   // include interrupt support

//AVRlib includes
#include "global.h"      // include global settings
//#include "buffer.h"      // include buffer function library
//#include "uart.h"      // include uart function library
//#include "rprintf.h"   // include printf function library
//#include "timer.h"      // include timer function library (timing, PWM, etc)
#include "a2d.h"      // include A/D converter function library

//define port functions; example: PORT_ON( PORTD, 6);
#define PORT_ON( port_letter, number )         port_letter |= (1<<number)
#define PORT_OFF( port_letter, number )         port_letter &= ~(1<<number)
#define PORT_ALL_ON( port_letter, number )      port_letter |= (number)
#define PORT_ALL_OFF( port_letter, number )      port_letter &= ~(number)
#define FLIP_PORT( port_letter, number )      port_letter ^= (1<<number)
#define PORT_IS_ON( port_letter, number )      ( port_letter & (1<<number) )
#define PORT_IS_OFF( port_letter, number )      !( port_letter & (1<<number) )


//************CONFIGURE PORTS************
//configure ports for input or output - specific to ATmega8
void configure_ports(void)
   {
   DDRC = 0x00;  //configure all C ports for input
   PORTC = 0x00; //make sure pull-up resistors are turned off
   DDRD = 0xFF;  //configure all D ports for output
   DDRB = 0xC7;  //configure B ports 0, 1, 2, 6, 7 for output (google search '0b11000111 to hex')
   }
//***************************************

//************DELAY FUNCTIONS************
//wait for X amount of cycles (23 cycles is about .992 milliseconds)
//to calculate: 23/.992*(time in milliseconds) = number of cycles
void delay_cycles(unsigned long int cycles)
   {
   while(cycles > 0)
      cycles--;
   }
//***************************************

//*********SIMPLIFIED FUNCTIONS**********
//functions to make coding easier for a beginner
//but could cause port mixup confusion for intermediate users
void LED_on(void)
   {
   PORT_OFF(PORTD, 4);//turn LED on
   }
void LED_off(void)
   {
   PORT_ON(PORTD, 4);//turn LED off
   }
void servo_left(signed long int speed)
   {
   PORT_ON(PORTD, 0);
   delay_cycles(speed);
   PORT_OFF(PORTD, 0);//keep off
   delay_cycles(200);
   }
void servo_right(signed long int speed)
   {
   PORT_ON(PORTD, 1);
   delay_cycles(speed);      
   PORT_OFF(PORTD, 1);//keep off
   delay_cycles(200);
   }
//***************************************
Title: Re: $50 robot: Everything look good, but PD4 wont go low/ LED is not lightning
Post by: HSG on July 12, 2012, 06:10:18 AM
Hi Webbot!

Thanks for your suggestion

I am prototyping using a breadboard, and I have independently verified that the LED works and that the orientation is right..

Regards,
HSG