Arduino-Uno per un Alimentatore variabile 2 Oled e controlli vari


//-------------------------Alimentatore PWM---------------------------\\

#include <Arduino.h>
#include <AVR/io.h>
#include <U8g2lib.h>
#include <Wire.h>

U8G2_SSD1309_128X64_NONAME0_1_HW_I2C oled1(U8G2_R0, U8X8_PIN_NONE);
U8G2_SSD1309_128X64_NONAME0_1_HW_I2C oled2(U8G2_R0, U8X8_PIN_NONE);

  int  Volts = A0;//input volt
  int Amps = A1;//input amp
  
  int LM35_pin = A2;//temperatura
  float tempC = 0;
  float temp = 0;
  float temp1 = 0;
  float milliVolt = 0;

  int potPin = A3;//input potenziometro A3
  float potPinRead = 0; //da 0 a 1023
  float dutyCycle = 0; //da 0 a 1 (1 è 100%)
  
  float VoltsADC = 0.0;
  int Volt = 0;
  float R1 = 27000.64; // resistance of R1
  float R2 = 4780.0; // resistance of R2
  float V = 5.12; // Arduino supply
  float vIn = 0.0;
  float vout = 0.0;

  float AmpsADC = 0.0;
  float Amps1 = 0.0;
  int amp = 0;
  float amper = 0.0;

float Watt = 0.0;

uint8_t secondario=0 ;
uint8_t ventola ;

 void setup() {
  
   Wire.begin();  

  oled1.begin();//  oled1.setI2CAddress(0x3C * 2)
  oled2.begin();//  oled2.setI2CAddress(0x3D * 2) 
  
  cli();//clear interrupts flag   
   DDRC &= ~(1 << PC0);//Pin A0 Porta C input Volt
   DDRC &= ~(1 << PC1);//Pin A1 Porta C input Amper 
     
   DDRC &= ~(1 << PC2);//Pin A2 Porta C input temperatura
   DDRC &= ~(1 << PC3);//Pin A3 Porta C input potenziometro PWM 
         
   DDRD |= (1 << PD2);// Pin 2 Porta D output //ventola on/off   
   DDRD |= (1 << PD7);// Pin 7 Porta D output //rele scambio avvolgimenti0/12
  
   DDRB = _BV(PB1);// Pin 9 Porta B output PWM           
  TCCR1A = _BV(COM1A1) | _BV(WGM11);//non-inverting PWM 
  TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // prescaler 1       
  ICR1 = 800;      //800=20Khz, 350=45.83Khz, 650=24.59Khz, 600=26.66Khz
  sei();//set interrupts flag  
  OCR1A = (int) (ICR1 * 0.99999);        
 }

float fmap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void loop() {
  
//Regolazione PWM

potPinRead = analogRead(potPin);
dutyCycle = potPinRead / 1023.0;
OCR1A = (int) (ICR1 * dutyCycle);// regola Duty

    //Leggi i volt

Volt=analogRead(PC0);
  for (int i = 0; i < 15; i++) {
    Volts = Volts + analogRead(PC0);
  }
  VoltsADC = Volts / 15;  //
  vout = (VoltsADC * V) / 1023;
  vIn = vout / (R2 / (R1 + R2));
   
  // Lettura Amper 
    
   amp = analogRead(PC1);
   for (int e =0; e < 15; e++){
    AmpsADC = AmpsADC + analogRead(PC1);
   }
  Amps1 = AmpsADC / 15 ;
  amper = fabs(fmap(Amps1, 508, 703.0, 0.0, 5.0))*2;
  if (amper < 0) amper = 0.0;
     
  // Gestione secondari

    if (vIn <= 14){
      PORTD &= ~(1<<PD7);      //Pin 7(PD7) LOW rele scambio avvolgimenti 0/12 +led verde 
   String secondario = "0/12 V";      
    }

    if (vIn > 14){
      PORTD |= (1<<PD7); //Pin 7(PD7) HIGH rele +led rosso      
    String secondario = "12/24 V";
  }

    //Calcolo Watt
  
  Watt = (amper * vIn);

  // temperatura sensore LM35

  temp = analogRead(LM35_pin);
  for (int e = 0; e < 100; e++) {
    temp1 = temp1 + analogRead(LM35_pin);
  }
  temp1 = temp1 / 100;
  milliVolt = (( temp1 * (V / 1023)) / 10) * 1000;
  tempC = milliVolt ;
  
  if (tempC > 50) {
      PORTD |= (1<<PD2); //Pin 2(PD2) HIGH Ventola on +led rosso         
    ventola = "On "; 
  }
  else {
          PORTD &= ~(1<<PD2);//Pin 2(PD2) LOW ventola off + led verde
    ventola = "Off";
  }
        
  // print OLED 1 e 2

  oled1.firstPage();
  do {
    oled1.setFontMode(0);
    oled1.setFont(u8g2_font_TimesNewPixel_tr);
    oled1.drawStr(5, 20, "Volt -");
    oled1.drawStr(3, 47, "Amp.-");
    oled1.drawRFrame(0, 0, 127, 63, 3);
    oled1.setFont(u8g2_font_osb21_tn);
    oled1.drawStr(50, 28, String(vIn).c_str());
    oled1.drawStr(50, 56, String(amper).c_str());
  } while (oled1.nextPage());
      
  oled2.firstPage();
  do {
    oled2.drawRFrame(0, 0, 127, 63, 3);
    oled2.setFont(u8g2_font_9x15_t_symbols);
    oled2.drawGlyph(100, 13, 8451); //Simbolo °C
    oled2.setFont(u8g2_font_ncenB08_tr);
    oled2.drawStr(65, 12, String(tempC).c_str());    
    oled2.drawStr(65, 23, (secondario));
    oled2.drawStr(65, 34, (ventola));
    oled2.drawStr(65, 45, String(Watt).c_str());
    oled2.setFont(u8g2_font_ncenR08_tr);
    oled2.drawStr(3, 12,"Tr. Finale");
    oled2.drawStr(3, 23, "Secondario");
    oled2.drawStr(3, 34, "Ventola");
    oled2.drawStr(3, 45, "Watt");
  } while (oled2.nextPage());
  delay(10);
}digita o incolla il codice qui