Hallo Forum,
mein Name ist Martin,ich bin lernwilliger Arduinoanfänger und stehe vor einem Problem das ich alleine nicht gelöst bekomme.
Ich habe 2 Sketche die unabhängig voneinander funktionieren. Füge ich jedoch Sketch 1 und 2 zusammen zu einem Programm funktioniert nur noch Sketch 1.
Konkret geht es darum verschiedene Spannungen und Ströme zu messen und auf LCD Displays anzuzeigen (Sketch 1) und gleichzeitig auch noch einen PWM Leistungssteller durch einen Drehencoder anzusteuern mit 0-5V (Sketch 2).
Ich habe die Sketches unabhängig voneinander geschrieben bzw. auf Beispiele zurückgegriffen und erfolgreich getestet. Falsche Verdrahtung kann ich ausschließen, es muss an der Software liegen.
Auch einen Fehler durch Copy & Paste kann ich ausschließen da ich den Code von Sketch 2 probehalber auch noch von Hand geschrieben habe.
Vielleicht kann von euch mal jemand über den Sketch drüberschauen und mir auf die Sprünge helfen.
>
#include <RotaryEncoder.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd1(0x23, 16, 2); // 3,3V
LiquidCrystal_I2C lcd2(0x24, 16, 2); // 5V
LiquidCrystal_I2C lcd3(0x25, 16, 2); // 12V
LiquidCrystal_I2C lcd4(0x26, 16, 2); // 24V
LiquidCrystal_I2C lcd5(0x27, 16, 2); // PWM
#define ROTARYSTEPS 5
#define ROTARYMIN 0
#define ROTARYMAX 100
RotaryEncoder encoder(A11, A12); // Setup a RotaryEncoder for pins A11 and A12:
int lastPos = -1; // Last known rotary position.
int PWM_Pin = 3;
int PWM = 0;
const int analogIn_Vcc = A0;
const int analogIn_U3V3 = A1;
const int analogIn_I3V3 = A2;
const int analogIn_U5V = A3;
const int analogIn_I5V = A4;
const int analogIn_U12V = A5;
const int analogIn_I12V = A6;
const int analogIn_U24V = A7;
const int analogIn_I24V = A8;
const float U_ref = 1.1; // in V
const float R0 = 52.5; // 100 kOhm Potis zum Abgleich für alle Spannungen
const float R1 = 219.7; // in kOhm für 3V3 + 5V + Vcc
const float R2 = 560.0; // in kOhm für 12V
const float R3 = 1100.0; // in kOhm für 24v
const int averages = 25; // Mittelwert aus 25 Messungen
float ACSoffset; // Offsetspannung von ACS712 Stromsensor = Vcc/2
float voltageVcc;
float voltageU3V3;
float voltageI3V3;
float voltageU5V;
float voltageI5V;
float voltageU12V;
float voltageI12V;
float voltageU24V;
float voltageI24V;
int ADC_countVcc;
int ADC_countU3V3;
int ADC_countI3V3;
int ADC_countU5V;
int ADC_countI5V;
int ADC_countU12V;
int ADC_countI12V;
int ADC_countU24V;
int ADC_countI24V;
float AmpsI3V3;
float AmpsI5V;
float AmpsI12V;
float AmpsI24V;
float Watt3V3;
float Watt5V;
float Watt12V;
float Watt24V;
void setup() {
encoder.setPosition(0 / ROTARYSTEPS); // start with the value of 0.
Serial.begin(9600);
analogReference(INTERNAL1V1);
lcd1.begin();
lcd1.backlight();
lcd2.begin();
lcd2.backlight();
lcd3.begin();
lcd3.backlight();
lcd4.begin();
lcd4.backlight();
lcd5.begin();
lcd5.backlight();
}
void messung_Vcc() {
ADC_countVcc = 0;
for (int i = 0; i < averages; i++)
ADC_countVcc += analogRead(analogIn_Vcc);
voltageVcc = (R1 + R0) / R0 * U_ref * ADC_countVcc / 1023 / averages;
}
void messung_5V() {
ADC_countU5V = 0;
for (int i = 0; i < averages; i++)
ADC_countU5V += analogRead(analogIn_U5V);
voltageU5V = (R1 + R0) / R0 * U_ref * ADC_countU5V / 1023 / averages;
ADC_countI5V = 0;
for (int i = 0; i < averages; i++)
ADC_countI5V += analogRead(analogIn_I5V);
voltageI5V = (R1 + R0) / R0 * U_ref * ADC_countI5V / 1023 / averages;
ACSoffset = voltageVcc * 0.5;
AmpsI5V = (voltageI5V - ACSoffset) / 0.066;
Watt5V = voltageU5V * AmpsI5V;
}
void messung_3V3() {
ADC_countU3V3 = 0;
for (int i = 0; i < averages; i++)
ADC_countU3V3 += analogRead(analogIn_U3V3);
voltageU3V3 = (R1 + R0) / R0 * U_ref * ADC_countU3V3 / 1023 / averages;
ADC_countI3V3 = 0;
for (int i = 0; i < averages; i++)
ADC_countI3V3 += analogRead(analogIn_I3V3);
voltageI3V3 = (R1 + R0) / R0 * U_ref * ADC_countI3V3 / 1023 / averages;
ACSoffset = voltageVcc * 0.5;
AmpsI3V3 = (voltageI3V3 - ACSoffset) / 0.066;
Watt3V3 = voltageU3V3 * AmpsI3V3;
}
void messung_12V() {
ADC_countU12V = 0;
for (int i = 0; i < averages; i++)
ADC_countU12V += analogRead(analogIn_U12V);
voltageU12V = (R2 + R0) / R0 * U_ref * ADC_countU12V / 1023 / averages;
ADC_countI12V = 0;
for (int i = 0; i < averages; i++)
ADC_countI12V += analogRead(analogIn_I12V);
voltageI12V = (R1 + R0) / R0 * U_ref * ADC_countI12V / 1023 / averages;
ACSoffset = voltageVcc * 0.5;
AmpsI12V = (voltageI12V - ACSoffset) / 0.066;
Watt12V = voltageU12V * AmpsI12V;
}
void messung_24V() {
ADC_countU24V = 0;
for (int i = 0; i < averages; i++)
ADC_countU24V += analogRead(analogIn_U24V);
voltageU24V = (R3 + R0) / R0 * U_ref * ADC_countU12V / 1023 / averages;
ADC_countI24V = 0;
for (int i = 0; i < averages; i++)
ADC_countI24V += analogRead(analogIn_I24V);
voltageI24V = (R1 + R0) / R0 * U_ref * ADC_countI24V / 1023 / averages;
ACSoffset = voltageVcc * 0.5;
AmpsI24V = (voltageI24V - ACSoffset) / 0.066;
Watt24V = voltageU24V * AmpsI24V;
}
void loop() {
encoder.tick();
// get the current physical position and calc the logical position
int newPos = encoder.getPosition() * ROTARYSTEPS;
analogWrite(PWM_Pin, PWM);
if (newPos < ROTARYMIN) {
encoder.setPosition(ROTARYMIN / ROTARYSTEPS);
newPos = ROTARYMIN;
}
else if (newPos > ROTARYMAX) {
encoder.setPosition(ROTARYMAX / ROTARYSTEPS);
newPos = ROTARYMAX;
}
if (lastPos != newPos) {
Serial.print(newPos);
Serial.println();
lastPos = newPos;
}
PWM = map(newPos, 0, 100, 0, 255);
lcd5.setCursor(0, 0);
lcd5.print(newPos);
lcd5.println(" Prozent");
messung_5V();
messung_Vcc();
messung_3V3();
messung_12V();
messung_24V();
lcd1.setCursor(0, 0);
lcd1.print("U=");
lcd1.print(voltageU5V, 1);
lcd1.print(" V");
lcd1.setCursor(0, 1);
lcd1.print("I=");
lcd1.print(AmpsI5V, 2);
lcd1.print(" A");
lcd1.setCursor(9, 1);
lcd1.print("P=");
lcd1.print(Watt5V, 1);
lcd1.print(" W");
delay(1000);
}
<