Arduino stopwatch

Hello!

I'm trying to create a stopwatch, clocking for 10 seconds, {instructions}, reset, 10s, {instruction}, reset etc...

I've seen a bunch of example, but mine does not work.

Here is my code : (-> when the sonar detect a movement between 2.5cm and 25cm, it lights up a led, and then send information with Firmata, for only 10s. Then, the led goes out, and the program does not send anymore information, until he detects another movement. And I want the 10s to be reset every time a movement happens) :

 for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    delay(29); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.


    if (sonar[i - 1].ping_cm() < 25 && sonar[i - 1].ping_cm() > 2.5) {// SONAR J1
      SONARJ1 = true;
      SONARJ2 = false;
      while (SONARJ1 == true && SONARJ2 == false) {

        digitalWrite(ledJ1, HIGH);
        while (Firmata.available()) {
          Firmata.processInput();
        }
        unsigned long timer = millis();
        unsigned long timing = 0;
        Serial.println(timer - timing);
        Firmata.sendAnalog(analogPin, analogRead(analogPin));


        if (timer - timing >= 10000) {
          timing = timer;
          digitalWrite(ledJ1, LOW);
          SONARJ1 = false;
          SONARJ2 = false;
        }

      }
    }

Thank you!

Sorry, your code is incomplete. Error could be anywhere. Are you getting compile errors? If so, post the error messages together with your code. If it compiles but simply does not work, then its a logic error and with incomplete code, difficult to guess.

Here is my complete code:

#include <Firmata.h>


#include <TimerFreeTone.h>


#include <NewPing.h>

#define SONAR_NUM     2 // Number or sensors.
#define MAX_DISTANCE 200 // Max distance in cm.

byte analogPin = 0; // PIN pour les boutons (FIRMATA)

boolean SONARJ1 = false;
boolean SONARJ2 = false;



int ledJ1 = 7;
int ledJ2 = 6;
int ledJ1State = 0;
int ledJ2State = 0;


NewPing sonar[SONAR_NUM] = { // Sensor object array.
  NewPing(3, 2, MAX_DISTANCE),
  NewPing(11, 8, MAX_DISTANCE),
};



// FIRMATA
void analogWriteCallback(byte pin, int value)
{
  if (IS_PIN_PWM(pin)) {
    pinMode(PIN_TO_DIGITAL(pin), OUTPUT);
    analogWrite(PIN_TO_PWM(pin), value);
  }
}
//


void setup() {
  Serial.begin(9600);

  // Initialisation de Firmata
  Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  Firmata.begin(9600);
  //

  pinMode(ledJ1, OUTPUT);
  pinMode(ledJ2, OUTPUT);
}

void loop() {
  // Envoi des valeur à PROCESSING //
  while (Firmata.available()) {
    Firmata.processInput();
  }
  Firmata.sendAnalog(analogPin, analogRead(analogPin));
  if (analogPin >= TOTAL_ANALOG_PINS) {
    analogPin = 0;
  }
  //                                //









  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    delay(29); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.


    if (sonar[i - 1].ping_cm() < 25 && sonar[i - 1].ping_cm() > 2.5) {// SONAR J1
      SONARJ1 = true;
      SONARJ2 = false;
      while (SONARJ1 == true && SONARJ2 == false) {

        digitalWrite(ledJ1, HIGH);
        while (Firmata.available()) {
          Firmata.processInput();
        }

        Firmata.sendAnalog(analogPin, analogRead(analogPin));
        SONARJ1 = false;
        SONARJ2 = false;
      }

    }
  }
}

I guess it's a logic error.

I know why I don't use firmata. Too many unspecified variables. Where does analogPin get changed? Where does ANALOG_MESSAGE come from? Where does TOTAL_ANALOG_PINS come from? You declare ledJ1State and ledJ2State and nothing gets done with them. What is the purpose of TimerFreeTone.h?

I don't know enough about firmata to know what's going on behind the scenes. This I think, is one reason nobody uses it. If you can define more clearly what you're trying to accomplish, maybe there's some advice available for you.