Hallo Leute,
Ich habe einen Zähler gebaut, der die Signale eines Relais zählen soll.
Ab einer gewissen Frequenz der Impulse Zählt das Arduino nur jedes Zweite mal.
Ich vermute, dass der code, der ausgeführt wird wenn ein Signal kommt, das Arduino solange aufhält, sodass der zweite Impuls versäumt wird.
Gibt es eine Möglichkeit das zu verhindern?
Ich freue mich über Vorschläge!
//Display
#include <SoftwareSerial.h>
// Attach the serial display's RX line to digital pin 2
SoftwareSerial mySerial(3,2); // pin 2 = TX, pin 3 = RX (unused)
//Stringarrays erstellen
char countstring[17], gesamtstring[8], averagestring[8];
//Zeitmessung
unsigned long ZeitDerGesamtenMessung; //Zeit seit dem 1. Stück
unsigned long Vorlaufzeit; //Zeit bevor die Messung beginnt
float Gesamtdurchschnitt; //Teile/ Ziet seit 1. Stück
float SecGesamtdurchschnitt; //Ges. / 1000
int hundertSecGesamtdurchschnitt; //Ges * 100
//Running Avarage
unsigned long Time;
unsigned long lastTime;
float timeDistance;
float secTimeDistance;
//Smoothing
const int numReadings = 50;
float readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
float total = 0; // the running total
float average = 0; // the average
float secAverage = 0;
int hundertSecAverage = 0; //* 100
//Anschluss Relais
// this constant won't change:
const int buttonPin = 4; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
//Zähler
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup()
{
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
//Display
mySerial.begin(9600); // set up serial port for 9600 baud
//Running average: Smoothing: initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
//Reset bestätigen
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write("Device is Ready_");
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(192);
mySerial.write(" "); //2. zeile löschen
}
void loop()
{
// Relais: read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState)
{
// if the state has changed, increment the counter
if (buttonState == HIGH)
{
//Zähler
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
//Zeitmessung
ZeitDerGesamtenMessung = millis() - Vorlaufzeit;
Serial.print("Zeit seit 1. Teil: ");
Serial.println(ZeitDerGesamtenMessung);
Gesamtdurchschnitt = ZeitDerGesamtenMessung / buttonPushCounter;
SecGesamtdurchschnitt = Gesamtdurchschnitt / 1000;
hundertSecGesamtdurchschnitt = SecGesamtdurchschnitt * 100;
Serial.print("Gesamtdurchschnitt pro 100 in Sec: ");
Serial.println(hundertSecGesamtdurchschnitt);
//Running Avarage
Time = millis();
timeDistance = Time - lastTime;
secTimeDistance = timeDistance / 1000;
Serial.print("Zeit/Teil ");
Serial.println(secTimeDistance);
//Smoothing
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = timeDistance;
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
secAverage = average / 1000;
hundertSecAverage = secAverage * 100;
Serial.print("Momentandurchschnitt pro 100 in sec: ");
// send it to the computer as ASCII digits
Serial.println(hundertSecAverage);
//Running Avarage last time aktualisieren
lastTime = millis();
//Daten auf Display ausgeben
//Clear Display
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write(" "); // clear display
mySerial.write(" ");
//Anzahl
sprintf(countstring,"%16d",buttonPushCounter); // create strings from the numbers mit 16 Platzhaltern
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write(countstring); //anzahl Drucken
//Gesamtdurchscnitt
sprintf(gesamtstring,"%7d",hundertSecGesamtdurchschnitt); // create strings from the numbers mit 7 Platzhaltern
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(192);
mySerial.write(gesamtstring); //anzahl Drucken
//Momentandurchschnitt
sprintf(averagestring,"%7d",hundertSecAverage); // create strings from the numbers mit 7 Platzhaltern
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(201);
mySerial.write(averagestring); //anzahl Drucken
}
else
{
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
//Vorlaufzeit aktualisiseren
if (buttonPushCounter < 1)
{
Vorlaufzeit = millis();
}
}