Hallo Freunde,
eine kleine Spielerei um den Gaszähler abzulesen und die Leistung zu ermitteln und nebenbei eine Übung für mein eigentliches Projekt GSM-Modul...
Lichtschranke am Analogpin wird mittels Schwellenwerten getriggert. Das klappt.
Die Idee ist übrigens aus einem anderen Sketch entliehen.
Getestet wird momentan von Hand mit einem kleinen Spiegel.
Was nicht klappt ist die Berechnung von "duration".
Im Serial Monitor kommen immer 3...5 ms raus und die laufen ständig durch solange triggerState HIGH ist. Dann folgt die "0" von Serial.println(triggerState? 1 : 0);.
Auf dem LCD wird statt duration auch nur Unsinn abgebildet.
Lediglich triggerState wechselt wunschgemäß.
Offensichtlich habe ich da irgendwo einen Denkfehler drin, finde ihn aber nicht.
Kann bitte mal jemand unvoreingenommen da drauf schauen - ich seh den Wald grad vor lauter Bäumen nicht...
/*
unTested!
may not work!
*/
int sensorValueOff = 0; // value read from the photo transistor when ir LED is off
int sensorValueOn = 0; // value read from the photo transistor when ir LED is on
int triggerLevelLow = 300; // trigger state and levels
int triggerLevelHigh = 600;
unsigned long oldMillis = 0; //counters to determine duration
unsigned long newMillis = 0;
unsigned long countMillis = 0;
unsigned long oldCountMillis = 0;
unsigned long duration = 0;
boolean triggerState = false; //the trigger itself
boolean lastTriggerState = false;
const int analogInPin = A0; // Analog input pin that the photo transistor is attached to
const int irOutPin = 12; // Digital output pin that the IR-LED is attached to
const int ledOutPin = 13; // Signal LED output pin
#include <LiquidCrystal.h> // include the library code
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
/*
The circuit:
* LCD RS pin to digital pin 7
* LCD Enable pin to digital pin 6
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3) */
// the setup routine runs once when you press reset:
void setup()
{
// initialize theese digital pins as an output.
pinMode(irOutPin, OUTPUT);
pinMode(ledOutPin, OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2); //initialize display with 16 columns ans two rows (row 0 and row 1)
lcd.display();// Turn on the display
lcd.clear(); //erase the display once
// Print a message to the LCD.
lcd.print("Booting...");
delay(1000); //add some time to read the message
lcd.clear(); //erase again
lcd.setCursor(0, 0);
lcd.print("TriggerState:");
lcd.print(triggerState? 1 : 0);
}
/**
* Detect and print a trigger event
*/
void detectTrigger(int val)
{
boolean nextState = triggerState; //could have been declared as false too
if (val > triggerLevelHigh)
{
nextState = true;
}
else if (val < triggerLevelLow)
{
nextState = false;
}
if (nextState != triggerState)
{
triggerState = nextState;
lastTriggerState = !nextState; //triggerState and lastTriggerState are opposite only if triggerState changed
lcd.setCursor(0, 0);
lcd.print("TriggerState:");
lcd.print(triggerState? 1 : 0);
Serial.println(triggerState? 1 : 0);
digitalWrite(ledOutPin, triggerState); // control internal LED
}
}
// the loop routine runs over and over again forever
void loop()
{
/* perform measurement */
newMillis = millis();
if ((newMillis - oldMillis) > 100)
{
oldMillis = newMillis;
digitalWrite(irOutPin, LOW);// turn IR LED off
delay(10);// wait 10 milliseconds
sensorValueOff = analogRead(analogInPin);// read the analog in value
digitalWrite(irOutPin, HIGH);// turn IR LED on
delay(10);
sensorValueOn = analogRead(analogInPin);// read the analog in value
detectTrigger(sensorValueOn - sensorValueOff);// detect and output trigger
// Serial.println(sensorValueOn - sensorValueOff); //wird auf Serial ausgegeben
lcd.setCursor(0, 1);
lcd.print(sensorValueOn - sensorValueOff);
lcd.print(" "); //if the printed value is lass than four characters long, the previous characters may still be left and are erased now
}
/*determine duration between two trigger events*/
if ((triggerState != lastTriggerState) && (triggerState == true))
{
countMillis = millis();
duration = countMillis - oldCountMillis;
oldCountMillis = countMillis;
lcd.setCursor(6, 1);
lcd.print(duration);
Serial.println(duration);
}
}