I am using spot detection type AMN 2311 and 1311.
I think a pulldown on the analog resistor wouldn't effect the overall system to the point where its necessary. The multimeter shows an idle around 2.5V DC and goes to ±1V DC when motion is detected. So thats why my threshold voltages are at 400 and 600 because on the O-scope shows the analog sensor dipping either above or below 2.5V, and theres no rhyme or reason why it chooses to go either below or above 2.5V. So thats why I didn't care to much. Yes, I could use a comparator and force to a 1 or 0 but at this rate again I don't have much time to keep fiddling around.
A pulldown might help but I don't think it will fix my issue.
Since the arduino's ADC converts to a value between 0 and 1023, I can simply add 500 to my analogRead value and that should effectively keep the sensor between 500 and 1023, according to analogRead. But again I don't think this fixed my issue either. Once I have a chance to test on Monday I can update with a possible solution.
Here is my code in its entirety. I'm trying to record speed, the count from each direction of the passing vehicle, and the overall concentration of vehicles on the road, then save to a SD for later analysis.
#define STATE_IDLE 0
#define STATE_DETECT_FROM_LEFT 1
#define STATE_DETECT_FROM_RIGHT 2
#define STATE_WAIT_FOR_RESET 3
#include <LiquidCrystal.h>
#include <SD.h>
const int chipSelect = 17;
int state = STATE_IDLE;
unsigned long sample_start = 0;
unsigned long sample_end = 0;
unsigned long countTime = 0;
int counter, counterLeft, counterRight, concentration = 0;
boolean digitalsensor;
boolean analogsensor;
int digitalPin = 2;
int analogPin = A0;
int minvalue = 400;
int maxvalue = 600;
float speed_0;
LiquidCrystal lcd(0, 13, 9, 4, 5, 6, 7);
void setup()
{
delay(2000);
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
Serial.begin(9600);
while(!Serial) {
;
}
Serial.print("Initializing SD card...");
lcd.print("Initializing SD");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("SD card failed, or not present");
lcd.setCursor(0,1);
lcd.print("SD card failed");
// don't do anything more:
return;
}
Serial.println("SD card initialized.");
lcd.setCursor(0,0);
lcd.print("SD card initialized.");
delay(1000);
lcd.clear();
pinMode(analogPin, INPUT);
pinMode(digitalPin, INPUT);
File dataFile = SD.open("newdata.txt", FILE_WRITE);
if (dataFile)
{
dataFile.println(", , , ,"); //Just a leading blank line, incase there was previous data
String header = "Speed | From Left | From Right | Concentration";
dataFile.println(header);
dataFile.close();
Serial.println(header);
}
else
{
Serial.println("Couldn't open log file");
}
}
void loop()
{
digitalsensor = digitalRead(digitalPin);
int rightanalog = analogRead(analogPin);
if (rightanalog >= minvalue && rightanalog <= maxvalue)
{
analogsensor = HIGH;
}
else
{
analogsensor = LOW;
}
switch(state)
{
case STATE_IDLE:
{
if (digitalsensor == HIGH)
{
sample_start = millis();
counter++;
counterLeft++;
state = STATE_DETECT_FROM_LEFT;
}
else if (analogsensor == HIGH)
{
sample_start = millis();
counter++;
counterRight++;
state = STATE_DETECT_FROM_RIGHT;
}
}
break;
case STATE_DETECT_FROM_LEFT:
{
if (analogsensor == HIGH)
{
// The analog sensor just triggered, this means
// we should calculate and display the speed.
sample_end = millis();
calculateData();
state = STATE_IDLE;
}
}
break;
case STATE_DETECT_FROM_RIGHT:
{
if (digitalsensor == HIGH)
{
// The digital sensor just triggered, this means
// we should calculate and display the speed.
sample_end = millis();
calculateData();
}
}
break;
case STATE_WAIT_FOR_RESET:
{
if (digitalsensor == LOW && analogsensor == LOW)
{
state = STATE_IDLE;
}
}
break;
}
}
void calculateData()
{
unsigned long time = (sample_end - sample_start)/1000;
// do speed conversion math here using duration
// speed is in MPH
float speed_0 = (4/time) * 0.681818;
// determine time since program start
// determine concentration
countTime = ((sample_start)/1000)/60;
concentration = counter/countTime;
// display results here
displayData();
// delay until sensors reset
state = STATE_WAIT_FOR_RESET;
}
void displayData(){ // displays/saves data to Serial, LCD, and SD file
lcd.setCursor(0,0);
// Displays Speed
lcd.print("Speed=");
lcd.print(speed_0);
lcd.print(" MPH");
lcd.setCursor(0,1);
lcd.print("R = ");
lcd.print(counterRight);
lcd.print(", L = ");
lcd.print(counterLeft);
// open the file, note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("newdata.txt", FILE_WRITE);
if (dataFile) {
dataFile.print(speed_0);
dataFile.print(" ");
dataFile.print(counterLeft);
dataFile.print(" ");
dataFile.print(counterRight);
dataFile.print(" ");
dataFile.println(concentration);
//dataFile.println(trafficData);
dataFile.close();
// print to the serial port too:
Serial.print(speed_0);
Serial.print(" ");
Serial.print(counterLeft);
Serial.print(" ");
Serial.print(counterRight);
Serial.print(" ");
Serial.println(concentration);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening newdata.txt");
}
}