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 ).
#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