Voltage measuring system

Hello everybody,

I'm working on a voltage measuring system with Arduino.
It's a school project so i'm not looking for direct corrections to faults in the code (for now :wink: ).

#include <avr/io.h>

int Ain_0 = PC0; // analoge in 0
int Ain_1 = PC1; // analoge in 1
int LED_1 = PB1; // Voltage change (Red Led)
int LED_2 = PB2; // System OK. (Green Led)
int LED_3 = PB3; // System fault (Orange Led)
unsigned int ADCv_0, ADCv_1, ADCv_0r, ADCv_1r;

int Analoge_read(Apin, A_val, Ref_val)
	{
		ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
		// ADEN: Set to turn on ADC , by default it is turned off
		//ADPS2: ADPS2 and ADPS0 set to make division factor 32
		ADMUX= (1<<Apin); // ADC input channel set to Anal. pin ...
		ADCSRA |= (1<<ADSC); // Start conversion in free running mode

		      while (ADCSRA & (1<<ADSC)); // wait for conversion to complete
		      Ref_val = ADCW; // ADCW = ADCL and ADCH readout (10 bit total)
		         if (A_val == Ref_val)
		         {
		               PORTB |= (1 << LED_2);

		         }
		         else if (A_val != Ref_val)
		         {
		        	 A_val = Ref_val;
		        	 PORTB |= (1 << LED_1);
		         }
		         else
		         {
		        	 PORTB |= (1 << LED_3);
		         }

		 return A_val;
	}

int main(void){

DDRC |= (0 << Ain_0);
DDRC |= (0 << Ain_1);

DDRB |= (1 << LED_1);
DDRB |= (1 << LED_2);
DDRB |= (1 << LED_3);

while(1)
	{
		int Analoge_read(Ain_0, ADCv_0, ADCv_0r);
		int Analoge_read(Ain_1, ADCv_1, ADCv_1r);
	}
}

So, I'm hoping the code above will read an analog voltage and compare it with the previous readout.
The first run it will update (Red Led), the second run it should detect no change and signal with green led (in theory... no small voltage changes...).

This is my first project so I still have a lot to learn.
Maybe feedback on what i have? the amount of mistakes?

code based on: http://www.electroschematics.com/10053/avr-adc/

Friendly greetings,

Piet

That does not look like a regular Arduino program.

Why not use the standard analogRead() function?

So, I'm hoping the code above will read an analog voltage and compare it with the previous readout.

What happens when you try the program?

...R

I'm writing the code in C, it gives a better understanding how the atmega 328p works.
It is also the way we learned programming Arduino, the courses are given with the idea that if you can understand the datasheet, you can program any chip with C compatibility.

What happens when you try the program?

That's the problem for now, i can not test it due to missing hardware.
Even if I had the material to test it, it doesn't have an output part so I would have no idea if it worked.

I thought it would be a good idea to ask feedback about the part I have for now, so that once I get to the part that I can have output, I already know for sure that a part was coded correctly (= limit the fault search).

piet_v:
That's the problem for now, i can not test it due to missing hardware.
Even if I had the material to test it, it doesn't have an output part so I would have no idea if it worked.

Then you need to get the hardware and add suitable output code to the program.

I thought it would be a good idea to ask feedback about the part I have for now, so that once I get to the part that I can have output, I already know for sure that a part was coded correctly (= limit the fault search).

IMHO it is almost impossible to know if a program will work just by reading the code. And with a program such as yours that makes liberal use of the Atmega registers it would be several times more difficult.

Programs should be developed and tested as continuous process, and the Arduino system makes that especially easy. Part of the design of a program should be the code needed to demonstrate that it behaves as required.

...R

I miss a setup() function so I take it you don't use the Arduino IDE?

Also do clean up your indentation, that will also make it much easier to read. Ctrl-T in the Arduino IDE does that for you.

Hi,
Does your code compile and load to Arduino?
What model Arduino are you using?

If you have no display device, and you are using the Arduino IDE you should be able to use the Serial.print command to output to the IDE monitor screen.

Tom... :slight_smile:

Thanks for the feedback everybody.

The hardware is ordered.

@Tom; i'm using Arduino Uno.
I'll put on the output part ASAP and see what it gives.
Does the Serial.print command work (have a function?) for an ADC setup without any real input?

Hi,
Does your code compile?

You can compile your code without uploading it, so you can check for program syntax errors.

Thanks.. Tom.. :slight_smile:

piet_v:
Does the Serial.print command work (have a function?) for an ADC setup without any real input?

Serial.print(A_val);  // Prints A_val
Serial.print("  ");   // Prints a couple of spaces after A_Val
Serial.println(B_val);  //Prints B_val after the spaces and then sends a Carriage Return so the next Serial.print will be on a new line.

This will print A_val and B_val on a line on the monitor.
You will have to read in the Reference to Arduino, look in the help tag of the IDE, about Serial.begin and Serial.print, Serial.println.

Also google Arduino Serial.print

Tom... :slight_smile:

TomGeorge:
Hi,
Does your code compile?

You can compile your code without uploading it, so you can check for program syntax errors.

Thanks.. Tom.. :slight_smile:

I'm writing the code in eclipse, syntax errors are shown in eclips and are not present.

Will take a look at your suggestion for the print command.

Thx

Hi,
I am not familiar with Eclipse.
But if you are going to load your code to an Arduino UNO using the USB socket on the board, you will need to change the structure of your code as you will be loading via a bootloader.

void main() is not used.

void loop() is used.

You do not need to use while(1) to force a program to loop around the loop.

In your course have they actually uploaded to a UNO and prove the code works?

Tom... :slight_smile:
(Got to go too work..)

It works, the code will compile and upload.
Winavr is a compiler you can use in combination with eclipse.

Eclipse is freeware btw, you should check it out.

I've never used the registers directly or the port commands, so I can't help you with any of that...

But, you can use the serial monitor to "look at" your analog reads, and you can write a little test to test the LEDs, etc. Just write and test a few lines of code at a time.... Don't try to write the whole program before trying it out! i.e. Read one of your analog inputs (and send the results to the serial monitor). When that works add the next analog read, then write to an LED, then write to the other LED, etc. Then, add some if-statements to turn on/off the LEDs as appropriate.

So, I'm hoping the code above will read an analog voltage and compare it with the previous readout.
The first run it will update (Red Led), the second run it should detect no change and signal with green led (in theory... no small voltage changes...).

With ANY analog-to-digital conversion you can be on the "hairy edge" of incrementing/decrementing by one bit. So, you should look for a change of at least two counts. And in the real world, you'd want to specify how much change to look for.

Also, you need some time (maybe 50mS or more) if you expect to see the LED flash. So you need to put a delay in the loop, or at least add some timing/delay to the LED to hold it on long enough to see it.

I'm writing the code in C, it gives a better understanding how the atmega 328p works.

Yes, the ports & registers are more "internal" but the [u]regular Arduino library[/u] is also C/C++. And, almost no Arduino/ATmega programming is standard ANSI/ISO C++. It's all Arduino/ATmega specific. Standard C/C++ can't read/write to ports or pins on the chip, and standard C/C++ expects you to have a keyboard, monitor, and disc space.

I guess you are taking a class and you have to follow the course, but if you REALLY wanted to get into the details of how the ATmega chip works internally you wouild be using Assembly language. Each assembly language instruction is a 1:1 human-readable "translation" of the binary instruction code. But, I would NOT recommend Assembly as your first programming language. (I've use a couple of different Assembly languages, but I've never programmed the ATmega in assembly.)

piet_v:
Eclipse is freeware btw, you should check it out.

I did, years ago. Now I just use Geany - much simpler.

...R

Robin2:
I did, years ago. Now I just use Geany - much simpler.

...R

Looks good.
I'll try it out for my next project.

ADMUX= (1<<Apin); // ADC input channel set to Anal. pin ...
This line is probably a bug - look at the register description closely.
Also your port manipulation does not work as you think it works.