Hi all,
I am working on a simple laser tripwire (using a ky-008 laser and reciever), modified for my needs.
I have code which reads the value of the analog input, waits 10ms, then reads it again.
It then compares these two values and if the second value is high when the first value is low, it increments a counter. I only want to increment on the rising edge from low to high.
If i run my code, it usually works, but i notice sometimes the value changes from low to high but the comparison and subsequent code does not trigger.
this is a snippet of what the good operation and bad operation looks like.
//BAD
11:50:33.304 -> 0
11:50:33.304 -> 0
11:50:33.304 -> 0
11:50:33.304 -> 0
11:50:33.304 -> 0
11:50:33.336 -> 0
11:50:33.336 -> 1023
11:50:33.336 -> 1023
11:50:33.336 -> 1023
11:50:33.336 -> 1023
11:50:33.336 -> 1023
11:50:33.373 -> 1023
11:50:33.373 -> 1023
11:50:33.373 -> 0
11:50:33.373 -> 0
11:50:33.373 -> 0
11:50:33.373 -> 0
11:50:33.411 -> 0
11:50:16.067 -> 0
11:50:16.067 -> 0
11:50:16.067 -> 0
11:50:16.102 -> 0
11:50:16.102 -> 1023
11:50:16.102 -> 1023
11:50:16.102 -> 1023
11:50:16.102 -> 1023
11:50:16.102 -> 1023
11:50:16.137 -> 1023
11:50:16.137 -> 1023
11:50:16.137 -> 0
11:50:16.137 -> 0
11:50:16.137 -> 0
//Good
1:50:32.635 -> 0
11:50:32.669 -> 0
11:50:32.669 -> 0
11:50:32.669 -> 1023
11:50:32.669 -> Cycles: 19 Time: 49506
11:50:32.669 ->
11:50:32.704 -> 1023
11:50:32.704 -> 1023
11:50:32.704 -> 1023
11:50:32.704 -> 0
11:50:32.704 -> 0
11:50:32.739 -> 0
11:50:15.430 -> 0
11:50:15.430 -> 0
11:50:15.430 -> 0
11:50:15.430 -> 0
11:50:15.430 -> 1023
11:50:15.430 -> Cycles: 12 Time: 32271
11:50:15.430 ->
11:50:15.464 -> 1023
11:50:15.464 -> 1023
11:50:15.464 -> 1023
11:50:15.464 -> 0
11:50:15.464 -> 0
11:50:15.502 -> 0
11:50:15.502 -> 0
11:50:15.502 -> 0
I noticed that the timestamps in a proper operation have a few 0's before the cycle (on the same timestamp) while the bad ones only have one timestamp. Below is my code, any suggestions would be much appreciated!
#include <SPI.h>
#include <SD.h>
const int chipSelect = SDCARD_SS_PIN;
int sensor = 0;
int cycles = 0;
int prev_val = 0;
int curr_val = 0;
unsigned long Elapsed = 0;
void setup()
{
Serial.begin(9600);
pinMode(A1, OUTPUT);
while (!Serial)
{
;// wait until serial init
}
Serial.print("Initializing SD Card...");
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
}
void loop()
{
prev_val = 0;
curr_val = 0;
CheckSensor();
prev_val = sensor;
//Serial.print(prev_val);
delay(10);
CheckSensor();// Store value, then wait 10ms, then measure again to check if change
curr_val = sensor;
//Serial.print(curr_val);
delay(1);
if ((curr_val == 1023 && prev_val == 0))//if rising edge (LOW to HIGH)
{
cycles = cycles + 1;
Elapsed = millis();
Serial.print("Cycles: ");
Serial.print(cycles);
Serial.print(" ");
Serial.print("Time: ");
Serial.println(Elapsed); // time in milliseconds since program began
Serial.println(" ");
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile)
{
dataFile.print("Cycles: ");
dataFile.println(cycles);
dataFile.print(" ");
dataFile.print("Time: ");
dataFile.println(Elapsed);
dataFile.print(" ");
dataFile.close();
// print to the serial port too:
//Serial.println(cycles);
}
// if the file isn't open, pop up an error:
else
{
Serial.println("error opening datalog.txt");
}
}
}
void CheckSensor()
{
sensor = analogRead(A1);
if(sensor > 500)//map to two states, high or low, 500 is arbitrary
{
sensor = 0;
//Serial.println(sensor);
}
else
{
sensor = 1023;
//Serial.println(sensor);
// delay(1000);
}
}