my first code - speed camera

right so this is my first attempt at writeing arduno code

its for a jokey speed camera for RC cars
its not actually a “speed camera” its just a laser trip wire and the object has to pass through the beam in a quick enough time

i would really really apreceate if people could take a quick look and see if iv done stuff im not alowed to do or theres a line missing or anything really! iv enjoyed this and it was easier than i thought. looking forward to meny projects!

#define flash 10 //pin to flash camera
#define laser 9 //pin for laser beam
#define led 8   //pin for status led
#define button 7  //pin for button
#define led2    //pin for car seen led

int val = 0;
int button1 = 0:
int light = 0;
int dark = 0;
int split = 0;
int trigger = 0;
unsigned long fronttime = 0;
float speedmph = 0;     
int carsize = 0;     
float speedmps = 0;
int carpasstime = 0;
int counter = 0;



void setup() {
  pinmode(flash, output);
  pinmode(laser, output);
  pinmode(led, output);
  pinmode(led2, output);
  pinmode(button, input);
  
}
  void calabrate(){
  
  digitalwrite(laser, high);  //turn on laser
   delay 300;                          //user alighns mirror
   
button1 = digitalread(button);     // waits until button is pressed
while (button1 = 0) {
  digitalwrite(led, high);         //flashes the led while waiting
  delay(200);
  digitalwrite(led, low);
  delay(200);
  button1 = digitalread(button); 
}
                           
  light = analogread(0);      //set the LDR value for light
  digitalwrite(laser, low);       //turn laser off
  delay(400);                    //let laser run down and LDR desaturate
  dark = analogread(0); //set the LDR value for dark
  

  split = (dark - light);   //calculate diffrence
  trigger= ((split / 2 ) + light);  //calculates trigger point which is 1/3 abouve the difrence between light and dark
  
  
speedmph = 15;     //enter camera trigger speed here <-----
carsize = 440;     // enter car size in mm here  <--------
  
  speedmps = (speedmph * 0.44704);   //comvert MPH to millimeters per millisecond (or meters per second)
  
 
  carpasstime = (carsize / speedmps);    //find the time it takes for a car off carsize lenght to pass at speedmph speed
  
  
  
  digitalwrite(led, high); //turn led on
  digitalwire(laser, high);  //turn laser on
  }
  void loop(){
  
  
  
  val = analogread(0);     //read the LDR
  if (val > trigger) {         //if the ldr has no light then do the follwing:
    
     fronttime = millis();           //record the time the beam was broken
     digitalwrite(led2, high);     //turn the car sense led on
     val = analogread(0);          //read the LDR
     while (val > trigger){
     delay(5);                    //wait for light to retun 
     val = analogread(0);          //read the LDR
     }
     digitalwrite(led2, low)            //turn led back off
    
     
    if ((millis() - fronttime) < carpasstime) {      //was the car going fast enough?
      
      digitalwrite(flash, high) ;         //turn the flash on 
      delay() 1000                       //give it  a second to go off properly
      digitalwrite(flash, low) ;          // turn it back off
      digitalwrite(led, low) ;           //turn off led  
   

     digitalwrite(laser, low);       //turn laser off in sleep mode
      
      
      
     counter = 0                          //reset counter
     while (counter < 60) && (button1 == 0){     //is the button pressed or is the counter past the alloted time?
     button1 = digitalread(button);          
    delay(700);
    digitalwrite(led, high);
    delay(700);
    digitalwrite(led, low);                   //read button to see if its been pressed
    counter++;                                //add one to counter
     }
      
    }
       
    
    
    
   }
  }
 delay(5)
 
    }

pinmode(flash, output);

should be:
pinMode(flash,OUTPUT);

digitalwrite needs to be digitalWrite and digitalread, digitalRead.

Arduino language is case sensitive.

once you get the code to compile let us know what happens and we can help to make it work

ahh that makes sence! i just realised there was a verifi function and it keeps telling me bal bla was not declared in the scope lol

If you get stuck post the code and the errors. I have had mucho experience decoding the cryptic IDE error messages as I am a beginner in Arduino C as well. : )

i had a list off about 50 errors
iv managed to resolve them all XD XD XD

its compleing now

how dose it look???

#define flash 10 //pin to flash camera
#define laser 9 //pin for laser beam
#define led 8   //pin for status led
#define button 7  //pin for button
#define led2  6  //pin for car seen led

int val = 0;
int button1 = 0;
int light = 0;
int dark = 0;
int split = 0;
int trigger = 0;
unsigned long fronttime = 0;
float speedmph = 0;     
int carsize = 0;     
float speedmps = 0;
int carpasstime = 0;
int counter = 0;



void setup() {
  
  pinMode(flash, OUTPUT);
  pinMode(laser,OUTPUT);
  pinMode(led,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(button,INPUT);
  
}
  void calabrate(){
  
  digitalWrite(laser,HIGH);  //turn on laser
   delay(300);                          //user alighns mirror
   
button1 = digitalRead(button);     // waits until button is pressed
while (button1 = 0) {
  digitalWrite(led,HIGH);         //flashes the led while waiting
  delay(200);
  digitalWrite(led,LOW);
  delay(200);
  button1 = digitalRead(button); 
}
                           
  light = analogRead(0);      //set the LDR value for light
  digitalWrite(laser,LOW);       //turn laser off
  delay(400);                    //let laser run down and LDR desaturate
  dark = analogRead(0); //set the LDR value for dark
  

  split = (dark - light);   //calculate diffrence
  trigger = ((split / 2 ) + light);  //calculates trigger point which is 1/3 abouve the difrence between light and dark
  
  
speedmph = 15;     //enter camera trigger speed here <-----
carsize = 440;     // enter car size in mm here  <--------
  
  speedmps = (speedmph * 0.44704);   //comvert MPH to millimeters per millisecond (or meters per second)
  
 
  carpasstime = (carsize / speedmps);    //find the time it takes for a car off carsize lenght to pass at speedmph speed
  
  
  
  digitalWrite(led, HIGH); //turn led on
  digitalWrite(laser, HIGH);  //turn laser on
  }
  void loop(){
  
  
  
  val = analogRead(0);     //read the LDR
  if (val > trigger) {         //if the ldr has no light then do the follwing:
    
     fronttime = millis();           //record the time the beam was broken
     digitalWrite(led2,HIGH);     //turn the car sense led on
     val = analogRead(0);          //read the LDR
     while (val > trigger){
     delay(5);                    //wait for light to retun 
     val = analogRead(0);          //read the LDR
     }
     digitalWrite(led2, LOW);            //turn led back off
    
     
    if ((millis() - fronttime) < carpasstime) {      //was the car going fast enough?
      
      digitalWrite(flash, HIGH);         //turn the flash on 
      delay(1000);                        //give it  a second to go off properly
      digitalWrite(flash, LOW);          // turn it back off
      digitalWrite(led, LOW);           //turn off led  
   

     digitalWrite(laser, LOW);       //turn laser off in sleep mode
      
      
      
     counter = 0;                        //reset counter
     while (counter < 60 && button1 == 0){     //is the button pressed or is the counter past the alloted time?
     button1 = digitalRead(button);          
    delay(700);
    digitalWrite(led, HIGH);
    delay(700);
    digitalWrite(led, LOW);                   //read button to see if its been pressed
    counter++;                                //add one to counter
     }
      
    }
       
    
    
    
   }
  
 delay(5);
 
    }
  1. please usae the CTRL-T for auto indentation, makes your code more readable

  2. #defines are always written with UPPERCASE, thats a C-style already 40+ yrs old.
    helps you to see in your code what the consts are from style.

#define FLASH 10 //pin to flash camera

  1. use can use a header above the code, e.g. like,

// FILE:
// AUTHOR:
// VERSION: 0.1.00
// PURPOSE:
// DATE:
// URL:
//
// COPYRIGHT: Released to the public domain
//

other fields are of course possibly

right

a little cleaner now
anything else?

id perticualy like to know if one bit iv done is acceptable

iv got void setup()
then void calabration()
then void loop()

im thinking it run through the setup. then calibrate and then go round and round the loop?

//    FILE: Speed_cam
//  AUTHOR: Josh Sams
// VERSION: 0.1.00
// PURPOSE: RC Speed camera using laser trip wire
//    DATE:11/10/13
//
// COPYRIGHT: Released to the public domain (not for profit)
//


#define FLASH 10 //pin to FLASH camera
#define LASER 9 //pin for LASER beam
#define LED 8   //pin for status LED
#define button 7  //pin for button
#define LED2  6  //pin for car seen LED

int val = 0;
int button1 = 0;
int light = 0;
int dark = 0;
int split = 0;
int trigger = 0;
unsigned long fronttime = 0;
float speedmph = 0;     
int carsize = 0;     
float speedmps = 0;
int carpasstime = 0;
int counter = 0;



void setup() {

  pinMode(FLASH, OUTPUT);
  pinMode(LASER,OUTPUT);
  pinMode(LED,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(button,INPUT);

}
void calabrate(){

  digitalWrite(LASER,HIGH);  //turn on LASER
  delay(300);                          //user alighns mirror

  button1 = digitalRead(button);     // waits until button is pressed
  while (button1 = 0) {
    digitalWrite(LED,HIGH);         //FLASHes the LED while waiting
    delay(200);
    digitalWrite(LED,LOW);
    delay(200);
    button1 = digitalRead(button); 
  }

  light = analogRead(0);      //set the LDR value for light
  digitalWrite(LASER,LOW);       //turn LASER off
  delay(400);                    //let LASER run down and LDR desaturate
  dark = analogRead(0); //set the LDR value for dark


  split = (dark - light);   //calculate diffrence
  trigger = ((split / 2 ) + light);  //calculates trigger point which is 1/3 abouve the difrence between light and dark


  speedmph = 15;     //enter camera trigger speed here <-----
  carsize = 440;     // enter car size in mm here  <--------

  speedmps = (speedmph * 0.44704);   //comvert MPH to millimeters per millisecond (or meters per second)


  carpasstime = (carsize / speedmps);    //find the time it takes for a car off carsize lenght to pass at speedmph speed



  digitalWrite(LED, HIGH); //turn LED on
  digitalWrite(LASER, HIGH);  //turn LASER on
}
void loop(){



  val = analogRead(0);     //read the LDR
  if (val > trigger) {         //if the ldr has no light then do the follwing:

    fronttime = millis();           //record the time the beam was broken
    digitalWrite(LED2,HIGH);     //turn the car sense LED on
    val = analogRead(0);          //read the LDR
    while (val > trigger){
      delay(5);                    //wait for light to retun 
      val = analogRead(0);          //read the LDR
    }
    digitalWrite(LED2, LOW);            //turn LED back off


    if ((millis() - fronttime) < carpasstime) {      //was the car going fast enough?

      digitalWrite(FLASH, HIGH);         //turn the FLASH on 
      delay(1000);                        //give it  a second to go off properly
      digitalWrite(FLASH, LOW);          // turn it back off
      digitalWrite(LED, LOW);           //turn off LED  


      digitalWrite(LASER, LOW);       //turn LASER off in sleep mode



      counter = 0;                        //reset counter
      while (counter < 60 && button1 == 0){     //is the button pressed or is the counter past the alloted time?
        button1 = digitalRead(button);          
        delay(700);
        digitalWrite(LED, HIGH);
        delay(700);
        digitalWrite(LED, LOW);                   //read button to see if its been pressed
        counter++;                                //add one to counter
      }

    }




  }

  delay(5);

}

Once the code has been compiled there is a process that runs that calls setup() once and the repeatedly calls loop() to achieve the looping effect. Other functions will only be called if they are called by your code in of setup() or loop(), so the order in which they appear does not generally matter.

while (button1 = 0) {
should be
while (button1 == 0) { // or LOW ?

digitalWrite(LED, LOW); // read button to see if its been pressed
in case of a mismatch between code and comment code always wins. Do not use comments if not 100% needed.

calabrate() is never called… Moving code in a function implies you need to call it from another function

  • setup() makes the most sense in this case

Several typos in the comments,

The structure of your code still contains sudden empty lines,
I did a redo of the style (not the logic of the code) took 3 minutes

//    FILE: Speed_cam
//  AUTHOR: Josh Sams
// VERSION: 0.1.00
// PURPOSE: RC Speed camera using laser trip wire
//    DATE:11/10/13
//
// COPYRIGHT: Released to the public domain (not for profit)
//

#define FLASH 10 // pin to FLASH camera
#define LASER 9  // pin for LASER beam
#define LED 8    // pin for status LED
#define button 7 // pin for button
#define LED2  6  // pin for car seen LED

int val = 0;
int button1 = 0;
int light = 0;
int dark = 0;
int split = 0;
int trigger = 0;

unsigned long fronttime = 0;
float speedmph = 0;     
int carsize = 0;     
float speedmps = 0;
int carpasstime = 0;
int counter = 0;


void setup() 
{
  pinMode(FLASH, OUTPUT);
  pinMode(LASER, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(button, INPUT);
}


void calabrate() 
{
  digitalWrite(LASER, HIGH);        // turn on LASER
  delay(300);                       // user alighns mirror

  button1 = digitalRead(button);    // waits until button is pressed
  while (button1 == 0) 
  {
    digitalWrite(LED, HIGH);        // FLASHes the LED while waiting
    delay(200);
    digitalWrite(LED, LOW);
    delay(200);
    button1 = digitalRead(button); 
  }

  light = analogRead(0);            // read the LDR value for light
  digitalWrite(LASER, LOW);         // turn LASER off
  delay(400);                       // let LASER run down and LDR desaturate
  dark = analogRead(0);             // read the LDR value for dark
  split = (dark - light);           // calculate difference
  trigger = ((split / 2 ) + light); // calculates trigger point which is 1/3 abouve the difrence between light and dark

  speedmph = 15;                    // enter camera trigger speed here <-----
  carsize = 440;                    // enter car size in mm here  <--------
  speedmps = (speedmph * 0.44704);  // convert MPH to meters per second
  carpasstime = (carsize / speedmps);    // find the time it takes for a car off carsize lenght to pass at speedmph speed

  digitalWrite(LED, HIGH);          // turn LED on
  digitalWrite(LASER, HIGH);        // turn LASER on
}


void loop()
{
  val = analogRead(0);              // read the LDR
  if (val > trigger)                // if the ldr has no light then do the following:
  {
    fronttime = millis();           // record the time the beam was broken
    digitalWrite(LED2, HIGH);       // turn the car sense LED on
    val = analogRead(0);            // read the LDR
    while (val > trigger){
      delay(5);                     // wait for light to retun 
      val = analogRead(0);          // read the LDR
    }
    digitalWrite(LED2, LOW);        // turn LED back off

    if ((millis() - fronttime) < carpasstime)   // was the car going fast enough?
    {
      digitalWrite(FLASH, HIGH);    // turn the FLASH on 
      delay(1000);                  // give it  a second to go off properly
      digitalWrite(FLASH, LOW);     // turn it back off
      digitalWrite(LED, LOW);       // turn off LED  
      digitalWrite(LASER, LOW);     // turn LASER off in sleep mode

      counter = 0;                  // reset counter
      while (counter < 60 && button1 == 0){     // is the button pressed or is the counter past the alloted time?
        button1 = digitalRead(button);          
        delay(700);
        digitalWrite(LED, HIGH);
        delay(700);
        digitalWrite(LED, LOW);     // read button to see if its been pressed
        counter++;                  // add one to counter
      }
    }
  }
  delay(5);
}
// -- END OF FILE --

search the 7 differences :wink:

right:
code cleaned
calibration section moved

i think iv spotted all 7 errors
the typos arent too important to me. im dyslexic so i don’t even notice them. my brain just reads what i think it says ect
iv been leaning from the “getting started with ardunio” book and it thought me to all-ways write comments so i have been doings so

cheers for your help! have to start building it now!

//    FILE: Speed_cam
//  AUTHOR: Josh Sams
// VERSION: 0.1.00
// PURPOSE: RC Speed camera using laser trip wire
//    DATE:11/10/13
//
// COPYRIGHT: Released to the public domain (not for profit)
//


#define FLASH 10   //pin to FLASH camera
#define LASER 9    //pin for LASER beam
#define LED 8      //pin for status LED
#define BUTTON 7   //pin for BUTTON
#define LED2  6    //pin for car seen LED

int val = 0;
int BUTTON1 = 0;
int light = 0;
int dark = 0;
int split = 0;
int trigger = 0;

unsigned long fronttime = 0;
float speedmph = 0;     
int carsize = 0;     
float speedmps = 0;
int carpasstime = 0;
int counter = 0;



void setup() 
{
  pinMode(FLASH, OUTPUT);
  pinMode(LASER, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(BUTTON, INPUT);

  digitalWrite(LASER, HIGH);            //turn on LASER
  delay(300);                           //user alighns mirror
  BUTTON1 = digitalRead(BUTTON);        // waits until BUTTON is pressed
  while (BUTTON1 == 0) 
  {
    digitalWrite(LED,HIGH);             //FLASHes the LED while waiting
    delay(200);
    digitalWrite(LED,LOW);
    delay(200);
    BUTTON1 = digitalRead(BUTTON); 
  }

  light = analogRead(0);                 //set the LDR value for light
  digitalWrite(LASER,LOW);               //turn LASER off
  delay(400);                            //let LASER run down and LDR desaturate
  dark = analogRead(0);                  //set the LDR value for dark

  split = (dark - light);                //calculate diffrence
  trigger = ((split / 2 ) + light);      //calculates trigger point which is 1/3 abouve the difrence between light and dark

  speedmph = 15;                         //enter camera trigger speed here <-----
  carsize = 440;                         // enter car size in mm here  <--------
  speedmps = (speedmph * 0.44704);       //comvert MPH to millimeters per millisecond (or meters per second)
  carpasstime = (carsize / speedmps);    //find the time it takes for a car off carsize lenght to pass at speedmph speed

  digitalWrite(LED, HIGH);               //turn LED on
  digitalWrite(LASER, HIGH);             //turn LASER on
}
void loop()
{

  val = analogRead(0);                   //read the LDR
  if (val > trigger)                     //if the ldr has no light then do the follwing:
  {                   
    fronttime = millis();                //record the time the beam was broken
    digitalWrite(LED2, HIGH);             //turn the car sense LED on
    val = analogRead(0);                 //read the LDR
    while (val > trigger)
    {
      delay(5);                          //wait for light to retun 
      val = analogRead(0);               //read the LDR
    }
    digitalWrite(LED2, LOW);             //turn LED back off


    if ((millis() - fronttime) < carpasstime)         //was the car going fast enough?
    {      
      digitalWrite(FLASH, HIGH);                     //turn the FLASH on 
      delay(1000);                                   //give it  a second to go off properly
      digitalWrite(FLASH, LOW);                      //turn it back off
      digitalWrite(LED, LOW);                        //turn off LED  
      digitalWrite(LASER, LOW);                      //turn LASER off in sleep mode
      
      counter = 0;                                   //reset counter
      while (counter < 60 && BUTTON1 == 0)           //is the BUTTON pressed or is the counter past the alloted time?
      {          
        delay(700);
        digitalWrite(LED, HIGH);
        delay(700);
        digitalWrite(LED, LOW);  
        BUTTON1 = digitalRead(BUTTON);               //read BUTTON to see if its been pressed  
        counter++;                                   //add one to counter
      }
    }
  }
  delay(5);
}

//END OF FILE
  BUTTON1 = digitalRead(BUTTON);        // waits until BUTTON is pressed

By convention, all capital letter names are reserved for constants. Constants do not belong on the left of an equal sign.

Variable names should make it obvious what they help. Names like buttonState and buttonPin are much more obvious than BUTTON1 and BUTTON.

  while (BUTTON1 == 0)

The digitalRead() function returns HIGH or LOW. You would do well to use these names, rather than 0 or 1.

For that code to do anything, the user would have to be holding the switch down when the Arduino is reset. Is that a valid thing to require?

    while (val > trigger)
    {
      delay(5);                          //wait for light to retun 
      val = analogRead(0);               //read the LDR
    }

You absolutely do not want to be sitting around with your thumb up your ass for ANY amount of time. You need to INSTANTLY recognize when the transition occurs.

      digitalWrite(FLASH, HIGH);                     //turn the FLASH on 
      delay(1000);                                   //give it  a second to go off properly

What? That comment makes no sense.

  delay(5);

Again, why?

PaulS:

  BUTTON1 = digitalRead(BUTTON);        // waits until BUTTON is pressed

By convention, all capital letter names are reserved for constants. Constants do not belong on the left of an equal sign.
Variable names should make it obvious what they help. Names like buttonState and buttonPin are much more obvious than BUTTON1 and BUTTON.

this was a mistake. i used find and replace and it change all the buttons to BUTTONS. this obv changed all off them! but i let it slide

PaulS:

  while (BUTTON1 == 0)

The digitalRead() function returns HIGH or LOW. You would do well to use these names, rather than 0 or 1.

ok, noted

PaulS:
For that code to do anything, the user would have to be holding the switch down when the Arduino is reset. Is that a valid thing to require?

the laser is supposed to turn on to allow it to be set up by the user. ie position the mirror. they have to press the button after this to confirm its been done and then the code runs the calabration and the rest ect
dose that sound like its going to work?

PaulS:

    while (val > trigger)

{
      delay(5);                          //wait for light to retun
      val = analogRead(0);               //read the LDR
    }



You absolutely do not want to be sitting around with your thumb up your ass for ANY amount of time. You need to INSTANTLY recognize when the transition occurs.



delay(5);



Again, why?

fair enough
i just thought every 5 ms would be accurate enough
for some reason iv got a feeling that the chip will just go flat out and rag itself to death at the expense of unnecessary accuracy
shall i just remove it?

PaulS:

      digitalWrite(FLASH, HIGH);                     //turn the FLASH on 

delay(1000);                                   //give it  a second to go off properly



What? That comment makes no sense.

the flash is high power so its going to be run via relay. i want the relay to energize and close properly and then allow for the full flash. i dont want the relay bouncing back after a fraction off a second. 1000ms is probably slightly to long though

for some reason iv got a feeling that the chip will just go flat out and rag itself to death at the expense of unnecessary accuracy

The Arduino runs flat out, all the time. That's the way it is designed. The same holds true for any CPU. That the CPU on your PC accomplishes nothing useful a large percentage of the time is only because there is nothing for it to do. Using delay() or sleep() or any do-nothing-for-a-while does not give the processor a rest. It simply gives it something (useless) to do.

shall i just remove it?

Of course.

PaulS:

for some reason iv got a feeling that the chip will just go flat out and rag itself to death at the expense of unnecessary accuracy

The Arduino runs flat out, all the time. That's the way it is designed. The same holds true for any CPU. That the CPU on your PC accomplishes nothing useful a large percentage of the time is only because there is nothing for it to do. Using delay() or sleep() or any do-nothing-for-a-while does not give the processor a rest. It simply gives it something (useless) to do.

shall i just remove it?

Of course.

so why when i do calculations on my pc dose it get a lot hotter?

joshnosh:
so why when i do calculations on my pc dose it get a lot hotter?

Many modern microprocessors support dynamic frequency scaling, which allows the clock frequency and voltage to be lowered to reduce power consumption. The Arduino microcontrollers don’t support anything like that, and I wouldn’t expect to find any microcontroller that does.

[/quote]

Many modern microprocessors support dynamic frequency scaling, which allows the clock frequency and voltage to be lowered to reduce power consumption. The Arduino microcontrollers don't support anything like that, and I wouldn't expect to find any microcontroller that does.
[/quote]

they do indeed. my pc is overclocked though. all the scalling has been disable for reliablity. it still gets much hotter when its working on something...