I'm trying to display the potentiometer's converted value(Tension) into 4 7-segment displays in real time, how do I do it?

119 Views Asked by At

I'm currently doing a project where I need do put the values in real time whenever I adjust the potentiometer, however, in my current code, it only updates the value when I reset my arduino. I'm wondering if there's anything misplaced in my code. Any help would be greatly appreciated. Thanks!

By the way, segmentos are the numbers from 0 to 9 in a 7-segment display. Then, escreve_display selects these numbers to the respective display.

It already displays the value correctly, it's just only updating when I reset.


#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>

uint8_t segmentos[10]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
    
void escreve_display(uint32_t valor, int display){
    
    PORTB &= ~(0x0F); // desliga 4 ultimos bits, apaga o display
    PORTD = segmentos[valor];
    switch(display)
    {
        case 0:
            PORTB |= (1 << 0);
            break;
        case 1:
            PORTB |= (1 << 1);
            break;
        case 2:
            PORTB |= (1 << 2);
            break;
        case 3:
            PORTD |= (1 << 7);
            PORTB |= (1 << 3);
            break;      
    }
    
}



int x = 0;

ISR(TIMER0_OVF_vect){

    TCNT0 = 6;      
    
        
    uint16_t valor_ADC = ((ADCH << 8)| ADCL);
        
    uint32_t valor_display = (((uint32_t)valor_ADC)*5000)/1023;

    
    if (x > 3)
    {
        x = 0;
    }

    switch(x)
    {
        case 0:
            escreve_display((valor_display%10),x);
            break;
        case 1:
            escreve_display(((valor_display/10)%10),x);
            break;
        case 2:
            escreve_display(((valor_display/100)%10),x);
            break;
        case 3:
            escreve_display(((valor_display/1000)%10),x);
            break;
    }
    
    x++;
    
    
}

void config_timer(){
    
    TCNT0 = 6;
    TCCR0A = 0x00;
    TCCR0B = 0x03;
    TIMSK0 = 0x01;
}

void config_ios(){
    
    DDRB = 0b00001111;
    DDRD = 0b11111111;
    ADMUX = 0b01000000;
    ADCSRA = 0b10000111;
}


int main(void)
{
    config_timer();
    config_ios();
    sei();
    
    
    while (1)
    {
        ADCSRA |= (1<<ADSC);
        while (ADCSRA & (1<<ADSC));
    }

}
0

There are 0 best solutions below