go away spammer

Author Topic: a2d Question  (Read 2882 times)

0 Members and 1 Guest are viewing this topic.

Offline weighatesTopic starter

  • Jr. Member
  • **
  • Posts: 12
  • Helpful? 0
a2d Question
« on: December 20, 2009, 03:56:36 AM »
Ok.   I like to do things myself because it helps me understand the underlying fundimentals.  I am in the process of building the $50 robot.  The circuit board is completely built to spec.  I am now trying to get a input signal from my photoresister on port c pin 4 to make a motor move if it goes above 500.  My code is as follows and nothing happens.  If I remove the input I can drive the servo no problem so I think it's got to be my code for the input.   It builds with 1 warning which is there because I havent set the cpu frequency but nothing happens and I have tried everything I can think of atm.   Thanks for your help.   Chris

#include <avr/io.h>
#include <util/delay.h>
#include <SoR_utils/global.h>
#include <SoR_utils/a2d.h>

int main(void)
{
   int a2dval;
   a2dInit();
   int here;
   DDRC = 0x00;
   PORTC = 0x00;
   here = 1;
   DDRD = 0xff;
   while(1)
   {
      a2dval = a2dConvert10bit(4);
      if (a2dval >= 500)
      {
         for (int i = 0; i < 100; i++)
         {
            PORTD = 0xff;
            _delay_ms(2);
            PORTD = 0x00;
            _delay_ms(20);
         }
      }
   }
   return 1;
}

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: a2d Question
« Reply #1 on: December 20, 2009, 04:00:25 AM »
Have you tried values other than 500?
Howdy

Offline weighatesTopic starter

  • Jr. Member
  • **
  • Posts: 12
  • Helpful? 0
Re: a2d Question
« Reply #2 on: December 20, 2009, 04:07:54 AM »
yes I have tried many values none of them work.  I have also tried covering the photoresistor and shining a light into it with each value and nothing.

thanks,
Chris
« Last Edit: December 20, 2009, 04:11:27 AM by weighates »

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: a2d Question
« Reply #3 on: December 20, 2009, 04:13:23 AM »
It may be because you declare a2dval as an integer, which is an 8 bit data type having a maximum value 255. For reading a photosensor you don't need 10 bit precision so try an a2dConvert8bit() and adjust your threshold value accordingly (125).

Offline weighatesTopic starter

  • Jr. Member
  • **
  • Posts: 12
  • Helpful? 0
Re: a2d Question
« Reply #4 on: December 20, 2009, 02:28:55 PM »
Here is a update of my code.   There is a problem with a2dconvert8 I just cant figure out what it is.  If I take out the a2dconvert line the servo goes forward if I leave it in nothing happens.   So what the hell is it returning?  If its a number it should do something either forward or backward right?   I have no clue what I am doing wrong here.



#include <avr/io.h>
#include <util/delay.h>
#include <SoR_utils/global.h>
#include <SoR_utils/a2d.h>

int main(void)
{
   int a2dval;
   a2dInit();
   int here;
   DDRC = 0x00;
   PORTC = 0x00;
   here = 1;
   DDRD = 0xff;
   a2dval = 501;
   a2dval = a2dConvert8bit(5);
   if (a2dval <= 500) // Servo goes forward
   {
      for (int i = 0; i < 100; i++)
      {
         PORTD = 0xff;
         _delay_ms(2);
         PORTD = 0x00;
         _delay_ms(20);
      }
   }
   if (a2dval > 500) // Servo goes back
   {
      for (int i = 0; i < 100; i++)
      {
         PORTD = 0xff;
         _delay_ms(1);
         PORTD = 0x00;
         _delay_ms(20);
      }
   }
   return 1;
}
« Last Edit: December 20, 2009, 02:31:38 PM by weighates »

Offline little-c

  • Robot Overlord
  • ****
  • Posts: 186
  • Helpful? 1
Re: a2d Question
« Reply #5 on: December 20, 2009, 03:20:38 PM »
first off, ARGGGGGGG hard to understand variable names! I like commented code. anyway, comments and easy to understand variable names are nice, but not a legal requirement! so dont worry.

why    a2dval = a2dConvert8bit(5); instead of a2dval = a2dConvert10bit(4);? *waits for swearing*


can you program it to take out the binary value and flash it from an led? or printf? (guessing not)


take off the servo and use a multimeter to see if the logic can pull the pin high?

use

Code: [Select]

         PORTD = 0xff;
         _delay_ms(20);
in one if statement, and 0x88 (or something around that value) in the other. that way you have a fairly accurate way of telling if the logic is working... I think...

Offline weighatesTopic starter

  • Jr. Member
  • **
  • Posts: 12
  • Helpful? 0
Re: a2d Question
« Reply #6 on: December 20, 2009, 03:42:15 PM »
how would I get it to output printf?  I would love to see what the value its pulling is.  I know the servo works cause If I take out my if statements and just use

PORTD = 0xff;
_delay_ms(1)
PORTD = 0x00;
_delay_ms(20);

the servo spins no problem.   I have used a2dConvert10 and a2dConvert8 with every port number there is. 
« Last Edit: December 20, 2009, 03:47:34 PM by weighates »

Offline little-c

  • Robot Overlord
  • ****
  • Posts: 186
  • Helpful? 1
Re: a2d Question
« Reply #7 on: December 20, 2009, 03:55:28 PM »
first off, why is it converting pin 5 instead of pin 4?

printf, c code for fprintf(stdout "%s");

 you need an output to print to. lcd/pc/7segment display/ect
 a method of interfacing to switch pixels on and off(colour and other cool stuff possibly)
     I cant find the paperwork I have for seven segment displays, but its fairly straightfoward to write the binary patterns and convet to hex. mental arithmatic, or just use the calaculator in windows.
 code to take the argument, (interger value) and output a representation of the interger.

8+ segment bar graph can output the binary representation of the number.

can write up some pesudo c code if you want.

Offline little-c

  • Robot Overlord
  • ****
  • Posts: 186
  • Helpful? 1
Re: a2d Question
« Reply #8 on: December 20, 2009, 04:01:36 PM »
right, debugging 101.

take the robot. check every conector and wire and pin and everything else you can think of. is it in the right place. are they the right value.

is what your telling it to convert from, what its ment to convert from. is it returning values to the correct place?

is the micorcontroler talking to the right thing?

if it is, and there is not a hardware issue.

code.
what values are been passed where?
are you calling the right function?
are values been passed as the same type?
are logic statements correct?
are you actuly doing what you think you are doing?

Offline weighatesTopic starter

  • Jr. Member
  • **
  • Posts: 12
  • Helpful? 0
Re: a2d Question
« Reply #9 on: December 20, 2009, 04:46:38 PM »
I would love some pseudo code to display the value on my pc.  I checked voltages and am get 3.6 volts in the dark and 2.8 ish in bright light.   I am measuring at the cpu pin so I know its getting the juice.  I think I am using a2d in the correct manner.  Thanks a ton chris.



PS - I also get this warning when I rebuild all.  ../../../WinAVR-20090313/avr/include/SoR_utils/a2d.c:110: warning: 'SIG_ADC' appears to be a misspelled signal handler
I havent touched a2d.c or a2d.h so I dunno why its giving me this warning
« Last Edit: December 20, 2009, 04:51:06 PM by weighates »

Offline little-c

  • Robot Overlord
  • ****
  • Posts: 186
  • Helpful? 1
Re: a2d Question
« Reply #10 on: December 20, 2009, 05:23:36 PM »
on a pc????? epp. loaded question! that is fairly hard, normaly I would use an mbed for such stuff(is a chip from ARM, nasty to program, well it takes c, but needs to be interfaced in assembler most of the time. but has open source stuff for talking to pc's)
there is issues with what terminal your running, teraterm is cool, but bug city... don't let the micro reset, ever! its restart pc time.
 putty I never use.

8 segment bar graph displays are nicer,

you need to map the binary interger to the display.
c code for an LED + resistor wired to ground and a output pin. frequency can be adjusted. assumes a2dval is called and all ints declared properly.

####to follow#### brains fried, its nearly tomorrow, and I will probably make a mistake.

assembler is easy.

left shift a2dval
take overflow/carry and store at pin address.(micro dependant, tell me the micro, prefferably send data sheet and I can work it out)
wait (some number of cycles)

repeat 8 times in total.
binary 128
10000000
binary 64
01000000
binary 32
00100000
00010000 16
00001000 8
00000100 4
00000010 2
00000001 1
add sequence together and hey presto. a value!

left shifts send the bits one to the left. and carry is the bit that comes out the end. flash that and it gives you a value

you may want to process
10101010 or
128+32+8+2 or
170. or
252 or
AA

leave you to work out the last 2 representations. hint, look at the scientifc calculator in windows, and google the stuff on it! dec, bin, ect.

Offline weighatesTopic starter

  • Jr. Member
  • **
  • Posts: 12
  • Helpful? 0
Re: a2d Question
« Reply #11 on: December 20, 2009, 07:50:24 PM »
its a atmega168p

Offline little-c

  • Robot Overlord
  • ****
  • Posts: 186
  • Helpful? 1
Re: a2d Question
« Reply #12 on: December 21, 2009, 02:40:00 AM »
I have woke up and worked out a c code to do this with an led and resistor. just work out which pin your using and wiggle it up and down.
float output;
int math;
for (i=8;i>0;i--)
math = a2dval/2;
output =a2dval/2;
output = output-math;
if output>0
(wiggle led pin high for .5 seconds or so)
else
(wiggle pin low)


this will work.

Im typing blind at the moment. I cant see the computer screen. its died.

Offline weighatesTopic starter

  • Jr. Member
  • **
  • Posts: 12
  • Helpful? 0
Re: a2d Question
« Reply #13 on: December 21, 2009, 09:51:56 PM »
OK.  I think I have figured it out but I dont know how to fix it.   I have been getting the warning
../../Photovore_v1/a2d.c:110: warning: 'SIG_ADC' appears to be a misspelled signal handler
in a2d.c.    Well it just so happens that is the interrupt so I think whats happening is that it reads the analog input in an infinate loop.   So do you know how to fix that?  The line of code is

//! Interrupt handler for ADC complete interrupt.
SIGNAL(SIG_ADC)
{
   // set the a2d conversion flag to indicate "complete"
   a2dCompleteFlag = TRUE;
}

If I download and compile photovore.c it doesnt appear.   So Why would it appear on my project which is written from scratch?   I did include a2d.c in my source files.   Any thoughts anyone?

Thanks Chris

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: a2d Question
« Reply #14 on: December 22, 2009, 04:19:01 AM »
It looks like you have ditched Admin's SoR_Utils.h but might not have the #include <avr/interrupt.h> in your code.   

Offline weighatesTopic starter

  • Jr. Member
  • **
  • Posts: 12
  • Helpful? 0
Re: a2d Question
« Reply #15 on: December 22, 2009, 03:02:04 PM »
I have included intterupt.h but then I get alot more warnings about handlers being misspelled.   I get like 9 warnings.  I have also tried using SoR_Utils.h but get the same warning.

Thanks,
Chris
« Last Edit: December 22, 2009, 03:06:59 PM by weighates »

Offline little-c

  • Robot Overlord
  • ****
  • Posts: 186
  • Helpful? 1
Re: a2d Question
« Reply #16 on: December 22, 2009, 04:26:38 PM »
best shot,

reset micro, reprogram with a new version of the headers, ect. preferably freshly downloaded.

if its still blowing up, scrap that program and rewrite your code in a new program.
(no copy paste. retype. retyping means your looking at what your putting down, and spot the stupid mistake with something)

more blowing up, you have ether cooked the micro, or are cross assigning variables or something wierd like that.

at some point the simple annoying mistake becomes clear. ether you have typoed something, or accedently changed something. or it works and you don't care. :D

 


Get Your Ad Here

data_list