2 csatornás feszültségmérő

alfa007
Újonc
Újonc
Hozzászólások: 1
Csatlakozott: 2019. június 12. szerda, 9:48

2 csatornás feszültségmérő

Hozzászólás Szerző: alfa007 »

Sziasztok!

Néhány hónapja foglalkozom Arduino programozással. Interneten találtam kódot 4 csatornás feszültségmérőhöz, ezt átjavítottam 2 csatornásra. Mindössze annyi problémám van, hogy lcd-n kiírt értéket szeretném a tizedes jel nélkül kiíratni. Mit kell ehhez módosítanom?

Kód: Egész kijelölése

#include <LiquidCrystal.h>

// number of analog samples to take per reading, per channel
#define NUM_SAMPLES 10
// voltage divider calibration values
#define DIV_1    11.1346
#define DIV_2    11.1969
#define DIV_3    11.0718
#define DIV_4    11.0718
// ADC reference voltage / calibration value
#define V_REF    4.991

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sum[4] = {0};                // sums of samples taken
unsigned char sample_count = 0;  // current sample number
float voltage[4] = {0.0};        // calculated voltages
char l_cnt = 0;                  // used in 'for' loops

void setup()
{
    lcd.begin(16, 2);
}

void loop()
{
    // take a number of analog samples and add them up
    while (sample_count < NUM_SAMPLES) {
        // sample each channel A2 to A5
        for (l_cnt = 0; l_cnt < 4; l_cnt++) {
            sum[l_cnt] += analogRead(A2 + l_cnt);
        }
        sample_count++;
        delay(10);
    }
    // calculate the voltage for each channel
    for (l_cnt = 0; l_cnt < 4; l_cnt++) {
        voltage[l_cnt] = ((float)sum[l_cnt] / (float)NUM_SAMPLES * V_REF) / 1024.0;
    }
    // display voltages on LCD
    // each voltage is multiplied by the resistor network
    // division factor to calculate the actual voltage
    // voltage 1 - A (pin A2)
    lcd.setCursor(0, 0);
    lcd.print("A ");
    lcd.print(voltage[0] * DIV_1, 1);
    lcd.print("V ");
    // voltage 2 - B (pin A3)
    lcd.setCursor(8, 0);
    lcd.print("B ");
    lcd.print(voltage[1] * DIV_2, 1);
    lcd.print("V ");
    // voltge 3 - C (pin A4)
    lcd.setCursor(0, 1);
    lcd.print("C ");
    lcd.print(voltage[2] * DIV_3, 1);
    lcd.print("V ");
    // voltage 4 - D (pin A5)
    lcd.setCursor(8, 1);
    lcd.print("D ");
    lcd.print(voltage[3] * DIV_4, 1);
    lcd.print("V ");
    // reset count and sums
    sample_count = 0;
    for (l_cnt = 0; l_cnt < 4; l_cnt++) {
        sum[l_cnt] = 0;
    }
} :x 
Avatar
csegebiga
Chipgyilok
Hozzászólások: 288
Csatlakozott: 2015. március 27. péntek, 21:27

Re: 2 csatornás feszültségmérő

Hozzászólás Szerző: csegebiga »

https://www.arduino.cc/reference/en/lan ... n/intcast/
irgészérték:
int(x) //4.9==> 4
kerekítéssel:
int(x+0,5) //4.9==> 5
Válasz küldése