Author Topic: How Would I Go About Writing A Program For This  (Read 2028 times)

0 Members and 1 Guest are viewing this topic.

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
How Would I Go About Writing A Program For This
« on: December 23, 2009, 07:02:17 PM »
Hi every one

I need to write a 2 programs, 1 really easy and one a bit harder and i need some help on what to include and how to go about them.I am starting to get my head around the C programming but i have still got a long way to go.

The easy program involves this. When i push a swithch/button that is connected to a certain port i want the servo to rotate for a specified period of time.

The harder one is for the putting ball return i am making. I need the program to do the following. When the golf ball blocks the light from the LED, raise a servo for a specified amount of time, hold it for a second, the return the servo back to its original position lowering the platform to where it started. after this i want the program to reset and do the  same when it senses the next ball and so on.

All help is greatly appreciated and like i said i have grasped the basic concept but i am stiil learning all the indepth and more detailed stuff.

Thanks very much

Joker94
« Last Edit: December 25, 2009, 04:17:38 AM by Joker94 »

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: How Would I Go About Writing A Program For ThissHi
« Reply #1 on: December 23, 2009, 07:27:54 PM »
Well your description is pretty good and that's how you start. Write a detailed description of what you want the program to do.
Like this:

1- wait for input A to go low, button pushed
2- set servo to rotate CW or CCW
3 - wait x milliseconds
4- set servo to stop.
5- wait for input A to go high, button released
6 - go back to #1

Flesh out the details and write the code.

Hopefully in your C programming environment you can step through your code to see if its doing what you want.

What processor are you write code for?
What compiler are you using?

When you have some code and get stuck post it back here and detail what it is or is not doing.

This is a good task to start with. Once you get this one working I believe the second one won't be that hard.

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: How Would I Go About Writing A Program For ThissHi
« Reply #2 on: December 23, 2009, 07:43:53 PM »
Thanks very much waltr

i am writing it for the atmega 8. and my compilers available are avr studio 4 and DEV-C++

i'll give writing some code a go

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: How Would I Go About Writing A Program For This
« Reply #3 on: December 25, 2009, 04:25:57 AM »
For my second problem/program

How do i start the program when it senses light under the throshold. At the moment i can get the sensor to detect light under the threshold but the servo is turning from the minute i turn on the power.

And i am thinking i am also gonna need to stop the program when there is no ball in there and then start again when the ball is sensed.

and my final question is, how do add a delay to my program as i need to add a delay when the platform has been lifted to its final hight.

Thanks for all your help.

Joker94

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: How Would I Go About Writing A Program For This
« Reply #4 on: December 25, 2009, 06:53:17 AM »
Couldn't you just:
Code: [Select]
If a2dconverter <threshhold
(
program in here
)
Else
(
Something else
)
Just as a reference, i don't know the exact keywords :-\
And for the delay, cant you just:
Delay_ms(time)?
Howdy

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: How Would I Go About Writing A Program For This
« Reply #5 on: December 25, 2009, 11:00:08 AM »
For my second problem/program

How do i start the program when it senses light under the throshold. At the moment i can get the sensor to detect light under the threshold but the servo is turning from the minute i turn on the power.

And i am thinking i am also gonna need to stop the program when there is no ball in there and then start again when the ball is sensed.

and my final question is, how do add a delay to my program as i need to add a delay when the platform has been lifted to its final height.

Thanks for all your help.

Joker94
In your main() you should initialize all of your hardware and set your servos to off or stop. Then turn a servo on as needed from a condition such as a light sensor threshold.
Same with the ball. Only do something when the ball is sensed.

Have you looked at and read through any sample code for an atmega 8 until you understand what the code is doing and why? This is a good way to learn programing methods. Do some google searching for code examples, there are many available.

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: How Would I Go About Writing A Program For This
« Reply #6 on: December 25, 2009, 05:09:52 PM »
Do some google searching for code examples, there are many available.
This forum has a lot of code posts for this kind of thing.
Search around a bit, they're everywhere!
Howdy

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: How Would I Go About Writing A Program For This
« Reply #7 on: December 25, 2009, 06:41:25 PM »
well this was my first attempt so please dont be to harsh as i know there is probably a number of mistakes in there.

Code: [Select]
#include "SoR_Utils.h"
#include "avr/io.h"
#include "util/delay.h"

         int main(void)
         {
             int sensor_left=0;
             int threshold=100;
             
            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

            servo_left=0x00
           
            while(1)
            {
                   
                //store sensor data
            sensor_left=a2dConvert8bit(5);
           
            if (sensor_left< threshold)
            {
                servo_left(
                {
                 DDRD = 0xff;
             PORTD = 0xff;
                for(int i=0; i < 100; i++)
            {
        PORTD = 0xff;
        _delay_ms(2);
          PORTD = 0x00;
            _delay_ms(20);
             }
            return 1;
           }
           )
           
           else if(sensor_left>threshold)
           {
           _delay_ms(100)
           {
                 DDRD = 0xff;
             PORTD = 0xff;
                for(int i=0; i < 100; i++)
            {
        PORTD = 0xff;
        _delay_ms(2);
          PORTD = 0x00;
            _delay_ms(20);
             }
            return 1;
           }
           
           return 0;
           
}

thanks all for your help it is greatly appreciated ;D

Joker94

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: How Would I Go About Writing A Program For This
« Reply #8 on: December 25, 2009, 11:33:40 PM »
Ok, that is a start. What I do see wrong in the code is the three returns! Where are they returning code execution to?
A return is only used at the end of a function that was called.

Inside the main() you do need a continuous loop:
 while(1) {
      // DO THIS OVER AND OVER
 }

But you must stay in this loop with a u-controller.

            for(int i=0; i < 100; i++)
            {
              PORTD = 0xff;
              _delay_ms(2);
               PORTD = 0x00;
                 _delay_ms(20);
             }
             return 1;
I don't see the purpose of this for loop. It just wrtie the same thing to the two ports 99 times and should not have the return 1.

A couple of suggestions:

It looks like you missed a ')' here: servo_left(
it should be: servo_left()

Try to write a comment on every line of code. That will help us understand what you are trying to do and when you look at this code in a few months you will remember what you have the code doing.

Use white space (indents) to improve the readability of your code. You started out good but then:
Code: [Select]
            while(1)
            {
                   
                //store sensor data
            sensor_left=a2dConvert8bit(5);
           
            if (sensor_left< threshold)
            {
                servo_left(
                {
                 DDRD = 0xff;
             PORTD = 0xff;
                for(int i=0; i < 100; i++)
            {
        PORTD = 0xff;
        _delay_ms(2);
          PORTD = 0x00;
            _delay_ms(20);
             }
            return 1;
           }
           )
           
           else if(sensor_left>threshold)
           {
           _delay_ms(100)
           {
                 DDRD = 0xff;
             PORTD = 0xff;
                for(int i=0; i < 100; i++)
            {
        PORTD = 0xff;
        _delay_ms(2);
          PORTD = 0x00;
            _delay_ms(20);
             }
            return 1;
           }
           
           return 0;
           
}

Should be:
Code: [Select]
           
    while(1)                             
    {                                     
                                         
        //store sensor data               
        sensor_left=a2dConvert8bit(5);   
                                         
        if (sensor_left < threshold)     
        {                                 
            servo_left()                 
                                         
            DDRD = 0xff;                 
            PORTD = 0xff;                 
            for(int i=0; i < 100; i++)   
            {                             
                PORTD = 0xff;             
                _delay_ms(2);             
                PORTD = 0x00;             
                _delay_ms(20);           
            }                             
        }                                 
        else if(sensor_left > threshold) 
        {                                 
                _delay_ms(100)           
                                         
                DDRD = 0xff;             
                PORTD = 0xff;             
                for(int i=0; i < 100; i++)
                {                         
                    PORTD = 0xff;         
                    _delay_ms(2);         
                    PORTD = 0x00;         
                    _delay_ms(20);       
                }                         
         }                               
    }   

This is easier to see what code is within an IF or FOR conditional.

I also removed the returns and extra {.
Hope that helps.

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: How Would I Go About Writing A Program For This
« Reply #9 on: December 26, 2009, 06:32:30 AM »
Brilliant, i can't even start to thank you enough ;D

i'll have another go and report back. at the moment i only have 3 errors in my code  ;D

so hopefully when i figure them out i can start the test and change process


thanks you so very much

Joker94

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: How Would I Go About Writing A Program For This
« Reply #10 on: December 26, 2009, 07:05:13 AM »
well i think i am on the right track.

don't ask me about my setting out, i don't know if it is up to scratch or not.

the code i have is as follows

Code: [Select]
#include "SoR_Utils.h"
#include "avr/io.h"
#include "util/delay.h"


         int main()
{
         
         int sensor_left=0;
         int threshold=100;
         
         configure_ports();
         a2dInit(); // initialize analog to digital converter (ADC)
    a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling
       a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage

while(1)                             
    {                                     
                                         
        //store sensor data               
        sensor_left=a2dConvert8bit(5);   
                                         
        if (sensor_left < threshold)     
        {                                                 
                                         
            DDRD = 0xff;                 
            PORTD = 0xff;                 
            for(int i=0; i < 100; i++)   
            {                             
                PORTD = 0xff;             
                _delay_ms(2);             
                PORTD = 0x00;             
                _delay_ms(20);           
            }                           
        }                                 
        else if(sensor_left > threshold) 
        {                                 
                _delay_ms(100);
}         
                                         
          {
DDRD = 0xff;           
            PORTD = 0xff;             
            for(int i=0; i < 100; i++)
            {                         
               PORTD = 0xff;         
               _delay_ms(2);         
               PORTD = 0x00;       
               _delay_ms(20);       
               }                         
}
      }                                 

}

The only problem now is the errors i get wheni attempt to compile it. I originally had other errors but i was able to snuff them out my self and fix the problem. The current errors are

C:\Myrobots\PuttingBallReturn_13_34\default/../PuttingBallReturn_13_34.c:13: undefined reference to `a2dInit'
C:\Myrobots\PuttingBallReturn_13_34\default/../PuttingBallReturn_13_34.c:14: undefined reference to `a2dSetPrescaler'
C:\Myrobots\PuttingBallReturn_13_34\default/../PuttingBallReturn_13_34.c:15: undefined reference to `a2dSetReference'
C:\Myrobots\PuttingBallReturn_13_34\default/../PuttingBallReturn_13_34.c:21: undefined reference to `a2dConvert8bit'

Build failed with 4 errors and 0 warnings...

I thought these  were done through "SoR_Utils.h" as i have never had any problems like this before.

Thanks very much, upto this point your help has been invaluable to my learning and my current program

Joker94

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: How Would I Go About Writing A Program For This
« Reply #11 on: December 26, 2009, 10:57:38 AM »
The "undefined reference to" means that the compiler/linker could not find these functions. Find and open the header file "SoR_Utils.h" and check that those functions are listed in the header file. Check the spelling for typos.

#include "avr/io.h"
#include "util/delay.h"

These two header file names don't look right! a '/' is not normally in a file name are you sure that these should be:

#include "avr_io.h"
#include "util_delay.h"

I have a feeling that the four a2d functions are declared in the avr_io.h.


I still don't really know what your code is suppose to do after checking for the a2d threshold.

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: How Would I Go About Writing A Program For This
« Reply #12 on: December 26, 2009, 05:32:27 PM »
cool thanks i'll give then a go.

Sorry i didn't tell you about my plan earlier.

I am building a putting ball return thing to practise my putting. I am using a photoresistor  and a led to sens when the ball is and is not there, as when the ball is there it blocks the light. When the light is blocked i want the platform to be raised to a specified hight by the servo , wait for the delay so that the ball has left the ramp and then lower its self to its original position ready for the next shot. and be able to do that over and over.

i hope this helps, and again thanks so very much for all your help, uch appreciated.

Joker94

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: How Would I Go About Writing A Program For This
« Reply #13 on: December 26, 2009, 05:47:11 PM »
ok tryed changing what you suggested and when i changed the name of the two included files with the "/" in it it gave me messages saying that there were no such files, and when i removed them completely to see what happens it gives me around 9 errors saying that there is no errors to _delay_ms so in regards to those file names i think they are right and what they are ment to be.

so i'll keep at it and see what i can come up with.

thanks again Waltr

Joker94

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: How Would I Go About Writing A Program For This
« Reply #14 on: December 26, 2009, 06:25:15 PM »
Yep, that's a way to find out. So it looks like those file names are correct.

Did you open the header file in a text editor and look the the names for the a2d functions. If they are not in those file look at the other header files in the directory.

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: How Would I Go About Writing A Program For This
« Reply #15 on: December 27, 2009, 04:22:23 AM »
Ok, thanks waltr. I'll check the header file now now.

Thanks

Joker94

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: How Would I Go About Writing A Program For This
« Reply #16 on: December 27, 2009, 07:09:14 PM »
i checked the heard file "SoR_Utils.h" and it has a #include "a2d.h" when i looked in a2d.h it had references to all of the functions in my code.

So i'll keep playing and see what i come up with

Thanks waltr

Joker94

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: How Would I Go About Writing A Program For This
« Reply #17 on: December 29, 2009, 05:55:57 AM »
Well i finally compiled it with no errors. Now all i ahve to do is furthur develope the code to do what i want.

At the moment when i turn the system on it starts immedieatly not with the change in light and when the light level changes the servo just jitters.

Here is my code, any tips or tricks to get my code to do what i have intended, if not i am gradually getting it to the point to be suitable.

Code: [Select]
#include "SoR_Utils.h"
#include "avr/io.h"
#include "util/delay.h"


         int main()
{
         
         int sensor_left=0;
         int threshold=50;
         
         configure_ports();
         a2dInit(); // initialize analog to digital converter (ADC)
    a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling
       a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage

while(1)                             
    {                                     
                                         
        //store sensor data               
        sensor_left=a2dConvert8bit(5);   
                                         
        if (sensor_left < threshold)     
        {                                                 
                                         
            DDRD = 0xff;                 
            PORTD = 0xff;                 
            for(int i=0; i < 100; i++)   
            {                             
                PORTD = 0xff;             
                _delay_ms(2);             
                PORTD = 0x00;             
                _delay_ms(20);           
            }                           
        }                                 
        else if(sensor_left > threshold) 
        {                                 
                _delay_ms(100);
}         
                                         
          {
DDRD = 0xff;           
            PORTD = 0xff;             
            for(int i=0; i < 100; i++)
            {                         
               PORTD = 0xff;         
               _delay_ms(2);         
               PORTD = 0x00;       
               _delay_ms(20);       
               }                         
}
      }                                 
return 0;
}

thanks very much

Joker94

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: How Would I Go About Writing A Program For This
« Reply #18 on: December 29, 2009, 09:32:53 AM »
Comments would help us know what you are thinking the code should do. Other wise we must guess.

This code for example:
            for(int i=0; i < 100; i++)   
            {                             
                PORTD = 0xff;             
                _delay_ms(2);             
                PORTD = 0x00;             
                _delay_ms(20);           
            }
 
I guess you have the servo connected to one of the pins on PortD. Then you are sending a 2msec high pulse to the servo, outputting a low for 20msec and doing this 100 times. Is your servo un-modified and goes to a position proportional to the pulse width or is it a servo modified for continuous rotation?

Do you have any LEDs connected to an output port. These can be helpful in debugging. For example: ligth the LED if the threshold is lower than the light level and turn it off when the threshold is higher than the light level.

You do the exact same values for both above and below the threshold. So the servo does the same thing regardless of the light level. Is this what you want? Why all 8 pins of the port?

What does this line of code do?
DDRD = 0xff;
Is this setting the PortD direction?
If so you only need to set it once in the Init part of your code.

You're getting there so keep at it.

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: How Would I Go About Writing A Program For This
« Reply #19 on: December 29, 2009, 09:43:32 PM »
Comments would help us know what you are thinking the code should do. Other wise we must guess.

I will add them right away. Thanks for reminding me.

I guess you have the servo connected to one of the pins on PortD. Then you are sending a 2msec high pulse to the servo, outputting a low for 20msec and doing this 100 times. Is your servo un-modified and goes to a position proportional to the pulse width or is it a servo modified for continuous rotation?

Yes my servo is connected to pin PD0. And my servo is modifyed for continuous rotation.

Do you have any LEDs connected to an output port. These can be helpful in debugging. For example: ligth the LED if the threshold is lower than the light level and turn it off when the threshold is higher than the light level.

Yes I have a LED connected like in the $50 robot, Port PD4.

Great idea, a more visual and informative way than watching a servo horn move.

You do the exact same values for both above and below the threshold. So the servo does the same thing regardless of the light level. Is this what you want? Why all 8 pins of the port?

Yeah i want the servo to do the exact same thing depending on if the light is above or below. Yes it is what i want. I dont know why it is all 8 pins, it was un intentional, i'll try and change it.

Thanks very very much for your time waltr

Joker94

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: How Would I Go About Writing A Program For This
« Reply #20 on: December 30, 2009, 08:39:22 AM »
Ok, why don't you try breaking down the project and first get the servo to do what you want.

Write code that does not do any thing with the A2D. Write a loop that rotates the servo, then stops the servo, then reverses the servo rotation.
This will teach you how to handle commanding the servo.
Then add code to start/stop/reverse the servo from a push button.

Then set aside the servo code and write just the a2d code turning on LED on for over threshold and off for under. Once you have both of these working then put the two together.

For a continuously rotating servo a pulse width of about 1.5msec should have the servo stopped. a shorter pulse and the servo rotates in one direction and a longer pulse it rotates in the other direction. The pulses must continue at about 20msec intervals even if you want the servo not to rotate.

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: How Would I Go About Writing A Program For This
« Reply #21 on: December 30, 2009, 11:39:13 PM »
Brilliant idea waltr

and it will be alot more advantagous learning in stages.

I'll give it a crack

thanks very much for your time waltr

joker94

 


Get Your Ad Here