Guten Morgen zusammen, guten Morgen Godot,
dann hatte ich das mit dem Herzschlag richtig verstanden.
Ich habe nochmal über die Funktion Windgeschwindigkeit () nachgedacht.
Momentan wäre es in dem Sketch ja so, dass wenn die Sonne scheint das Solarpanel ausgefahren ist. Sollte es jetzt windig werden und die Windgeschwindigkeit überschritten werden, würde das Solarpanel einfahren wollen. Das würde jetzt ja zu einem undefinierten Zustand führen. Müsste man da bei der Funktion Windgeschwindigkeit() ein break setzten, damit das Panel nicht wieder ausfährt?
Hier der aktuelle Sketch
#define rxPin =2
#define txPin =3
#define Motor1 4 // Motor 1: pin 4 goes to DIR // Motor 1: pin 5 goes to STEP
int linearPin = A5;
int linearPos = 0;
const byte motorPin = 9;
const byte reverserPin = 10;
const int northLDRPin = A4;
const int southLDRPin = A1;
int WestLDRPin = A3; // West photoresistor sensor pin
int EastLDRPin = A2; // East photoresistor sensor pin
int WestLDRValue; // West photoresistor circuit Vout reading
int EastLDRValue; // East photoresistor circuit Vout reading
int solarPin = A0;
int solarValue;
float angLR = 0.0; // Initial E-W tracking angle
int angMin = -110; // Minimum angle for the motors to approach
int angleMax = 110; // Maximum angle for the motors to approach
float stepsize = 1.8; // Degrees rotated through per step
const float thresholdLR = 0.03; //volts-Natural Difference between L and R sensors in full sun
int enablePin = 6;
int northLDR[10]; // 10 sample readings from sensor on the north side
int southLDR[10]; // 10 sample readings from sensor on the south side
const int sample_time_interval = 500; // Change this value to set the interval between each sample is taken (1000 ms)
const long solar_panel_adjustment_interval = 1000; // Change this value to set the interval between each adjustment from the solar panel (6000ms)
float wind = 0.0;
const unsigned long MESSINTERVALL = 1000; // in ms
unsigned long jetzt = 0, vorhin = 0, herz = 0;
volatile unsigned int zaehler = 0;
const byte anemoPin=3;
void countWind();
void windGeschwindigkeit();
void setup()
{
attachInterrupt(digitalPinToInterrupt(anemoPin), countWind, RISING);
pinMode(motorPin, OUTPUT);
pinMode(reverserPin, OUTPUT);
digitalWrite(motorPin, LOW);
digitalWrite(reverserPin, LOW);
pinMode(northLDRPin, INPUT);
pinMode(southLDRPin, INPUT);
pinMode(linearPin, INPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
Serial.begin(9600);
delay(5000);
}
void loop() {
linearPos = analogRead(linearPin);
int solar_input_north = 0; // Sun light intensity readings from sensor north
int solar_input_south = 0; // Sun light intensity readings from sensor south
for ( int i = 0; i < 10; i++)
{
northLDR[i] = analogRead(northLDRPin); // Taking the analog readings from sensor west
southLDR[i] = analogRead(southLDRPin);
// Taking the analog readings from sensor east
solar_input_north = northLDR[i] + solar_input_north; // Sum all the inputs from sensor north
solar_input_south = southLDR[i] + solar_input_south; // Sum all the inputs from sensor south
delay(sample_time_interval);
}
solar_input_north = (solar_input_north) / 10; // The the average of input signals from sensor west
solar_input_south = (solar_input_south) / 10; // The the average of input signals from sensor east
if ( solar_input_north - solar_input_south > 20) // If the sunlight intensity is higher on the west side of the panel
{
digitalWrite(reverserPin, HIGH);
digitalWrite(motorPin, HIGH);
if (linearPos > 740)
{
digitalWrite(reverserPin, LOW);
digitalWrite(motorPin, LOW);
}
}
else if ( solar_input_south - solar_input_north > 20) // Geändert solar_input_south - solar_input_south > 20
{
digitalWrite(reverserPin, LOW);
digitalWrite(motorPin, HIGH);
}
else // If ther sunlight intensity is similar from both side of the panel
{
digitalWrite(reverserPin, LOW);
digitalWrite(motorPin, LOW); // Stationary (1520) signal stop the solar panel from moving
}
delay(solar_panel_adjustment_interval); // Delay before another adjustment will be made
Serial.print("North: ");
Serial.println( solar_input_north);
Serial.print("South: ");
Serial.println(solar_input_south);
Serial.print("Linear: ");
Serial.println(linearPos);
if (solar_input_south && solar_input_north > 950) //Geändert solar_input_south && solar_input_south >950
{
digitalWrite(reverserPin, LOW);
digitalWrite(motorPin, HIGH);
}
if (linearPos < 25)
{
digitalWrite(reverserPin, LOW);
digitalWrite(motorPin, LOW);
}
windGeschwindigkeit();
schrittMotor();
}
void windGeschwindigkeit()
{
jetzt = millis();
if (jetzt - vorhin >= MESSINTERVALL) {
vorhin = jetzt;
unsigned int z = zaehler;
zaehler = 0;
wind = z / 2.4; // ob diese Rechnung stimmt, hängt vom Sensor ab!
Serial.print("zaehler: ");
Serial.print(z);
Serial.print("\tWindgeschwindigkeit: ");
Serial.print(wind); //Speed in Km/h
Serial.println(" km/h - ");
if (wind > 14 && linearPos > 700) {
Serial.println("Merker setzen für Einfahren!");
digitalWrite(motorPin, HIGH);
digitalWrite(reverserPin, LOW);
}
if (wind < 10) {
Serial.println("Merker setzen für Ausfahren!");
}
}
}
void countWind() {
zaehler ++;
}
void schrittMotor() {
WestLDRValue = analogRead(WestLDRPin); // West photoresistor circuit Vout reading
float newWestLDRValue = map_to_float(WestLDRValue, 0, 1023, 0, 5);
EastLDRValue = analogRead(EastLDRPin); // East photoresistor circuit Vout reading
float newEastLDRValue = map_to_float(EastLDRValue, 0, 1023, 0, 5);
solarValue = analogRead(solarPin); // Solar Panel circuit Vout reading
float newSolarValue = map_to_float(solarValue, 0, 1023, 0, 5);
delay(1000);
if ((newWestLDRValue - newEastLDRValue) >= thresholdLR && (newWestLDRValue - newEastLDRValue) > 0 && angLR >=
(angMin - 0.01) && angLR < angleMax) {
turnCW(Motor1);
angLR += stepsize;
}
else if (newEastLDRValue - newWestLDRValue >= thresholdLR && (newEastLDRValue - newWestLDRValue) > 0 && angLR >
angMin && angLR < angleMax) {
turnCCW(Motor1);
angLR -= stepsize;
}
else {
shutdown(Motor1);
angLR = angLR;
}
Serial.print("Millis: ");
Serial.println(millis()); //prints all information to be copied and pasted into a csv for later plotting
Serial.print("AngleWestEast: ");
Serial.println(angLR);
Serial.print("NewSolarValue:");
Serial.println(newSolarValue);
Serial.print("SolarValue:");
Serial.println(solarPin);
Serial.print("\n");
}
void turnCW(int Digital0) {
pinMode(Digital0, OUTPUT); // Set motor pins to output
pinMode(Digital0 + 1, OUTPUT);
digitalWrite(Digital0, LOW); // CW Direction
digitalWrite(Digital0 + 1, HIGH); // Send pulse for motor to trigger on
delay(10);
digitalWrite(Digital0 + 1, LOW);
delay(100);
}
void turnCCW(int Digital0) {
pinMode(Digital0, OUTPUT); // Set motor 1pins to output
pinMode(Digital0 + 1, OUTPUT);
digitalWrite(Digital0, HIGH); // CCW Direction
digitalWrite(Digital0 + 1, HIGH); // Send pulse for motor to trigger on
delay(10);
digitalWrite(Digital0 + 1, LOW);
delay(100);
}
void idle(int Digital0) {
pinMode(Digital0, OUTPUT);
pinMode(Digital0 + 1, OUTPUT);
digitalWrite(Digital0, LOW);
digitalWrite(Digital0 + 1, LOW);
}
void shutdown(int Digital0) {
pinMode(Digital0, INPUT);
pinMode(Digital0 + 1, INPUT);
}
float map_to_float(float x, float a, float b, float c, float d)
{
float f = (x - a) / (b - a) * (d - c) + c;
return f;
}