go away spammer

Author Topic: Digital Light Sensor Code  (Read 2526 times)

0 Members and 1 Guest are viewing this topic.

Offline mnroboTopic starter

  • Beginner
  • *
  • Posts: 5
  • Helpful? 0
Digital Light Sensor Code
« on: December 06, 2008, 01:19:38 PM »
Hi,

I have been trying to use a digital light sensor as a switch to control two servos.  If the input from the digital sensor is a 1, the servos will rotate, and if it is a zero, the servos will center (the instructions for the light sensor say that it will maintain a HIGH status when the power is on and fall to LOW status when light is detected.  I have tried to configure pin A1 as a digital input and then read the value, but nothing seems to work.  I haven't done any programming for years, and can't seem to find what I am looking for in any other posts.

I have an Axon MCU and have tried following the instructions in the following paragraph:
"DIGITAL INPUTS
By default, all Axon digital ports are set as outputs (for use with servos). But you can also make them input pins.
First, you must declare the pin a digital input. Look at the file SoR_Utils.h for examples, and read the comments for instructions.
You can also do it quick and dirty in control.c like this:
cbi(DDRG, PG5);  //set G5 as input pin
Then, just like in the button code, you'd want to read the pin:
bit_is_clear(PING, 5);  //returns a 1 or 0
bit_is_set(PING, 5);  //returns a 1 or 0"

This just isn't in depth enough for me, I guess.  if I want to use A1 for this digital light sensor, I would use "cbi(DDRA, PA1);  //set A1 as input pin" in control.c, correct?  Where do I put the last two lines of code, and do i really need to use both lines (changing to "... (PINA, 1)")?  What is the difference between them if the comments both say "returns a 1 or 0?

Finally, once I have the pin set as a digital input and have it returning a 1 or 0, how do I use this value in my code to control the servos?  Specifically, do I need to define "light_sensor..." in hardware.c and then int light_sensor in control.c?

I hope this post makes sense; please help me out.


Offline Canabots

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
    • Salmigondis Tech
Re: Digital Light Sensor Code
« Reply #1 on: December 06, 2008, 01:39:21 PM »
What light sensor is it? Have you read the datasheet?

I had a similar problem as this once with a Sharp digital IR sensor. The datasheet showed that you needed a resistor connecting the regulated voltage with the sensor output pin in order to be able to create an output. It wasn't well visible though.

Is this the aswer to your solution, maybe not. But I recommend looking at the datasheet again just incase. (It also helps us understand the problem if you showed a link to the part.)

Good luck :)

Canabots
« Last Edit: December 06, 2008, 01:39:54 PM by Canabots »
My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

Offline mnroboTopic starter

  • Beginner
  • *
  • Posts: 5
  • Helpful? 0
Re: Digital Light Sensor Code
« Reply #2 on: December 06, 2008, 02:10:20 PM »
I haven't been able to find a datasheet on this sensor, there are multiple sites that have the same info; here is the site I bought it from: http://www.robotshop.ca/home/suppliers/hitec-en/hitec-light-sensor.html

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Digital Light Sensor Code
« Reply #3 on: December 10, 2008, 04:20:36 AM »
It appears that your light sensor is just a $9 photoresistor:
http://www.acroname.com/robotics/parts/H8-LIGHT-SENSOR.html



I recommend hooking up the output to your multimeter and see how the voltage output changes with light.

Also, just use ADC to keep things simple for you (no need to reconfig pins). ADC can read in digital signals no problem.

Quote
This just isn't in depth enough for me, I guess.  if I want to use A1 for this digital light sensor, I would use "cbi(DDRA, PA1);  //set A1 as input pin" in control.c, correct?  Where do I put the last two lines of code, and do i really need to use both lines (changing to "... (PINA, 1)")?  What is the difference between them if the comments both say "returns a 1 or 0?

Finally, once I have the pin set as a digital input and have it returning a 1 or 0, how do I use this value in my code to control the servos?

Do this:
Code: [Select]
cbi(DDRA, PA1);  //set A1 as input pin

//if the sensor output is 0, do something
if(bit_is_clear(PINA, 1))
        take_over_world();

 


Get Your Ad Here

data_list