BaroneRosso.it - Forum Modellismo

BaroneRosso.it - Forum Modellismo (https://www.baronerosso.it/forum/)
-   Circuiti Elettronici (https://www.baronerosso.it/forum/circuiti-elettronici/)
-   -   Arduino Uno: chi ci gioca? (https://www.baronerosso.it/forum/circuiti-elettronici/369985-arduino-uno-chi-ci-gioca.html)

andycar 21 maggio 17 01:12

Dividere un problema complesso in tanti problemi semplici, è il consiglio migliore che mi abbiano mai dato...

lambdafly 21 maggio 17 19:18

Citazione:

Originalmente inviato da andycar (Messaggio 5040895)
Dividere un problema complesso in tanti problemi semplici, è il consiglio migliore che mi abbiano mai dato...

Divide et impera. Così i Romani governavano le province duemila e passa anni fa e così si fa ancora oggi nello sviluppo del software.

dex1 22 maggio 17 19:05

Citazione:

Originalmente inviato da aero330 (Messaggio 5040611)
Allora:
1) collega la scheda arduino al PC
2) apri l'IDE di arduino, vai su "Strumenti ->Scheda" e selezioni quella che utilizzi
e poi "Strumenti -> Programmatore -> AVRISP mkII"
3) scarica i due file ZIP che ti ho messo in allegato
4) vai su "Strumenti -> Sketch -> #includi libreria -> Aggiungi libreria da file .ZIP"
e cerca i due file "Adafruit_GFX.ZIP" e "Adafruit_ST7735".
5) chiudi tutto e riapri l'IDE di arduino
6) "File -> Nuovo", cancelli tutto e incolli questo:
Codice:

#define sclk 13
#define mosi 11
#define cs  10
#define dc  9
#define rst  8  //  reset

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>

Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

// number of analog samples to take per reading, per channel
#define NSAMP 100 // number of samples to take befor displaying

// voltage divider calibration values
#define Dv1    11.00
#define Dv2    11.001
#define Dv3    11.00
#define Dv4    10.985

// ADC reference voltage / calibration value
#define VREF    5.00

int sum[4] = {0};                // sums of samples taken
unsigned char Scount = 0;  // current sample number
float AVvolts[4] = {0.0};        // calculated voltages
char cnt1 = 0;                  // used in 'for' loops

float V_max[4] = {0.00 ,0.00, 0.00, 0.00};
float V_min[4] = {100.00, 100.00, 100.00, 100.00};

void setup()
{
  tft.initR(INITR_BLACKTAB);  // initialize a ST7735S chip, black tab
  tft.fillScreen(ST7735_BLACK); //  clear screen
  tft.setTextColor(ST7735_WHITE);
  tft.drawRoundRect(2, 20, 120, 110, 5,ST7735_WHITE);
  tft.setTextSize(1);
  tft.setCursor(5,0);
  tft.println("4 channel voltmeter");
  tft.setTextColor(0XFF00);
  tft.setCursor(0,140);
  tft.println(" Caution max voltage        55vdc");
}

       
void loop()
{
    // take a number of analog samples and add them up
    while (Scount < NSAMP) {
        // sample each channel A0 to A3
        for (cnt1 = 0; cnt1 < 4; cnt1++) {
            sum[cnt1] += analogRead(A0 + cnt1);
        }
        Scount++;
        delay(10);
    }
   
    // calculate the voltage for each channel
    for (cnt1 = 0; cnt1 < 4; cnt1++) {
        AVvolts[cnt1] = ((float)sum[cnt1] / (float)NSAMP * VREF) / 1024.0;
        if(AVvolts[cnt1] > V_max[cnt1]){ //get MAX value of sample
          V_max[cnt1] = AVvolts[cnt1];
        }
        if(AVvolts[cnt1] < V_min[cnt1]){ //get MIN value of sample
          V_min[cnt1] = AVvolts[cnt1];
        }       
    }
    // display voltages on TFT LCC Display
 
    // voltage 1 - V1(pin A0) 
    tft.setTextColor(ST7735_YELLOW,ST7735_BLACK); // set color for V1
    tft.setTextSize(2);
    tft.setCursor(15, 40);
    tft.print("V1 ");
    tft.print(AVvolts[0] * Dv1, 1);
    tft.print("V ");
    // voltage 2 - V2(pin A1)
    tft.setTextColor(ST7735_GREEN,ST7735_BLACK);// set color for V2
    tft.setCursor(15, 60);
    tft.print("V2 ");
    tft.print(AVvolts[1] * Dv2, 1);
    tft.print("V ");
    // voltge 3 - V3(pin A2)
    tft.setTextColor(ST7735_CYAN,ST7735_BLACK);// set color for V3
    tft.setCursor(15, 80);
    tft.print("V3 ");
    tft.print(AVvolts[2] * Dv3, 1);
    tft.print("V ");
    // voltage 4 - V4(pin A3)
    tft.setTextColor(ST7735_WHITE,ST7735_BLACK);// set color for V4
    tft.setCursor(15, 100);
    tft.print("V4 ");
    tft.print(AVvolts[3] * Dv4, 2);
    tft.print("V ");
    tft.drawRoundRect(2, 20, 120, 110, 5,ST7735_WHITE);
    // reset count and sums
    Scount = 0;
    for (cnt1 = 0; cnt1 < 4; cnt1++) {
        sum[cnt1] = 0;
    }
}

7)clicca in alto sulla freccia a destra ("carica")
8)se tutto è andato a buon fine non ottieni nessun errore ma solo "caricamento "completato"
9) fatto

Ho preso il codice quello completo con le 4 tensioni lette e aggiunto il calcolo dei valori massimi/minimi per ciascun canale.
L'unica cosa è che non li visualizzi perchè non c'è abbastanza spazio sul display, a meno che tu non mi dica come fare (esempio: visualizzo ogni singolo canale ciclicamente con sotto il max/min relativo, oppure i 4 canali contemporaneamente ma con i valori max/min scrittti a fianco più piccoli ecc ecc)

P.s. non ho il display in questione per cui sto facendo tutto al buio :lol:

:approved:

grazie,
appena mi arrivano i display mi metto a sperimentare.

dex1 23 maggio 17 16:54

Citazione:

Originalmente inviato da aero330 (Messaggio 5040611)
Allora:
1) collega la scheda arduino al PC
2) apri l'IDE di arduino, vai su "Strumenti ->Scheda" e selezioni quella che utilizzi
e poi "Strumenti -> Programmatore -> AVRISP mkII"
3) scarica i due file ZIP che ti ho messo in allegato
4) vai su "Strumenti -> Sketch -> #includi libreria -> Aggiungi libreria da file .ZIP"
e cerca i due file "Adafruit_GFX.ZIP" e "Adafruit_ST7735".
5) chiudi tutto e riapri l'IDE di arduino
6) "File -> Nuovo", cancelli tutto e incolli questo:
Codice:

#define sclk 13
#define mosi 11
#define cs  10
#define dc  9
#define rst  8  //  reset

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>

Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

// number of analog samples to take per reading, per channel
#define NSAMP 100 // number of samples to take befor displaying

// voltage divider calibration values
#define Dv1    11.00
#define Dv2    11.001
#define Dv3    11.00
#define Dv4    10.985

// ADC reference voltage / calibration value
#define VREF    5.00

int sum[4] = {0};                // sums of samples taken
unsigned char Scount = 0;  // current sample number
float AVvolts[4] = {0.0};        // calculated voltages
char cnt1 = 0;                  // used in 'for' loops

float V_max[4] = {0.00 ,0.00, 0.00, 0.00};
float V_min[4] = {100.00, 100.00, 100.00, 100.00};

void setup()
{
  tft.initR(INITR_BLACKTAB);  // initialize a ST7735S chip, black tab
  tft.fillScreen(ST7735_BLACK); //  clear screen
  tft.setTextColor(ST7735_WHITE);
  tft.drawRoundRect(2, 20, 120, 110, 5,ST7735_WHITE);
  tft.setTextSize(1);
  tft.setCursor(5,0);
  tft.println("4 channel voltmeter");
  tft.setTextColor(0XFF00);
  tft.setCursor(0,140);
  tft.println(" Caution max voltage        55vdc");
}

       
void loop()
{
    // take a number of analog samples and add them up
    while (Scount < NSAMP) {
        // sample each channel A0 to A3
        for (cnt1 = 0; cnt1 < 4; cnt1++) {
            sum[cnt1] += analogRead(A0 + cnt1);
        }
        Scount++;
        delay(10);
    }
   
    // calculate the voltage for each channel
    for (cnt1 = 0; cnt1 < 4; cnt1++) {
        AVvolts[cnt1] = ((float)sum[cnt1] / (float)NSAMP * VREF) / 1024.0;
        if(AVvolts[cnt1] > V_max[cnt1]){ //get MAX value of sample
          V_max[cnt1] = AVvolts[cnt1];
        }
        if(AVvolts[cnt1] < V_min[cnt1]){ //get MIN value of sample
          V_min[cnt1] = AVvolts[cnt1];
        }       
    }
    // display voltages on TFT LCC Display
 
    // voltage 1 - V1(pin A0) 
    tft.setTextColor(ST7735_YELLOW,ST7735_BLACK); // set color for V1
    tft.setTextSize(2);
    tft.setCursor(15, 40);
    tft.print("V1 ");
    tft.print(AVvolts[0] * Dv1, 1);
    tft.print("V ");
    // voltage 2 - V2(pin A1)
    tft.setTextColor(ST7735_GREEN,ST7735_BLACK);// set color for V2
    tft.setCursor(15, 60);
    tft.print("V2 ");
    tft.print(AVvolts[1] * Dv2, 1);
    tft.print("V ");
    // voltge 3 - V3(pin A2)
    tft.setTextColor(ST7735_CYAN,ST7735_BLACK);// set color for V3
    tft.setCursor(15, 80);
    tft.print("V3 ");
    tft.print(AVvolts[2] * Dv3, 1);
    tft.print("V ");
    // voltage 4 - V4(pin A3)
    tft.setTextColor(ST7735_WHITE,ST7735_BLACK);// set color for V4
    tft.setCursor(15, 100);
    tft.print("V4 ");
    tft.print(AVvolts[3] * Dv4, 2);
    tft.print("V ");
    tft.drawRoundRect(2, 20, 120, 110, 5,ST7735_WHITE);
    // reset count and sums
    Scount = 0;
    for (cnt1 = 0; cnt1 < 4; cnt1++) {
        sum[cnt1] = 0;
    }
}

7)clicca in alto sulla freccia a destra ("carica")
8)se tutto è andato a buon fine non ottieni nessun errore ma solo "caricamento "completato"
9) fatto

Ho preso il codice quello completo con le 4 tensioni lette e aggiunto il calcolo dei valori massimi/minimi per ciascun canale.
L'unica cosa è che non li visualizzi perchè non c'è abbastanza spazio sul display, a meno che tu non mi dica come fare (esempio: visualizzo ogni singolo canale ciclicamente con sotto il max/min relativo, oppure i 4 canali contemporaneamente ma con i valori max/min scrittti a fianco più piccoli ecc ecc)

P.s. non ho il display in questione per cui sto facendo tutto al buio :lol:

Citazione:

Originalmente inviato da dex1 (Messaggio 5041231)
:approved:

grazie,
appena mi arrivano i display mi metto a sperimentare.

fatto,e ho più o meno capito come fare,pero preferisco lo sketc a due tensioni.
ho gia capito come cambiare i colori delle varie scritte,ma non come mettere i valori medi.
un grazie e poco.
ciao

aero330 23 maggio 17 19:45

Citazione:

Originalmente inviato da dex1 (Messaggio 5041474)
fatto,e ho più o meno capito come fare,pero preferisco lo sketc a due tensioni.
ho gia capito come cambiare i colori delle varie scritte,ma non come mettere i valori medi.
un grazie e poco.
ciao

Tenendo presente tutto quello che fin'ora è stato detto (dovrei aver messo tutto):
Codice:

#define sclk 13
#define mosi 11
#define cs  10
#define dc  9
#define rst  8  //  reset

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>

Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

// number of analog samples to take per reading, per channel
#define NSAMP 128 // number of samples to take befor displaying (128=2^7)

// voltage divider calibration values
#define Dv1    11.00
#define Dv2    11.00

//number of channels
#define N_CH  2

// ADC reference voltage / calibration value
#define VREF    5.00

unsigned int u16_sum[2] = {0, 0};                // u16_sums of samples taken
unsigned char u8_Scount = 0;  // current sample number
float f32_AVvolts[2] = {0.0, 0.0};        // calculated voltages
char u8_cnt1 = 0;                  // used in 'for' loops

float f32_V_max[2] = {0.00 ,0.00};
float f32_V_min[2] = {100.00, 100.00};

void setup()
{
  tft.initR(INITR_BLACKTAB);  // initialize a ST7735S chip, black tab
  tft.fillScreen(ST7735_BLACK); //  clear screen
  tft.setTextColor(ST7735_WHITE);
  tft.drawRoundRect(2, 10, 120, 110, 5,ST7735_WHITE);
  tft.setTextSize(1);
  tft.setCursor(5,0);
  tft.println("2 channel voltmeter");
  tft.setTextColor(0XFF00);
  tft.setCursor(0,140);
  tft.println(" Caution max voltage        55vdc");
}

       
void loop()
{

    ADC_Reading();          //Read voltages
    MaxMin_Detection();    //Detect MAX/MIN and mean
   
    Print_V1();    //Display V1 and V2 voltages
    Print_V2();
   
    tft.drawRoundRect(2, 10, 120, 120, 5,ST7735_WHITE);  // draw a rectangle
   
    Reset_var();    //Reset variables
}

void ADC_Reading(){
 
    while (u8_Scount < NSAMP) {
        // sample each channel A0 to A1
        for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
            u16_sum[u8_cnt1] += analogRead(A0 + u8_cnt1);
        }
        u8_Scount++;
        delay(10);
    }
}

void MaxMin_Detection(){
 
    // calculate the voltage for each channel
    for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
        f32_AVvolts[u8_cnt1] = ((u16_sum[u8_cnt1]>>7) * VREF) / 1024.0;
        if(f32_AVvolts[u8_cnt1] > f32_V_max[u8_cnt1]){ //get MAX value of sample
          f32_V_max[u8_cnt1] = f32_AVvolts[u8_cnt1];
        }
        if(f32_AVvolts[u8_cnt1] < f32_V_min[u8_cnt1]){ //get MIN value of sample
          f32_V_min[u8_cnt1] = f32_AVvolts[u8_cnt1];
        }       
    }
}

void Print_V1(){
      // voltage 1 - V1(pin A0) 
    tft.setTextColor(ST7735_YELLOW,ST7735_BLACK); // set color for V1
    tft.setTextSize(3);
    tft.setCursor(15, 20);
    tft.print("V1 ");
    tft.print(f32_AVvolts[0] * Dv1, 1);
    tft.print("V ");
    // voltage 1 - mean of V1
    tft.setTextColor(ST7735_GREEN,ST7735_BLACK);// set color for V1
    tft.setTextSize(2);
    tft.setCursor(15, 46);
    tft.print("Vmax ");
    tft.print(f32_V_max[1] * Dv1, 1);
    tft.print("V ");
    tft.setCursor(15, 63);
    tft.print("Vmin ");
    tft.print(f32_V_min[1] * Dv1, 1);
    tft.print("V ");
}

void Print_V2(){
    // voltge 2 - V2 (pin A1)
    tft.setTextColor(ST7735_CYAN,ST7735_BLACK);// set color for V3
    tft.setTextSize(3);
    tft.setCursor(15, 80);
    tft.print("V2 ");
    tft.print(f32_AVvolts[2] * Dv2, 1);
    tft.print("V ");
    // voltage 2 - mean of V2
    tft.setTextColor(ST7735_WHITE,ST7735_BLACK);// set color for V4
    tft.setTextSize(2);
    tft.setCursor(15, 106);
    tft.print("Vmax ");
    tft.print(f32_V_max[2] * Dv2, 1);
    tft.print("V ");
    tft.setCursor(15, 123);
    tft.print("Vmin ");
    tft.print(f32_V_min[2] * Dv2, 1);
    tft.print("V ");
}

void Reset_var(){
      // reset variables
    u8_Scount = 0;
    for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
        u16_sum[u8_cnt1] = 0;
    }
}

Sul display dovresti vedere una cosa del genere:

2 channels voltmeter
/--------------------------------------------\
V1 0.00 V
Vmax 0.00V
Vmin 0.00V

V2 0.00 V
Vmax 0.00V
Vmin 0.00V
\--------------------------------------------/
Caution max voltage
55vdc

Se le scritte sono decentrate o non allineate correggi tu in base a quello che vedi su display agendo su tft.setTextSize(), tft.setCursor() e tft.drawRoundRect()

dex1 24 maggio 17 01:38

Citazione:

Originalmente inviato da aero330 (Messaggio 5041507)
Tenendo presente tutto quello che fin'ora è stato detto (dovrei aver messo tutto):
Codice:

#define sclk 13
#define mosi 11
#define cs  10
#define dc  9
#define rst  8  //  reset

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>

Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

// number of analog samples to take per reading, per channel
#define NSAMP 128 // number of samples to take befor displaying (128=2^7)

// voltage divider calibration values
#define Dv1    11.00
#define Dv2    11.00

//number of channels
#define N_CH  2

// ADC reference voltage / calibration value
#define VREF    5.00

unsigned int u16_sum[2] = {0, 0};                // u16_sums of samples taken
unsigned char u8_Scount = 0;  // current sample number
float f32_AVvolts[2] = {0.0, 0.0};        // calculated voltages
char u8_cnt1 = 0;                  // used in 'for' loops

float f32_V_max[2] = {0.00 ,0.00};
float f32_V_min[2] = {100.00, 100.00};

void setup()
{
  tft.initR(INITR_BLACKTAB);  // initialize a ST7735S chip, black tab
  tft.fillScreen(ST7735_BLACK); //  clear screen
  tft.setTextColor(ST7735_WHITE);
  tft.drawRoundRect(2, 10, 120, 110, 5,ST7735_WHITE);
  tft.setTextSize(1);
  tft.setCursor(5,0);
  tft.println("2 channel voltmeter");
  tft.setTextColor(0XFF00);
  tft.setCursor(0,140);
  tft.println(" Caution max voltage        55vdc");
}

       
void loop()
{

    ADC_Reading();          //Read voltages
    MaxMin_Detection();    //Detect MAX/MIN and mean
   
    Print_V1();    //Display V1 and V2 voltages
    Print_V2();
   
    tft.drawRoundRect(2, 10, 120, 120, 5,ST7735_WHITE);  // draw a rectangle
   
    Reset_var();    //Reset variables
}

void ADC_Reading(){
 
    while (u8_Scount < NSAMP) {
        // sample each channel A0 to A1
        for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
            u16_sum[u8_cnt1] += analogRead(A0 + u8_cnt1);
        }
        u8_Scount++;
        delay(10);
    }
}

void MaxMin_Detection(){
 
    // calculate the voltage for each channel
    for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
        f32_AVvolts[u8_cnt1] = ((u16_sum[u8_cnt1]>>7) * VREF) / 1024.0;
        if(f32_AVvolts[u8_cnt1] > f32_V_max[u8_cnt1]){ //get MAX value of sample
          f32_V_max[u8_cnt1] = f32_AVvolts[u8_cnt1];
        }
        if(f32_AVvolts[u8_cnt1] < f32_V_min[u8_cnt1]){ //get MIN value of sample
          f32_V_min[u8_cnt1] = f32_AVvolts[u8_cnt1];
        }       
    }
}

void Print_V1(){
      // voltage 1 - V1(pin A0) 
    tft.setTextColor(ST7735_YELLOW,ST7735_BLACK); // set color for V1
    tft.setTextSize(3);
    tft.setCursor(15, 20);
    tft.print("V1 ");
    tft.print(f32_AVvolts[0] * Dv1, 1);
    tft.print("V ");
    // voltage 1 - mean of V1
    tft.setTextColor(ST7735_GREEN,ST7735_BLACK);// set color for V1
    tft.setTextSize(2);
    tft.setCursor(15, 46);
    tft.print("Vmax ");
    tft.print(f32_V_max[1] * Dv1, 1);
    tft.print("V ");
    tft.setCursor(15, 63);
    tft.print("Vmin ");
    tft.print(f32_V_min[1] * Dv1, 1);
    tft.print("V ");
}

void Print_V2(){
    // voltge 2 - V2 (pin A1)
    tft.setTextColor(ST7735_CYAN,ST7735_BLACK);// set color for V3
    tft.setTextSize(3);
    tft.setCursor(15, 80);
    tft.print("V2 ");
    tft.print(f32_AVvolts[2] * Dv2, 1);
    tft.print("V ");
    // voltage 2 - mean of V2
    tft.setTextColor(ST7735_WHITE,ST7735_BLACK);// set color for V4
    tft.setTextSize(2);
    tft.setCursor(15, 106);
    tft.print("Vmax ");
    tft.print(f32_V_max[2] * Dv2, 1);
    tft.print("V ");
    tft.setCursor(15, 123);
    tft.print("Vmin ");
    tft.print(f32_V_min[2] * Dv2, 1);
    tft.print("V ");
}

void Reset_var(){
      // reset variables
    u8_Scount = 0;
    for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
        u16_sum[u8_cnt1] = 0;
    }
}

Sul display dovresti vedere una cosa del genere:

2 channels voltmeter
/--------------------------------------------\
V1 0.00 V
Vmax 0.00V
Vmin 0.00V

V2 0.00 V
Vmax 0.00V
Vmin 0.00V
\--------------------------------------------/
Caution max voltage
55vdc

Se le scritte sono decentrate o non allineate correggi tu in base a quello che vedi su display agendo su tft.setTextSize(), tft.setCursor() e tft.drawRoundRect()

fantastico,domani provo.
nel frattempo ho cercato qualche tutorial per capire meglio sia arduino
che come impostare questo display.
ce ne molto da studiare.
hai qualche consiglio?
sono in debito.
grazie ancora.

aero330 24 maggio 17 11:46

1 Allegato/i
Citazione:

Originalmente inviato da dex1 (Messaggio 5041577)
fantastico,domani provo.
nel frattempo ho cercato qualche tutorial per capire meglio sia arduino
che come impostare questo display.
ce ne molto da studiare.
hai qualche consiglio?
sono in debito.
grazie ancora.

Figurati!
In allegato ti ho messo un pdf che ho trovato su internet un po' di tempo fa.
Non l'ho mai letto, ma dandoci uno sguardo veloce mi pare un buon inizio per uno che è alle prime armi...ovviamente non è completo per quanto riguarda la programmazione ma rende bene l'idea di come muoversi in questo ambiente.

Per il display basta cercare in rete. Non è il "classico" LCD per cui trovi molti esempi, ma sicuramente otterrai le info necessarie se trovi la lista delle istruzioni e realative funzioni svolte.

dex1 24 maggio 17 12:41

buondi.
caricato,in sostanza funziona ma ho dovuto ridurre i caratteri,altrimenti l'indicazione del voltaggio saltava la riga.
pero il V2 non mi rileva niente,ho provato a studiare lo sketc,ma non ho capito dove sta il problema.
Codice:

#define sclk 13
#define mosi 11
#define cs  10
#define dc  9
#define rst  8  //  reset

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>

Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

// number of analog samples to take per reading, per channel
#define NSAMP 128 // number of samples to take befor displaying (128=2^7)

// voltage divider calibration values
#define Dv1    11.00
#define Dv2    11.00

//number of channels
#define N_CH  2

// ADC reference voltage / calibration value
#define VREF    5.00

unsigned int u16_sum[2] = {0, 0};                // u16_sums of samples taken
unsigned char u8_Scount = 0;  // current sample number
float f32_AVvolts[2] = {0.0, 0.0};        // calculated voltages
char u8_cnt1 = 0;                  // used in 'for' loops

float f32_V_max[2] = {0.00 ,0.00};
float f32_V_min[2] = {100.00, 100.00};

void setup()
{
  tft.initR(INITR_BLACKTAB);  // initialize a ST7735S chip, black tab
  tft.fillScreen(ST7735_BLACK); //  clear screen
  tft.setTextColor(ST7735_WHITE);
 
  tft.setTextSize(1);
  tft.setCursor(5,0);
  tft.println("2 channel voltmeter");
  tft.setTextColor(0XFF00);
  tft.setCursor(0,140);
  tft.println(" Caution max voltage        55vdc");
}

       
void loop()
{

    ADC_Reading();          //Read voltages
    MaxMin_Detection();    //Detect MAX/MIN and mean
   
    Print_V1();    //Display V1 and V2 voltages
    Print_V2();
   
   
   
    Reset_var();    //Reset variables
}

void ADC_Reading(){
 
    while (u8_Scount < NSAMP) {
        // sample each channel A0 to A1
        for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
            u16_sum[u8_cnt1] += analogRead(A0 + u8_cnt1);
        }
        u8_Scount++;
        delay(10);
    }
}

void MaxMin_Detection(){
 
    // calculate the voltage for each channel
    for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
        f32_AVvolts[u8_cnt1] = ((u16_sum[u8_cnt1]>>7) * VREF) / 1024.0;
        if(f32_AVvolts[u8_cnt1] > f32_V_max[u8_cnt1]){ //get MAX value of sample
          f32_V_max[u8_cnt1] = f32_AVvolts[u8_cnt1];
        }
        if(f32_AVvolts[u8_cnt1] < f32_V_min[u8_cnt1]){ //get MIN value of sample
          f32_V_min[u8_cnt1] = f32_AVvolts[u8_cnt1];
        }       
    }
}

void Print_V1(){
      // voltage 1 - V1(pin A0) 
    tft.setTextColor(ST7735_YELLOW,ST7735_BLACK); // set color for V1
    tft.setTextSize(2);
    tft.setCursor(15, 20);
    tft.print("V1 ");
    tft.print(f32_AVvolts[0] * Dv1, 1);
    tft.print("V ");
    // voltage 1 - mean of V1
    tft.setTextColor(ST7735_GREEN,ST7735_BLACK);// set color for V1
    tft.setTextSize(1);
    tft.setCursor(15, 46);
    tft.print("Vmax ");
    tft.print(f32_V_max[1] * Dv1, 1);
    tft.print("V ");
    tft.setCursor(15, 63);
    tft.print("Vmin ");
    tft.print(f32_V_min[1] * Dv1, 1);
    tft.print("V ");
}

void Print_V2(){
    // voltge 2 - V2 (pin A1)
    tft.setTextColor(ST7735_CYAN,ST7735_BLACK);// set color for V2
    tft.setTextSize(2);
    tft.setCursor(15, 80);
    tft.print("V2 ");
    tft.print(f32_AVvolts[2] * Dv2, 1);
    tft.print("V ");
    // voltage 2 - mean of V2
    tft.setTextColor(ST7735_WHITE,ST7735_BLACK);// set color for V2
    tft.setTextSize(1);
    tft.setCursor(15, 106);
    tft.print("Vmax ");
    tft.print(f32_V_max[2] * Dv2, 1);
    tft.print("V ");
    tft.setCursor(15, 123);
    tft.print("Vmin ");
    tft.print(f32_V_min[2] * Dv2, 1);
    tft.print("V ");
}

void Reset_var(){
      // reset variables
    u8_Scount = 0;
    for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
        u16_sum[u8_cnt1] = 0;
    }
}


ElNonino 24 maggio 17 12:51

Citazione:

Originalmente inviato da dex1 (Messaggio 5041647)
buondi.
caricato,in sostanza funziona ma ho dovuto ridurre i caratteri,altrimenti l'indicazione del voltaggio saltava la riga.
pero il V2 non mi rileva niente,ho provato a studiare lo sketc,ma non ho capito dove sta il problema.
void Print_V2(){
// voltge 2 - V2 (pin A1)
tft.setTextColor(ST7735_CYAN,ST7735_BLACK);// set color for V2
tft.setTextSize(2);
tft.setCursor(15, 80);
tft.print("V2 ");
tft.print(f32_AVvolts[2] * Dv2, 1);
tft.print("V ");
// voltage 2 - mean of V2
tft.setTextColor(ST7735_WHITE,ST7735_BLACK);// set color for V2
tft.setTextSize(1);
tft.setCursor(15, 106);
tft.print("Vmax ");
tft.print(f32_V_max[2] * Dv2, 1);
tft.print("V ");
tft.setCursor(15, 123);
tft.print("Vmin ");
tft.print(f32_V_min[2] * Dv2, 1);
tft.print("V ");
}

void Reset_var(){
// reset variables
u8_Scount = 0;
for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
u16_sum[u8_cnt1] = 0;
}
}[/CODE]

sostituisci [2] con [1].

:yeah:

aero330 24 maggio 17 12:57

Citazione:

Originalmente inviato da dex1 (Messaggio 5041647)
buondi.
caricato,in sostanza funziona ma ho dovuto ridurre i caratteri,altrimenti l'indicazione del voltaggio saltava la riga.
pero il V2 non mi rileva niente,ho provato a studiare lo sketc,ma non ho capito dove sta il problema.
Codice:

#define sclk 13
#define mosi 11
#define cs  10
#define dc  9
#define rst  8  //  reset

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>

Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

// number of analog samples to take per reading, per channel
#define NSAMP 128 // number of samples to take befor displaying (128=2^7)

// voltage divider calibration values
#define Dv1    11.00
#define Dv2    11.00

//number of channels
#define N_CH  2

// ADC reference voltage / calibration value
#define VREF    5.00

unsigned int u16_sum[2] = {0, 0};                // u16_sums of samples taken
unsigned char u8_Scount = 0;  // current sample number
float f32_AVvolts[2] = {0.0, 0.0};        // calculated voltages
char u8_cnt1 = 0;                  // used in 'for' loops

float f32_V_max[2] = {0.00 ,0.00};
float f32_V_min[2] = {100.00, 100.00};

void setup()
{
  tft.initR(INITR_BLACKTAB);  // initialize a ST7735S chip, black tab
  tft.fillScreen(ST7735_BLACK); //  clear screen
  tft.setTextColor(ST7735_WHITE);
 
  tft.setTextSize(1);
  tft.setCursor(5,0);
  tft.println("2 channel voltmeter");
  tft.setTextColor(0XFF00);
  tft.setCursor(0,140);
  tft.println(" Caution max voltage        55vdc");
}

       
void loop()
{

    ADC_Reading();          //Read voltages
    MaxMin_Detection();    //Detect MAX/MIN and mean
   
    Print_V1();    //Display V1 and V2 voltages
    Print_V2();
   
   
   
    Reset_var();    //Reset variables
}

void ADC_Reading(){
 
    while (u8_Scount < NSAMP) {
        // sample each channel A0 to A1
        for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
            u16_sum[u8_cnt1] += analogRead(A0 + u8_cnt1);
        }
        u8_Scount++;
        delay(10);
    }
}

void MaxMin_Detection(){
 
    // calculate the voltage for each channel
    for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
        f32_AVvolts[u8_cnt1] = ((u16_sum[u8_cnt1]>>7) * VREF) / 1024.0;
        if(f32_AVvolts[u8_cnt1] > f32_V_max[u8_cnt1]){ //get MAX value of sample
          f32_V_max[u8_cnt1] = f32_AVvolts[u8_cnt1];
        }
        if(f32_AVvolts[u8_cnt1] < f32_V_min[u8_cnt1]){ //get MIN value of sample
          f32_V_min[u8_cnt1] = f32_AVvolts[u8_cnt1];
        }       
    }
}

void Print_V1(){
      // voltage 1 - V1(pin A0) 
    tft.setTextColor(ST7735_YELLOW,ST7735_BLACK); // set color for V1
    tft.setTextSize(2);
    tft.setCursor(15, 20);
    tft.print("V1 ");
    tft.print(f32_AVvolts[0] * Dv1, 1);
    tft.print("V ");
    // voltage 1 - mean of V1
    tft.setTextColor(ST7735_GREEN,ST7735_BLACK);// set color for V1
    tft.setTextSize(1);
    tft.setCursor(15, 46);
    tft.print("Vmax ");
    tft.print(f32_V_max[1] * Dv1, 1);
    tft.print("V ");
    tft.setCursor(15, 63);
    tft.print("Vmin ");
    tft.print(f32_V_min[1] * Dv1, 1);
    tft.print("V ");
}

void Print_V2(){
    // voltge 2 - V2 (pin A1)
    tft.setTextColor(ST7735_CYAN,ST7735_BLACK);// set color for V2
    tft.setTextSize(2);
    tft.setCursor(15, 80);
    tft.print("V2 ");
    tft.print(f32_AVvolts[2] * Dv2, 1);
    tft.print("V ");
    // voltage 2 - mean of V2
    tft.setTextColor(ST7735_WHITE,ST7735_BLACK);// set color for V2
    tft.setTextSize(1);
    tft.setCursor(15, 106);
    tft.print("Vmax ");
    tft.print(f32_V_max[2] * Dv2, 1);
    tft.print("V ");
    tft.setCursor(15, 123);
    tft.print("Vmin ");
    tft.print(f32_V_min[2] * Dv2, 1);
    tft.print("V ");
}

void Reset_var(){
      // reset variables
    u8_Scount = 0;
    for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
        u16_sum[u8_cnt1] = 0;
    }
}


Pardon...colpa mia.
L'errore è dovuto ad una lettura errata negli array dove sono salvati i valori delle tensioni (sia quelli mediati che quelli di max/min).

Nella funzione Print_V2, vado a leggere e stampare i valori nella 3° posizione dei rispettivi array quando in realtà questi sono lunghi 2 "celle":
tft.print(f32_AVvolts[2] * Dv2, 1); -> tft.print(f32_AVvolts[1] * Dv2, 1);
tft.print(f32_V_max[2] * Dv2, 1); -> tft.print(f32_V_max[1] * Dv2, 1);
tft.print(f32_V_min[2] * Dv2, 1); -> tft.print(f32_V_min[1] * Dv2, 1);
Anche in Print_V1 c'era una lettura errata, gli indici degli array vanno tutti a "[0]".

Gli array/puntatori sono bestiacce :lol:

Ecco il codice corretto:
Codice:

#define sclk 13
#define mosi 11
#define cs  10
#define dc  9
#define rst  8  //  reset

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>

Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

// number of analog samples to take per reading, per channel
#define NSAMP 128 // number of samples to take befor displaying (128=2^7)

// voltage divider calibration values
#define Dv1    11.00
#define Dv2    11.00

//number of channels
#define N_CH  2

// ADC reference voltage / calibration value
#define VREF    5.00

unsigned int u16_sum[2] = {0, 0};                // u16_sums of samples taken
unsigned char u8_Scount = 0;  // current sample number
float f32_AVvolts[2] = {0.0, 0.0};        // calculated voltages
char u8_cnt1 = 0;                  // used in 'for' loops

float f32_V_max[2] = {0.00 ,0.00};
float f32_V_min[2] = {100.00, 100.00};

void setup()
{
  tft.initR(INITR_BLACKTAB);  // initialize a ST7735S chip, black tab
  tft.fillScreen(ST7735_BLACK); //  clear screen
  tft.setTextColor(ST7735_WHITE);
 
  tft.setTextSize(1);
  tft.setCursor(5,0);
  tft.println("2 channels voltmeter");
  tft.setTextColor(0XFF00);
  tft.setCursor(0,140);
  tft.println(" Caution max voltage        55vdc");
}

       
void loop()
{

    ADC_Reading();          //Read voltages
    MaxMin_Detection();    //Detect MAX/MIN and mean
   
    Print_V1();    //Display V1 and V2 voltages
    Print_V2();
   
    tft.drawRoundRect(2, 10, 120, 120, 5,ST7735_WHITE);  // draw a rectangle
   
    Reset_var();    //Reset variables
}

void ADC_Reading(){
 
    while (u8_Scount < NSAMP) {
        // sample each channel A0 to A1
        for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
            u16_sum[u8_cnt1] += analogRead(A0 + u8_cnt1);
        }
        u8_Scount++;
        delay(10);
    }
}

void MaxMin_Detection(){
 
    // calculate the voltage for each channel
    for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
       
        f32_AVvolts[u8_cnt1] = ((u16_sum[u8_cnt1]>>7) * VREF) / 1024.0;
       
        if(f32_AVvolts[u8_cnt1] > f32_V_max[u8_cnt1]){ //get MAX value of sample
          f32_V_max[u8_cnt1] = f32_AVvolts[u8_cnt1];
        }
        if(f32_AVvolts[u8_cnt1] < f32_V_min[u8_cnt1]){ //get MIN value of sample
          f32_V_min[u8_cnt1] = f32_AVvolts[u8_cnt1];
        }       
    }
}

void Print_V1(){
      // voltage 1 - V1(pin A0) 
    tft.setTextColor(ST7735_YELLOW,ST7735_BLACK); // set color for V1
    tft.setTextSize(2);
    tft.setCursor(15, 20);
    tft.print("V1 ");
    tft.print(f32_AVvolts[0] * Dv1, 1);
    tft.print("V ");
    // voltage 1 - mean of V1
    tft.setTextColor(ST7735_GREEN,ST7735_BLACK);// set color for V1
    tft.setTextSize(2);
    tft.setCursor(15, 46);
    tft.print("Vmax ");
    tft.print(f32_V_max[0] * Dv1, 1);
    tft.print("V ");
    tft.setCursor(15, 63);
    tft.print("Vmin ");
    tft.print(f32_V_min[0] * Dv1, 1);
    tft.print("V ");
}

void Print_V2(){
    // voltge 2 - V2 (pin A1)
    tft.setTextColor(ST7735_CYAN,ST7735_BLACK);// set color for V3
    tft.setTextSize(2);
    tft.setCursor(15, 80);
    tft.print("V2 ");
    tft.print(f32_AVvolts[1] * Dv2, 1);
    tft.print("V ");
    // voltage 2 - mean of V2
    tft.setTextColor(ST7735_WHITE,ST7735_BLACK);// set color for V4
    tft.setTextSize(2);
    tft.setCursor(15, 106);
    tft.print("Vmax ");
    tft.print(f32_V_max[1] * Dv2, 1);
    tft.print("V ");
    tft.setCursor(15, 123);
    tft.print("Vmin ");
    tft.print(f32_V_min[1] * Dv2, 1);
    tft.print("V ");
}

void Reset_var(){
      // reset variables
    u8_Scount = 0;
    for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
        u16_sum[u8_cnt1] = 0;
    }
}

EDIT:battuto sul tempo da ELNonino :lol::lol::lol:


Tutti gli orari sono GMT +2. Adesso sono le 18:12.

Basato su: vBulletin versione 3.8.11
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
E' vietata la riproduzione, anche solo in parte, di contenuti e grafica. Copyright 1998/2019 - K-Bits P.I. 09395831002