Hallo zusammen,
ich bräuchte eure Hilfe bei meinem Projekt. Ich möchte mittels den HX711 A/D-Wandlern vier Wägezellen auslesen, diverse Berechnungen machen und dann mittels der Servo.h ein Ansteuerbefehl für einen Motor mittels eines PWM-Signals ausgeben.
Der grundsätzliche Code hatte schon funktioniert war aber mega unübersichtlich und auch unsicher, da jede Menge globale Variablen enthalten waren.
Nun wollte ich bestimmte Funktionen jeweils in eigene Module verpacken. Jedoch habe ich das Problem, das wenn ich in meiner sensors.cpp mit HX711 SENS_1 einen Sensor definiere, kann ich in meiner validation.cpp nicht mit SENS_1.read() den Sensorwert auslesen, da er sagt 'SENS_1' was not declared in this scope
Den selben Fehler bekomme ich auch, wenn ich die Definition von Servo PWM_out in mehreren Modulen benutzen will
Hier mal die abgespeckte Version von meinem Code. Ich verstehe nicht ganz warum das nicht funktioniert, selbst wenn ich die Sensoren noch vor der void setup() definiere.
Wäre echt top, wenn mir jemand weiterhelfen könnte =)
MAIN
#include <Arduino.h>
#include <HX711.h> //library for HX711 A/D Converter
#include <SoftwareSerial.h> //library for Bluetoothmodule
#include <Servo.h> //libary for PWM output
//#include "led.h"
//#include "array.h"
//#include "pwm.h"
#include "validation.h"
#include "sensors.h"
void setup ()
{
Serial.begin(57600); // Setup serial communication
Serial.println("Programm running");
SoftwareSerial BT(0, 1); // D0 RX and D1 TX
BT.begin(57600); // set Baudrate for BT-Module
//tare();
Serial.println(" Ready: Starting Push ");
}
void loop()
{
}
sensors.cpp
#include <HX711.h>
// Definition of IO PINs
const int SCK_S1 = 3;
const int DT_S1 = 4;
const int SCK_S2 = 5;
const int DT_S2 = 6;
const int Gain = 64; // 128,64 for Channel A
//Declaring Sensor 1 and 2
HX711 SENS_1;
HX711 SENS_2;
void sensorsetup()
{
Serial.println("Setup Sensors");
// Setup Sensors 1 and 2
SENS_1.begin(DT_S1,SCK_S1,Gain);
SENS_2.begin(DT_S2,SCK_S2,Gain);
Serial.println(" Waiting for Sensors ");
while(!SENS_1.is_ready())
{
Serial.println("Waiting for Sensor 1... ");
delay(100);
}
while(!SENS_2.is_ready())
{
Serial.println("Waiting for Sensor 2... ");
delay(100);
}
}
sensors.h
#ifndef sensors_h
#define sensors_h
void sensorsetup();
#endif // sensors_h
validation.cpp
#include <Arduino.h>
//#include "pwm.h"
#include "sensors.h"
//#include "array.h"
int samples = 25;
long Sens1_Ofs, Sens2_OfS;
bool validSignal(long r) // checks if sensor signal is valid
{
const int long maxVal = 8388600; //set maximum value for sensor reading
return ( (r < maxVal) && (r > -maxVal) && (r != 0) );
}
long mean_value(long *vec) // calculates the mean value of Sig1 /Sig2
{
int i;
double avg = 0;
long meanval = 0;
for(i=0;i<(samples-1);i++)
avg += (double) vec[i];
avg /= (double) samples;
meanval = (long) avg;
return meanval;
}
void tare() // create array with store_readings and calculate Offset with mean_value
{
int i=0;
long r1,r2; //readings of Sensor 1 and 2
while(i<samples)
{
if(SENS_1.is_ready()&&SENS_2.is_ready())
{
r1 = SENS_1.read();
r2 = SENS_2.read();
if( validSignal(r1) && validSignal(r2))
{
//store_readings(r1,r2);
i++;
}
else
Serial.println("ERROR");
delay(80);
}
}
}
validation.h
#ifndef validation_h
#define validation_h
bool validSignal(long r); // checks if sensor signal is valid
long mean_value(long *vec); // calculates the mean value of Sig1 /Sig2
void tare(); // create array with add_value and calculate Offset with mean_value
long SRC(long ); // does a Signal Range Check of the sensor readings
int failsafe(); // checks which sensor is still working. if the value exceeds a threshold a permanent low value is written as PWM-Signal. if not, PWM is zero
#endif //validation_h