OK, christmas, free time, hardware and arduino at hand. Got everything hooked up and my code works after some modifications. I realized the hard way that when iside a interrupt routine millis() isn't ticking along. Well, modified and working in my test setup. Here is the code. One problem to solve is that if something breaks one laser beam and not the other the code assumes a car is moving inside the speedgate and calculates a very low speed when the second beem eventually is broken. Have tried several ways to reset this but no good idea works for me.
int upSpeed, downSpeed; //Speed calculated in each direction
int upCarCounter=0, downCarCounter=0, totalCars=0; //Number of cars in each direction and total
int upMaxSpeed=0, downMaxSpeed=0; //Max speed for both directions
int upMinSpeed=999, downMinSpeed=999; //Minimum speed for both directions
long upAverageSpeed=0, downAverageSpeed=0, totalAverageSpeed=0; //Sum of speeds to calculate average from
int upCarCounterHour=0, downCarCounterHour=0, totalCarsHour=0; //Number of cars in each direction and total
int upMaxSpeedHour=0, downMaxSpeedHour=0; //Max speed for both directions
int upMinSpeedHour=999, downMinSpeedHour=999; //Minimum speed for both directions
long upAverageSpeedHour=0, downAverageSpeedHour=0, totalAverageSpeedHour=0; //Sum of speeds to calculate average from
long upGateTime=0, downGateTime=0; //When interrupt happened, car passing time gate
long lastUpGateTime=0, lastDownGateTime=0; //Previous time a car passed
long hourTimer=0; //Timer to control hourly output
int hourCounter=0; //Count hours
int upTime, downTime; //Time for car to pass gate in each direction
int distance=10000; // Distance betveen upwards and downwards gate in mm
int upGate=2; //Pin for up gate
int downGate=3; //Pin for down gate
void setup()
{
pinMode(upGate, INPUT); //Setup pins for input
pinMode(downGate, INPUT);
Serial.begin(9600);
attachInterrupt(0, upwardsTimer, FALLING); //Set up interrupts to call when change happens, car is passing gate
attachInterrupt(1, downwardsTimer, FALLING);
}
void loop()
{
if (millis()-hourTimer>10000) //Check if 60 minutes has passed
{
hourTimer=millis(); //Reset hour timer for new measurement period
hourlyPrintout(); //Print the total and this hours stats
resetHourlyStats(); //Reset hourly stats for new period
hourCounter++; //Increase hour counter
}
if (upGateTime!=lastUpGateTime && downGateTime!=lastDownGateTime) // Both gates have changed so a car has passed
{
if (upGateTime<downGateTime) //Did it pass downwards or upwards?
downwardsRun();
else
upwardsRun();
lastUpGateTime=upGateTime; //Reset timers
lastDownGateTime=downGateTime;
}
}
void upwardsRun() //Prints stats for car going upwards
{
upTime=upGateTime-downGateTime; //How long for the car to pass the gate
upSpeed=distance/upTime*36/10; //Calculate speed in Km/h
if(upSpeed<upMinSpeed) //update fastest and slowest speed
upMinSpeed=upSpeed;
if(upSpeed>upMaxSpeed)
upMaxSpeed=upSpeed;
if(upSpeed<upMinSpeedHour) //update fastest and slowest speed this hour
upMinSpeedHour=upSpeed;
if(upSpeed>upMaxSpeedHour)
upMaxSpeedHour=upSpeed;
upAverageSpeed=upAverageSpeed+upSpeed; //Update average speeds and car counters
totalAverageSpeed=totalAverageSpeed+upSpeed;
upCarCounter++;
totalCars++;
upAverageSpeedHour=upAverageSpeedHour+upSpeed; //Update average speeds and car counters this hour
totalAverageSpeedHour=totalAverageSpeedHour+upSpeed;
upCarCounterHour++;
totalCarsHour++;
Serial.print("Car number ");
Serial.print(totalCars);
Serial.print(" upwards speed: "); //Print the speed
Serial.println(upSpeed);
Serial.println("");
}
void downwardsRun() //Prints stats for car going downwards
{
downTime=downGateTime-upGateTime; //How long for the car to pass the gate
downSpeed=distance/downTime*36/10; //Calculate speed in Km/h
if(downSpeed<downMinSpeed) //update fastest and slowest speed
downMinSpeed=downSpeed;
if(downSpeed>downMaxSpeed)
downMaxSpeed=downSpeed;
if(downSpeed<downMinSpeedHour) //update fastest and slowest speed this hour
downMinSpeedHour=downSpeed;
if(downSpeed>downMaxSpeedHour)
downMaxSpeedHour=downSpeed;
downAverageSpeed=downAverageSpeed+downSpeed; //Update average speeds and car counters
totalAverageSpeed=totalAverageSpeed+downSpeed;
downCarCounter++;
totalCars++;
downAverageSpeedHour=downAverageSpeedHour+downSpeed; //Update average speeds and car counters this hour
totalAverageSpeedHour=totalAverageSpeedHour+downSpeed;
downCarCounterHour++;
totalCarsHour++;
Serial.print("Car number ");
Serial.print(totalCars);
Serial.print(" downwards speed: "); //Print the speed
Serial.println(downSpeed);
Serial.println("");
}
void hourlyPrintout() //Prints hourly stats
{
Serial.println("----------------------------------------------------------"); //Print all info about the cars
Serial.println("");
Serial.print(" Hour interval: ");
Serial.print(hourCounter);
Serial.print("-");
Serial.println(hourCounter+1);
Serial.print(" Total cars passed: ");
Serial.print(totalCars);
Serial.print("\t Total cars passed this hour: ");
Serial.println(totalCarsHour);
Serial.print(" Total Average speed: ");
Serial.print(totalAverageSpeed/totalCars);
Serial.print("\t Total Average speed this hour: ");
Serial.println(totalAverageSpeedHour/totalCarsHour);
Serial.println("");
Serial.print(" Cars passed upwards: ");
Serial.print(upCarCounter);
Serial.print("\t Cars passed upwards this hour: ");
Serial.println(upCarCounterHour);
Serial.print(" Upwards Average speed: ");
Serial.print(upAverageSpeed/upCarCounter);
Serial.print("\t Upwards Average speed this hour: ");
Serial.println(upAverageSpeedHour/upCarCounterHour);
Serial.print(" Upwards Max speed: ");
Serial.print(upMaxSpeed);
Serial.print("\t Upwards Max speed this hour: ");
Serial.println(upMaxSpeedHour);
Serial.print(" Upwards Min speed: ");
Serial.print(upMinSpeed);
Serial.print("\t Upwards Min speed this hour: ");
Serial.println(upMinSpeedHour);
Serial.println("");
Serial.print(" Cars passed downwards: ");
Serial.print(downCarCounter);
Serial.print("\t Cars passed downwards this hour: ");
Serial.println(downCarCounterHour);
Serial.print("Downwards Average speed: ");
Serial.print(downAverageSpeed/downCarCounter);
Serial.print("\tDownwards Average speed this hour: ");
Serial.println(downAverageSpeedHour/downCarCounterHour);
Serial.print(" Downwards Max speed: ");
Serial.print(downMaxSpeed);
Serial.print("\t Downwards Max speed this hour: ");
Serial.println(downMaxSpeedHour);
Serial.print(" Downwards Min speed: ");
Serial.print(downMinSpeed);
Serial.print("\t Downwards Min speed this hour: ");
Serial.println(downMinSpeedHour);
Serial.println("");
Serial.println("");
}
void resetHourlyStats()
{
totalCarsHour=0; //Reset hourly stats for new measurement period
upCarCounterHour=0;
downCarCounterHour=0;
upMaxSpeedHour=0;
downMaxSpeedHour=0;
upMinSpeedHour=999;
downMinSpeedHour=999;
upAverageSpeedHour=0;
downAverageSpeedHour=0;
totalAverageSpeedHour=0;
}
void upwardsTimer() //Car has passed upper gate
{
upGateTime=millis(); //Time when car passed gate
/*
Serial.print("Upper gate passed at: ");
Serial.println(upGateTime);
Serial.println("");
*/
}
void downwardsTimer() //Car has passed lower gate
{
downGateTime=millis(); //Time when car passed gate
/*
Serial.print("Lower gate passed at: ");
Serial.println(downGateTime);
Serial.println("");
*/
}
/Ulf