Analog Servo Feedback Question

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
solardude
 
Posts: 145
Joined: Fri Oct 18, 2013 12:47 am

Re: Analog Servo Feedback Question

Post by solardude »

That was the problem and now its working!

Here is a quick video showing it working: http://youtu.be/tSD9hpXyfDY

After the sweep is completed how do we drive the servo back to the maxLuxPosition using the analog feedback function to make sure it actually gets there.

I tried adding the following code but it would drive the servo to the maxLuxPosition with every 5 degree step. I tried it outside the for loop and nothing happened.

Code: Select all

Seek(myservo, 0, maxLuxPosition);
If I can get the servo driven back to the maxLuxPosition after the full sweep then I can just call this function to run 1 time every 60 mins and then revert back to the sun tracking loop and everything should work perfectly.

Thanks for your help again Bill, I wouldn't be this far along if it wasn't for your help.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Analog Servo Feedback Question

Post by adafruit_support_bill »

I tried adding the following code but it would drive the servo to the maxLuxPosition with every 5 degree step. I tried it outside the for loop and nothing happened.
It needs to go after the 'for' loop. The problem is that the 'for' loop is inside your 'loop()' function and will get called over and over again. Even if you seek to the maxLuxPosition, you will immediately start the scan again.
If I can get the servo driven back to the maxLuxPosition after the full sweep then I can just call this function to run 1 time every 60 mins and then revert back to the sun tracking loop and everything should work perfectly.
So take the whole thing outside of the loop() function and put it in a scan function of its own. Add your tracking code to the loop and a timer to call the scan function every hour.

User avatar
solardude
 
Posts: 145
Joined: Fri Oct 18, 2013 12:47 am

Re: Analog Servo Feedback Question

Post by solardude »

Yep thats exactly what we need to do.

I have spend last night and some of today reading and searching to learn about time based interrupts and milli timers and I'm coming across so much different stuff that I'm throughly confused. I also came across where somebody suggested using the Polling function also since its easy and quick but I'm not sure what to use for this simple task.

I ideally would like to run the sun tracking loop & the scan function interrupt from a AtTiny 85 / Trinket if possible. The sun tracking sketch is very small so this souldn't be an issue go get it running on the the AT85.

So I need some advice/guidance on what is the most code efficient way to get the scan function added to my sun tracking loop so it runs 1 time very 30 or 60 mins?

I can't find any easy to understand timer interrupt sketch to work from. I have the Aruduino Cookbook and have been looking through it also but so far nothing that allows me to do what I want to do. Everything will be up and running once I have this interrupt working, very exciting!

Can you point me in the right direction??

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Analog Servo Feedback Question

Post by adafruit_support_bill »

There is a nice timer library and tutorial here: http://playground.arduino.cc/Code/Timer
It allows you to set up periodic 'callbacks' to run your functions.

User avatar
solardude
 
Posts: 145
Joined: Fri Oct 18, 2013 12:47 am

Re: Analog Servo Feedback Question

Post by solardude »

OK, I have been looking at different options all day long and still have not come up with anything.

So I'm back to looking at the simple timer library you linked me to and it looks like my best bet to me is the following code after I install the simple timer libarary:

Code: Select all

int every(long period, callback)

 Run the 'callback' every 'period' milliseconds.
 Returns the ID of the timer event.

I should replace the Long Period with my delay in Milliseconds.

And I should put my function name where the callback is currently. ??

So I just need to put the Scan code into a function that looks something like the code below outside the loop:

Code: Select all

void  Scan()
{
Scan code
}
Does this look right to you?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Analog Servo Feedback Question

Post by adafruit_support_rick »

Bill is taking some time off this week.
solardude wrote:I should replace the Long Period with my delay in Milliseconds.

And I should put my function name where the callback is currently. ??

So I just need to put the Scan code into a function that looks something like the code below outside the loop:
Sounds right to me!

User avatar
solardude
 
Posts: 145
Joined: Fri Oct 18, 2013 12:47 am

Re: Analog Servo Feedback Question

Post by solardude »

OK, I figured out how to call the Seek function via a timer every 60 mins so it only runs once every hour.

I have one more thing that I need help figuring out.

This for loop sweeps the servo from 0 to 180 degrees in 5 degree increments while a light sensor logs the brightest position. I am trying to figure out how to drive the servo to the maxLuxPosition variable AFTER the complete sweep has completed and the sweepForPeak() function stops.

Here is what I want to add to run only after the complete for loop has completed:

Code: Select all

Seek(myservo, 0, maxLuxPosition);
How do I add this code to the function below so it only runs after the servo sweep has completed? Any help is appreciated.


Here is the for loop:

Code: Select all

void sweepForPeak()
{
   for (pos = 0; pos < 180; pos += 5)
   {
    Seek(myservo, 0, pos);
    int luxReading = analogRead(luxPin);  // take a sensor reading here
    if (luxReading > maxLuxReading)
   {
      maxLuxReading = luxReading;
      maxLuxPosition = pos;
   }
  delay(500);
}} 

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Analog Servo Feedback Question

Post by adafruit_support_rick »

You mean like this?

Code: Select all

void sweepForPeak()
{
   for (pos = 0; pos < 180; pos += 5)
   {
     Seek(myservo, 0, pos);
     int luxReading = analogRead(luxPin);  // take a sensor reading here
     if (luxReading > maxLuxReading)
     {
       maxLuxReading = luxReading;
       maxLuxPosition = pos;
     }
     delay(500);
   }
   Seek(myservo, 0, maxLuxPosition);
}

User avatar
solardude
 
Posts: 145
Joined: Fri Oct 18, 2013 12:47 am

Re: Analog Servo Feedback Question

Post by solardude »

adafruit_support_rick wrote:You mean like this?

Code: Select all

void sweepForPeak()
{
   for (pos = 0; pos < 180; pos += 5)
   {
     Seek(myservo, 0, pos);
     int luxReading = analogRead(luxPin);  // take a sensor reading here
     if (luxReading > maxLuxReading)
     {
       maxLuxReading = luxReading;
       maxLuxPosition = pos;
     }
     delay(500);
   }
   Seek(myservo, 0, maxLuxPosition);
}
Yes indeed! It's alive and working now just as I had dreamed about just 30 days ago!

It was just a simple curly bracket, I'm learning quickly!

I really love Adafruit's online support, its top notch!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Analog Servo Feedback Question

Post by adafruit_support_rick »

Cool :D

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”