Analog Problems

Hello everyone!

I wrote this simple program in AVR studio 4 for my BabyO 48. Analog channel 0 (PC0) works accordingly but I can’t get channel 1 (PC1 or any other channel) to work. What am I doing wrong? Here’s my code:

Thanks!

Stephen

#include <pololu/orangutan.h>
#include <stdio.h>


unsigned char pot_1;
unsigned char pot_2;

int main()
{
   

    
    set_analog_mode(MODE_8_BIT);


		

	pot_1 = 0;			// Value of analog input to PC0
	pot_2 = 0;			// Value of analog input to PC1
	

		start_analog_conversion(IO_C0);		
		
		start_analog_conversion(IO_C1);
			
		

 while(1)
	{
		
	if (!analog_is_converting(IO_C0))

		{
				pot_1 = analog_conversion_result(IO_C0);
				start_analog_conversion(IO_C0);

				

		if(pot_1 >= 127)
		{
			set_digital_output(IO_B0, HIGH);
			set_digital_output(IO_B1, LOW);
		}
		else 
			{
			set_digital_output(IO_B1, HIGH);
			set_digital_output(IO_B0, LOW);
			}

		}
   
   
   	if (!analog_is_converting(IO_C1))

		{
				pot_2 = analog_conversion_result(IO_C1);
				start_analog_conversion(IO_C1);

				

		if(pot_2 >= 127)
		{
			set_digital_output(IO_D0, HIGH);
			set_digital_output(IO_D2, LOW);
		}
		else 
			{
			set_digital_output(IO_D2, HIGH);
			set_digital_output(IO_D0, LOW);
			}

		}


   
		        
	}
 
 }

Hello,
I am surprised that this code compiles at all! Did you include analog.h or orangutan.h at the beginning? You are making calls to analog_is_converting() with an argument when actually, it does not take any argument.

Your main problem, however, is that you are trying to do two conversions at once. Unfortunately, that is not possible on the AVR, since there is only a single A/D converter that is multiplexed between the different input lines. You will need to rearrange your code to alternate between the two ports.

If you still can’t get it to work, you should simplify your program to the minimal possible code that does not work and repost it. Telling us what your code is supposed to do and how it fails will also really help us debug it!

Good luck!
-Paul

Hello.

Another problem is that the IO_xx macros from the digitalIO library should not be used with the analog library. Rather, the arguments to these functions should be the channel number itself (e.g. 5 for ADC5, which happens to be pin PC5 on the atmega48).

- Ben

Hello!

The program that I wrote above was to learn about the ATmega48. I am a mechanical engineering student building a project for school. I have very little programming experience so any help would be greatly appreciated!

The project I’m building involves three pressure sensors connected through OpAmps for 3 analog inputs. I also have 6 air valves connected through transistors to the controller as digital outputs. Every minute I would read the 3 pressures sensors (doesn’t have to be simultaneously) and each sensor will control two digital outputs (the air valves) according to the pressure feedback. Could you please show me how to write the code to do multiplexing so I can read the three analog inputs?

Thank you very much!

Stephen

In general, to read an analog port, just use

value = analog_read(port);

There is probably no need for the more complicated background reads in your application. You could handle the logic for one at a time, then wait for a minute before you do it again.

-Paul

Hi Paul,
It works!

Thanks
Stephen