Show Posts
Pages: [1] 2 3 ... 5
1  Using Arduino / Sensors / Capacitive sensor Vs better responce time on: January 22, 2014, 08:54:53 am
Hi.
The last few weeks i'm trying to build a reaction meter , and so far i tried to use capacitive sensors to track the presence of a hand to turn on or off the time counter.
The "system" works very well but i belive that the response time of this kind of sensor is not accurate enough.
Could you please make any suggestions on what is the best sensor for my case  of use ?
The only "rule" that this sensor must follow is to avoid any physical touch with the human body and of course better responce time than the capacitive sensor

Thank you in advance
2  Using Arduino / Programming Questions / Re: Capacitive Sensor Responce time on: January 20, 2014, 06:43:16 pm
Hello again Paul.. smiley

In my application :
1) Latency +- 1msec is accepted
2) I dont want to use traditional ON-OFF switches .

I want to sense the presence of a hand and when it moves to activate the counter.
Apart the Capacitive Sensor there is any other sensor that can fulfil the above rules ?

Any suggestions are more than welcome.
Thank you.
3  Using Arduino / Programming Questions / Re: Capacitive Sensor Responce time on: January 20, 2014, 01:06:52 pm
Thank you for your fast reply.
Already doing this but during the tests i made i noticed that is not accurate...
let me explain a bit more what i mean...
Lets assume that the Capacitive sensor No1 senses values between min=5 and max=150.
And my threshold is 50...so every time the sensor values are  below 50 i set that is not pressed(touched) and when above 50 is pressed(touched).
The problem is that it takes some time when i release the sensor to go from 150 which is the max sensing value to 50 where my threshold is.
There is any way to calculate this time ?
 
4  Using Arduino / Programming Questions / Capacitive Sensor Responce time on: January 20, 2014, 12:51:49 pm
Hi.
I'm using Capacitive library to sense two different aluminium foil pieces to act as a on-off switch.
So far working fine and can correctly sense the two 'switches".
The problem is that they will be used in a time sensitive aplication and i want to know the responce time of each sensor.
Any idea how can i calculate the responce time of each sensor ?

Thank you in advance.
5  Using Arduino / Programming Questions / Re: Count Time in 2 different states on: January 13, 2014, 09:14:52 am
Quote
Any idea WHY ?
Nope. I've never used capacitive sensors, nor had any desire to do so.
  smiley

And one last question..
do you think that this aproach to calculate the latency introduced by the sensor is correct ?

Code:
case 1:
         
          SensorStartMillis = millis();
          BaseState = ReadBaseSensor();
          SensorEndMillis = millis();
          SensorLatency = SensorEndMillis - SensorStartMillis;
          Serial.print(" BASE SENSOR LATENCY  ");
          Serial.println(SensorLatency);
6  Using Arduino / Programming Questions / Re: Count Time in 2 different states on: January 13, 2014, 08:49:16 am
I'm trying to help you debug your code, but you're really not making it easy.

Sorry...i'm doing the best i can do  smiley-red smiley-red smiley-red

The function used to look like this:
Code:
int ReadBaseSensor(){
  BaseSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  long total1 =  BaseSensor.capacitiveSensor(10);
  if (total1 > 20) { return 1; } else { return 0; }
}

Quote
Done.
Tested and the function called .
So, you made some changes.

Yes ..the new function i use in order to make sure if is called or not is the following

Code:
int ReadBaseSensor(){
  BaseSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  long total1 =  BaseSensor.capacitiveSensor(10);
  if (total1 > 20)
  {
    Serial.print("------------  BaseState ------  1  ---------");
    return 1;
  }
  else
  {
    Serial.print("------------  BaseState -----  0  ----------");
    return 0;
  }
}

Feel free to volunteer information that you think might be useful.

Hmmmm...
i've made some changes and seems that when i put
Code:
BaseSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
 long total1 =  BaseSensor.capacitiveSensor(10);
in void setup()
and changes function to
Code:
int ReadBaseSensor(){
  long total1 =  BaseSensor.capacitiveSensor(10);
  if (total1 > 20)
  {
    Serial.print("------------  BaseState ------  1  ---------");
    return 1;
  }
  else
  {
    Serial.print("------------  BaseState -----  0  ----------");
    return 0;
  }
}

the function is working fine at the begining of the loopp ABD inside the case 1.  smiley-confuse
Any idea WHY ?
7  Using Arduino / Programming Questions / Re: Count Time in 2 different states on: January 13, 2014, 07:16:39 am
Quote
I guess that this mean that the function is not called or returns wrong results.
You could verify that the function is, or is not called, by putting a Serial.print() statement in the function.

Done.
Tested and the function called .

You could verify that the function returns the correct, or wrong, results, by setting BaseState to some value that the function would not possibly return. If it retains that value after the function call, you have one problem. If it changes, you have another problem.

The function returns wrong results.
I've set BaseState=2; and the function always return zero
8  Using Arduino / Programming Questions / Re: Count Time in 2 different states on: January 13, 2014, 06:26:52 am
Failing to define what "doesn't work" means.

Does the code fail to compile? Is the function not called? Does it return incorrect results? I the return value not persisted correctly?

Compiles correctly.
When i place the BaseState = ReadBaseSensor();  at the beginning of the loop and BaseSensor is pressed

Code:
void loop()
{
       BaseState = ReadBaseSensor();
       EndState  = ReadEndSensor();

the code working as expected and i'm taking the following readings in Serial
Code:
  CALIBRATE 
------------  BaseState ---------------1
  BASE PRESSED 
------------  BaseState ---------------0
  BASE RELEASED 
 

But when i place it inside the case1

Code:
case 1:
         
          BaseState = ReadBaseSensor();
          Serial.print("------------  BaseState ---------------");
          Serial.println(BaseState);
          if(BaseState == 1)
         {
        ..........................etc ....................

in Serial i'm getting

Code:
CALIBRATE 
------------  BaseState ---------------0
  BASE RELEASE

and the code doesnt run as expected.

I guess that this mean that the function is not called or returns wrong results.
9  Using Arduino / Programming Questions / Re: Count Time in 2 different states on: January 12, 2014, 06:08:20 pm
So, you are trying to measure reaction time (how long between when the LED goes on and when the reactee contacts the capacitive sensor.

Then, you are measuring how long it takes the user to solve some undefined puzzle and press the second capacitive sensor.

The bit about solving a puzzle is completely new information and is EXACTLY why I suggested that you needed to write the user guide first.

Code:
void loop()
{
  BaseState = ReadBaseSensor();
  EndState  = ReadEndSensor();
No. Unless the LED is lit, there is no reason to read the base sensor. The LED won't be lit until the referee has pressed the start switch. Unless the base sensor has been contacted, and the user is solving the puzzle, there is no need to read the end sensor.

i know...
tried to avoid that but when i put this part of the code inside the case 1 (look in the code below) right before the... if(BaseState == 1) ... doesn't work smiley-red
What i'm doing wrong ?

Code:
case 1:
         
          BaseState = ReadBaseSensor();  <<------here----
          if(BaseState == 1)
          {
             Serial.println("  BASE PRESSED  ");             
             
          }
..........etc.....

Other than that, I think you are now on the right track.

 smiley Thank you
10  Using Arduino / Programming Questions / Re: Count Time in 2 different states on: January 12, 2014, 04:27:26 pm
In a situation like this, I find it helpful to write the user manual first. Explain exactly what the user is supposed to do, in the order that the user is expected to do things.

I'll start from this.

USER MANUAL.  smiley-lol
1) The referee calibrates the system (sensitivity of sensors using pots)
2)The player put his hand over the 1st capacitive sensor ( he is ready to play )
3) The referee hit the start button (game begin)
4) After a random amount of time a RedLight goes ON (At this point 1st Timing begin)
5) Player remove his hand over the 1st capacitive sensor and starts solving the puzzle(here we stop 1st counting to measure how fast the player reacted AND also starting the 2nd counting)
6) Player put his hand oner the 2nd Capacitive sensor when puzzle solved(Here we stop 2nd counting to measure in how much time the player solved the puzzle )
7)Display the results
8 ) Referee hits the start button and GAME starts again

You may need to add, or remove state, and renumber accordingly.

Take a look on what i've done

Code:
#include <CapacitiveSensor.h>

const int RedLight         =  6;    // red stop LED
const int BlueLight         =  10;    // red stop LED
const int StartSwitch      =  13;    // reaction timer button

const int PotBase = A0;       // analog IN Sensitinity Base
int PotBaseVal = 0;           // variable to store the value read from PotBase A3
int BaseSense = 0;            // value to adjust Base sensitivity
int BaseState = 0;           //this variable controls if BaseSensor is ON=1 or OFF=0

CapacitiveSensor   BaseSensor = CapacitiveSensor(7,8);        // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil

const int PotEnd = A1;       // analog IN Sensitinity End
int PotEndVal = 0;           // variable to store the value read from PotEnd A1
int EndSense = 0;            // value to adjust End sensitivity
int EndState = 0;           //this variable controls if EndSensor is ON=1 or OFF=0
CapacitiveSensor   EndSensor = CapacitiveSensor(7,9);        // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil

int GameState = 0; // state will be 0 = idle; 1 = started; 2 = keeping user guessing; 3 = timing; 4 = reacted; 5 = done
long timerStartMillis;       // the time when the timer started
long timerMidMillis;         // the time when in Mid state
long timerEndMillis;         // the time when the timer finished
long difference2;
long difference;

void setup()
{
  pinMode(RedLight, OUTPUT);       // red LED is an output
  pinMode(BlueLight, OUTPUT);       // Blue LED is an output
  pinMode(StartSwitch, INPUT);     // button is an input
 
//  BaseSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
//  EndSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);
  long start = millis();
//  long total1 =  BaseSensor.capacitiveSensor(10);
//  long total2 =  EndSensor.capacitiveSensor(10);
     
  randomSeed(analogRead(2));     // use unconnected pin to seed random sequence
  Serial.begin(9600);
}

 

void loop()
{
  BaseState = ReadBaseSensor();
  EndState  = ReadEndSensor();
 
   switch(GameState)
   {
     
      case 0: //  CALIBRATE....This will run OLNLY at STARTUP
     
          if(digitalRead(StartSwitch) == HIGH) // Start Switch Pressed and GAME BEGINS !!!
          { 
             delay(2000);
             digitalWrite(BlueLight,LOW);
             digitalWrite(RedLight,HIGH);
             timerStartMillis = millis();           
             GameState = 1; // Move to the next state
          }
          else
          {
             Serial.println("  CALIBRATE  ");
             digitalWrite(BlueLight,HIGH);
          }
         
      break;
     
      case 1:
             
          if(BaseState == 1)
          {
             Serial.println("  BASE PRESSED  ");             
             
          }
          else
          {
           
            Serial.println("  BASE RELEASED  ");
            timerMidMillis = millis(); // get the current time
            difference = timerMidMillis - timerStartMillis; // time taken is difference between times
            Serial.print("  difference  ");
            Serial.println(difference);
            GameState = 2; // Move to the next state
          }
         
        break;
       
        case 2: // idle CALIBRATE
       
          if(EndState == 1)
          {
             Serial.println("  END PRESSED  "); 
             timerEndMillis = millis(); // get the current time   
             difference2 = timerEndMillis - timerMidMillis; // time taken is difference between times
             digitalWrite(RedLight,LOW);
             GameState = 3; // Move to the next state           
             
          }
          else
          {
            Serial.println("  END RELEASED  ");
          }
         
         break;
         
         case 3: // DISPLAY RESULTS
         
          if(digitalRead(StartSwitch) == HIGH)
          {
             GameState = 1; // Move to case 1
          }
          else
          {
             Serial.println("  DISPLAY RESULTS  ");
             Serial.print("  difference  ");
             Serial.print(difference);
             Serial.print("  difference2  ");
             Serial.println(difference2); 
          }
         
         break;
   }
}

int ReadBaseSensor(){
  BaseSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  long total1 =  BaseSensor.capacitiveSensor(10);
  if (total1 > 20) { return 1; } else { return 0; }
}

int ReadEndSensor(){
  EndSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  long total2 =  EndSensor.capacitiveSensor(10);
  if (total2 > 20) { return 1; } else { return 0;}
}

I tryied to avoid running all this in every loop

Code:
   StartState = digitalRead(StartSwitch);
    long start = millis();
    
    long total1 =  BaseSensor.capacitiveSensor(10);
    if (total1 > 20){ BaseState =1; } else { BaseState =0; }
    
    long total2 =  EndSensor.capacitiveSensor(10);
    if (total2 > 20){ EndState =1; } else { EndState =0; }

so i created two functions to read the sensors individually
ReadBaseSensor()   and  ReadEndSensor()
 
but i place them in the beginning of the loop in order to work ...which means that will running in every loop...right ?
I tried to put them inside the switch statement to avoid the above but is not working
Code:
case 1:
          BaseState = ReadBaseSensor();  /// <<<<<<--------------Here NOT WORKING -------------    
          if(BaseState == 1)
          {
             Serial.println("  BASE PRESSED  ");            
            
          }
          else
          {
            Serial.println("  BASE RELEASED  ");
            GameState = 2; // Move to the next state
          }
          
        break;

How can i avoid running this parts of code in every loop ?
11  Using Arduino / Programming Questions / Re: Count Time in 2 different states on: January 12, 2014, 08:15:21 am
Thank you Paul for your valuable help..
without your help i could not make any progress ....
especially thank you for pointing me the right way i should think and work

I think you need to re-write your code from scratch.

Done

If the start switch has not been pressed, is there any reason to read the two capacitive sensors?

Yes there is.
I need to test the sensors and to adjust the threshold with a pot individually for each sensor (to be done)

If the start switch has been pressed, is there any reason to read it again?

Yes

You need a state machine. You have only a few states - not yet started, started, timing, reacted, and done. That is, the user has not yet started the process, the user has started the process (so LED is off and the random delay is happening), the LED is on and timing has begun, and, the user has reacted to the LED coming on. Certain state changes happen because the user did something. Some happen at specific times (relative to other events)

In the reacted state, you calculate how long is took for the user to react, and you transition to the done state. In the done state, you display the results,.

I guess that the new code complying with all the above...
Right ?

Here is the NEWly written code that is working.....i guess  smiley-red

Code:

#include <CapacitiveSensor.h>

const int RedLight         =  6;    // red stop LED
const int BlueLight         =  10;    // red stop LED
const int StartSwitch      =  13;    // reaction timer button
int StartState = 0;         // variable for reading the pushbutton status

int BaseState = 0;           //this variable controls if BaseSensor is ON=1 or OFF=0
CapacitiveSensor   BaseSensor = CapacitiveSensor(7,8);        // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
int EndState = 0;           //this variable controls if EndSensor is ON=1 or OFF=0
CapacitiveSensor   EndSensor = CapacitiveSensor(7,9);        // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
long randomDelayTime;   
long timerStartMillis;       // the time when the timer started
long timerMidMillis;         // the time when in Mid state
long timerEndMillis;         // the time when the timer finished
long difference2;
long difference;
boolean PrepareState = true; // in introduction mode

boolean GameStart = false;
boolean GameMid = false;
boolean GameEnd = false;

void setup() {
 
 
  pinMode(RedLight, OUTPUT);       // red LED is an output
  pinMode(BlueLight, OUTPUT);       // Blue LED is an output
  pinMode(StartSwitch, INPUT);     // button is an input
 
   BaseSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   EndSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);
   long start = millis();
   long total1 =  BaseSensor.capacitiveSensor(10);
   long total2 =  EndSensor.capacitiveSensor(10);
 
   pinMode(StartSwitch, INPUT);     // button is an input
 
   randomSeed(analogRead(2));     // use unconnected pin to seed random sequence
   Serial.begin(9600);
}



void loop()
{
    StartState = digitalRead(StartSwitch);
    long start = millis();
   
    long total1 =  BaseSensor.capacitiveSensor(10);
    if (total1 > 20){ BaseState =1; } else { BaseState =0; }
   
    long total2 =  EndSensor.capacitiveSensor(10);
    if (total2 > 20){ EndState =1; } else { EndState =0; }
 
 
  if(PrepareState) // Prepare State
  {
    digitalWrite(BlueLight, HIGH); // PREPARE STATE = BLUE LIGHT ON
   
    if(BaseState == 1) // ===================================================== TEST BASE LIGHT
    {
      Serial.println(" Prapare State and  Base Pressed  ");
      digitalWrite(RedLight, HIGH);
    }
    else
    {
     Serial.println("  Prepare  ");
     digitalWrite(RedLight, LOW);
    }
    if(EndState == 1) // =======================================================  TEST END LIGHT
    {
      Serial.println(" Prapare State and  END Pressed  ");
      digitalWrite(RedLight, HIGH);
    }
    else
    {
     Serial.println("  Prepare  ");
     digitalWrite(RedLight, LOW);
    }
   
    if(BaseState ==1 && StartState == 1)// StartState Pressed Prepare State OFF
    {
       //Serial.println(" Prepare State and Start State Pressed  ");
       digitalWrite(BlueLight, LOW); // NO PREPARE STATE = BLUE LIGHT ON
       digitalWrite(RedLight, LOW);
       //randomDelayTime = random(10000);  // this is the random amount to be used 0-10 seconds
       //delay(randomDelayTime);
       delay(2000);
       timerStartMillis = millis(); // get the current time             
       PrepareState=false ;
       GameStart=true;
    }
     
  }
  else //No Prepare State
  {
                 
       if(GameStart == true) /// Lets Start count TIMES
       {
               
          if (BaseState == 1) // ===============================================================   Base Released
          {
           
            digitalWrite(RedLight,HIGH);
            Serial.print("  TimerStarts ");
            Serial.println(timerStartMillis);
           
          }
         else
        {
            timerMidMillis = millis(); // get the current time
            difference = timerMidMillis - timerStartMillis; // time taken is difference between times
            Serial.print("  difference  ");
            Serial.println(difference);
            GameStart=false;
            GameMid=true;
        }
       }
     
       if (GameStart == false && GameMid == true)
       {
         if(EndState == 1) // =============================================  End Pressed Prepare State ON
          {     
             timerEndMillis = millis(); // get the current time   
             difference2 = timerEndMillis - timerMidMillis; // time taken is difference between times
             digitalWrite(RedLight,LOW);
             GameMid=false;
             GameEnd=true;
          }       
       }     
       
       if (GameStart == false && GameMid == false && GameEnd == true)
       {
            if (StartState==1) // Start Pressed...Lets Start Over
             {
                 PrepareState = true;
                 GameStart=false;
                 GameMid=false;
                 GameEnd=false;
             }
            else
            {
                Serial.print("  difference  ");
                Serial.print(difference);
                Serial.print("  difference2  ");
                Serial.println(difference2);
             
            }
         }
  }     
}

The questions now are:
1)The way that i've structure my code is it counting correctly the differences of each state ?
2)There is any way to calculate the latency introduced by the Capacitive Sensors ?
12  Using Arduino / Programming Questions / Count Time in 2 different states on: January 11, 2014, 07:41:09 am
I'm trying to create a simple Game.
The idea is to count in millis
1) how fast the player reacts(Releases a switch when a RedLight turns ON
2)how fast he can solve the puzzle (when puzzle solved he pussh a different Switch)
Following PaulS advice...i rebuilded the code made here http://forum.arduino.cc/index.php?topic=208757.0 to the following

Code:

#include <CapacitiveSensor.h>

const int RedLight         =  6;    // red stop LED
const int BlueLight         =  10;    // red stop LED
const int StartSwitch      =  13;    // reaction timer button
int StartState = 0;         // variable for reading the pushbutton status

int BaseState = 0;           //this variable controls if BaseSensor is ON=1 or OFF=0
CapacitiveSensor   BaseSensor = CapacitiveSensor(7,8);        // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
int EndState = 0;           //this variable controls if EndSensor is ON=1 or OFF=0
CapacitiveSensor   EndSensor = CapacitiveSensor(7,9);        // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
long randomDelayTime;   
long timerStartMillis;       // the time when the timer started
long timerMidMillis;         // the time when in Mid state
long timerEndMillis;         // the time when the timer finished
long difference2;
long difference;
boolean PrepareState = true; // in introduction mode

void setup() {
 
 
  pinMode(RedLight, OUTPUT);       // red LED is an output
  pinMode(BlueLight, OUTPUT);       // Blue LED is an output
  pinMode(StartSwitch, INPUT);     // button is an input
 
   BaseSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   EndSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);
   long start = millis();
   long total1 =  BaseSensor.capacitiveSensor(10);
   long total2 =  EndSensor.capacitiveSensor(10);
 
   pinMode(StartSwitch, INPUT);     // button is an input
 
   randomSeed(analogRead(2));     // use unconnected pin to seed random sequence
   Serial.begin(9600);
}



void loop()
{
    StartState = digitalRead(StartSwitch);
    long start = millis();
    long total1 =  BaseSensor.capacitiveSensor(10);
    long total2 =  EndSensor.capacitiveSensor(10);
   
       
    if (total1 > 20){ BaseState =1; } else { BaseState =0; }
     
    if (total2 > 20){ EndState =1; } else { EndState =0; }
 
 
  if(PrepareState) // Prepare State
  {
    digitalWrite(BlueLight, HIGH); // PREPARE STATE = BLUE LIGHT ON
   
    if(BaseState == 1) // ===================================================== TEST BASE LIGHT
    {
      Serial.println(" Prapare State and  Base Pressed  ");
      digitalWrite(RedLight, HIGH);
    }
    else
    {
     Serial.println("  Prepare  ");
     digitalWrite(RedLight, LOW);
    }
    if(EndState == 1) // =======================================================  TEST END LIGHT
    {
      Serial.println(" Prapare State and  END Pressed  ");
      digitalWrite(RedLight, HIGH);
    }
    else
    {
     Serial.println("  Prepare  ");
     digitalWrite(RedLight, LOW);
    }
   
    if(BaseState ==1 && StartState == 1)// StartState Pressed Prepare State OFF
    {
       Serial.println(" Prepare State and Start State Pressed  ");
       digitalWrite(BlueLight, LOW); // NO PREPARE STATE = BLUE LIGHT ON
       digitalWrite(RedLight, LOW);
       //randomDelayTime = random(10000);  // this is the random amount to be used 0-10 seconds
       //delay(randomDelayTime);
       delay(2000);
       timerStartMillis = millis(); // get the current time
       PrepareState=false ;
    }
     
  }
  else //No Prepare State
  {
      Serial.print(" TIMER START ");
      Serial.println(timerStartMillis);
      digitalWrite(RedLight,HIGH);
     
      if (BaseState==0) // ===============================================================   Base Released
      {
        timerMidMillis = millis(); // get the current time
        difference = timerMidMillis - timerStartMillis; // time taken is difference between times
        Serial.print(" TIMER MID ");
        Serial.println(timerMidMillis);
                 
        while(EndState == 1) // =============================================  End Pressed Prepare State ON
        {     
            timerEndMillis = millis(); // get the current time   
            difference2 = timerEndMillis - timerMidMillis; // time taken is difference between times
            Serial.print(" TIMER END ");
            Serial.println(timerEndMillis);
            digitalWrite(RedLight,LOW);
           
        }       
          //  Serial.print("  difference  ");
          //  Serial.print(difference);
         //   Serial.print("  difference2  ");
          //  Serial.println(difference2);
         
          if (StartState==1)
          {
             PrepareState = true;
          }
      }
  } // No Prepare State
}

The problem is at the last stages of the code..
Take a look at the values i'm taking in serial

 
Code:
TIMER START 4574   <<<<<---------------------timerStartMillis = millis();
 TIMER START 4574
 TIMER START 4574
 TIMER START 4574
 TIMER START 4574
 TIMER START 4574
 TIMER MID 4803
 TIMER START 4574
 TIMER MID 4841 <<---------------------When BaseState ==0 (Switch released)
 TIMER START 4574
 TIMER START 4574
 TIMER MID 4897
 TIMER START 4574
 TIMER MID 4935
 TIMER START 4574
 TIMER MID 4973
 TIMER START 4574
 TIMER MID 5010
 TIMER START 4574
 TIMER MID 5048
 TIMER START 4574
 TIMER MID 5085
 TIMER END 5103 << -----------------------------When EndState ==1 (End switch pressed)
 TIMER END 5121
 TIMER END 5138
 TIMER END 5155
 TIMER END 5174
 TIMER END 5191
 TIMER END 5209
 TIMER END 5226
 TIMER END 5244
 TIMER END 5262
 TIMER END 5279
 TIMER END 5297
 TIMER END 5315
 TIMER END 5332
 TIMER END 5350
 TIMER END 5368
 TIMER END 5386
 TIMER END 5403
 TIMER END 5421

although i'm taking the desired values  the loop continues for ever so i cant calculate the differences i want to count
Code:
  Serial.print("  difference  ");
  Serial.print(difference);
  Serial.print("  difference2  ");
  Serial.println(difference2);

So my questions are :
1) What did i have to do in order to display the two values difference and difference2 stable?
2) Is it possible when all values displayed correctly when i  StartState==1 to start over the Game ?

PS1. Later , I'm planning to use an LCD screen in order to display results

PS2. I'm struggling several days on this...with no luck ..  smiley-red

Thank you in advance.
13  Using Arduino / Programming Questions / Re: Game Reaction tester and two capacitive sensors on: January 10, 2014, 12:28:32 pm
Hi Paul...
Seems that the last days i took several lessons but no one of them lead me to the right results..  smiley-red smiley-cry
I really need your help.
14  Using Arduino / Programming Questions / Re: Game Reaction tester and two capacitive sensors on: January 08, 2014, 10:38:40 am
Code:
        while (BaseState !=1 ) {} // wait until the button is released
This will never end, if it starts, since nothing in the (empty) body ever changes BaseState.

Of course, the while loop will never be executed, since it is in a block that only executes if BaseState IS 1.

Hmmm..i guess this a remaining part of code during several modifications  smiley-red

What does difference2 track? How is that different from what difference tracks?


difference is tracking the time in millis starting right after RedLight goes on untill the time that the player releases the baseswitch BaseState == 0 ..actually i'm counting his reaction time

difference2 i want to track the time starting at BaseState ==0 untill EndState ==1. when he finifhes the game

Sorry for my poor English ...i'll try to explain better in the below "diagram"

RedLight -------------------------------------- BaseState==0 ---------------------------------EndState==1
Light goes on ---------------------------- Player releases BaseState --------------------- player completes the game
timerStartMillis = millis();------------------- timerMidMillis = millis(); ----------------------timerEndMillis = millis();
<--difference = timerMidMillis - timerStartMillis;---->>      <<<------difference2 = timerEndMillis - timerMidMillis; -->>

I hope it's more clear now
Once again thank you for your time ...
15  Using Arduino / Programming Questions / Re: Game Reaction tester and two capacitive sensors on: January 08, 2014, 09:44:36 am
You should consider unplugging the LCD and putting it away. Concentrate on getting the logic of the program correct. Use Serial.print() statements to track where in the code you are.

Done

When the program flow is correct, then you can add the LCD back in and print data to it.

The code flows correctly untill the last state where i want to count the difference2 time (starting at BaseState == 0 untill EndState==1).
Whatever i try EndState NEVER goes to 1.
What i m doing wrong ?

Your code is unnecessarily complex. You have several states - timing not started, timing in progress, timing done. There is a limited amount of work to do in each state, and only one way to get from one state to another.
I know...but is the best i can do  smiley-red

Code:
/*
 Arduino Reaction Timer using an RGB LCD 16x2 Character Display
   Derek Molloy, DCU, Ireland.
   Using LCD library from:
   http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
#include <CapacitiveSensor.h>
#include <LiquidCrystal.h>  // include LCD library

// Set up the display with 4 bits - R/W tied to GND
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // RS, E, D4-D7

int RedLight         =  6;    // red stop LED
int BlueLight         =  10;    // red stop LED
int StartSwitch      =  1;    // reaction timer button

const int PotBase = A0;       // analog IN Sensitinity Base
int PotBaseVal = 0;           // variable to store the value read from PotBase A3
int BaseSense = 0;            // value to adjust Base sensitivity
int BaseState = 0;           //this variable controls if BaseSensor is ON=1 or OFF=0
CapacitiveSensor   BaseSensor = CapacitiveSensor(7,8);        // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil

const int PotEnd = A1;       // analog IN Sensitinity End
int PotEndVal = 0;           // variable to store the value read from PotEnd A1
int EndSense = 0;            // value to adjust End sensitivity
int EndState = 0;           //this variable controls if EndSensor is ON=1 or OFF=0
CapacitiveSensor   EndSensor = CapacitiveSensor(7,9);        // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil

// States of execution
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
long randomDelayTime;        // holds the random time amount
boolean prepareState = true; // in introduction mode
boolean isTiming = false;    // timing the press state
long timerStartMillis;       // the time when the timer started
long timerMidMillis;         // the time when in Mid state
long timerEndMillis;         // the time when the timer finished
long difference2;
long difference;


// Setup function - called only once
void setup() {
  pinMode(RedLight, OUTPUT);       // red LED is an output
  pinMode(BlueLight, OUTPUT);       // Blue LED is an output
  
   BaseSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   EndSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);
 
  randomSeed(analogRead(2));     // use unconnected pin to seed random sequence
  Serial.begin(9600);
}

void loop()
{  
    long start = millis();
    long total1 =  BaseSensor.capacitiveSensor(10);
    long total2 =  EndSensor.capacitiveSensor(10);
    buttonState = digitalRead(StartSwitch);
        
    if (total1 > 20){ BaseState =1; } else { BaseState =0; }
    if (total2 > 20){ EndState =1; } else { EndState =0; }
  
    if(prepareState)// prepare state - give out the instruction to press button
    {                
      Serial.println("  Prepare State  ");
      
      if (BaseState ==1 && buttonState != lastButtonState)  // if the button is pressed
      {
        digitalWrite(RedLight, LOW);
        Serial.println("  READY  ");
        randomDelayTime = random(10000);  // this is the random amount to be used 0-10 seconds
        while (BaseState !=1 ) {} // wait until the button is released
        prepareState = false;       // finished prepare state - lets move on
      }
    }
    else // not in prepare state
    {
       if (!isTiming)                // the timer isn't running, so we are pausing for random amount
       {
          delay(randomDelayTime);      // delay for the random amount
          digitalWrite(RedLight, HIGH);  // when finished - set red LED high
          Serial.println("  --- GO ---  ");
          isTiming = true;             // now we are ready to start timing reactions
          timerStartMillis = millis(); // get the current time
       }
       else // now we are timing person's reaction
       {
          if (BaseState == 0 && EndState==0)  // when they release the button
          {
             timerMidMillis = millis(); // get the current time
             difference = timerMidMillis - timerStartMillis; // time taken is difference between times            
             digitalWrite(RedLight, LOW); // turn off the red led
                                    
             if (difference < 10 )            // If the difference is 0 they held the button - or are supermen!
             {
                 Serial.println("  -- TOO SOON --  ");
             }
             else
             {
                  Serial.print("Reaction Time : ");
                  Serial.print(difference);
                  
                 if (EndState ==1)  // when they hit end button
                 {  
                     timerEndMillis = millis(); // get the current time  
                     difference2 = timerEndMillis - timerMidMillis; // time taken is difference between times
                                
                     Serial.println("  EndSensor PRESSED  ");
                 }  
             }
              delay(5000);                    // leave the message on the screen for 5 seconds
              isTiming = false;               // ready to start timing again
              prepareState = true;            // ready to start all over      
          }
       }
    }
}
Pages: [1] 2 3 ... 5