Show Posts
Pages: 1 ... 20 21 [22] 23 24 ... 239
316  Using Arduino / Programming Questions / Re: Help on: June 24, 2014, 06:13:08 am
Try this

Code:
boolean GotFirstSensor= false, GotSecondSensor = false;
unsigned long SensorOne_timer = 0, SensorTwo_timer = 0;

void setup()
{
  Serial.begin(115200);
  attachInterrupt(0, SensorOneState, RISING); // digital pin 2
  attachInterrupt(1, SensorTwoState, RISING); // digital pin 3
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop()
{
  if(GotFirstSensor & GotSecondSensor) //both must be true for this condition to be true
  {
    if( (SensorOne_timer > SensorTwo_timer) && abs( SensorOne_timer - SensorTwo_timer) <= 10 /*millisSeconds */)
    {
      digitalWrite(12, HIGH);
      digitalWrite(13, LOW);
    }
    else if( (SensorOne_timer < SensorTwo_timer) && abs( SensorOne_timer - SensorTwo_timer) <= 10 /*millisSeconds */)
    {
      digitalWrite(12, LOW);
      digitalWrite(13, HIGH);
    }
    // LEDs off
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);

    ClearStates(); // clear the states for next use
  }

  if( (millis() - SensorOne_timer  > 2000) || (millis() - SensorTwo_timer > 2000) ) // hang timer, if nothing happens for 2 seconds clear the states
    ClearStates();
}

void ClearStates()
{
  GotFirstSensor= false;
  GotSecondSensor = false;
}

void SensorOneState()
{
  if(!GotFirstSensor)
    SensorOne_timer = millis(); //record the time the car reaches the first gate
  GotFirstSensor = true;
}

void SensorTwoState()
{
  if(!GotSecondSensor)
    SensorTwo_timer = millis(); //record the time the car reaches the second gate
  GotSecondSensor = true;
}
317  Development / Other Software Development / Re: playing with (out of) ranges to improve map() e.g. mapping to percentage on: June 23, 2014, 03:55:56 pm
Ok, that's understandable, and an error on my part. Decimal points don't do so well as int types.
318  Using Arduino / Programming Questions / Re: Help on: June 23, 2014, 03:48:11 pm
Yes, but the time still changes. I can make it so it only updates the time if it is not already active. Its just a few more lines of code.
319  Using Arduino / Programming Questions / Re: Help on: June 23, 2014, 02:50:50 pm
Once one of the states are active, it stays active, but the time changes.

However this looks at the time difference of the two state and not how many times the input is activated.
320  Using Arduino / Programming Questions / Re: Help on: June 23, 2014, 02:43:03 pm
Well, you have a 10 millisecond window to press the two buttons and get an output.

OR

You can increase this number (abs( SensorOne_timer - SensorTwo_timer) <= 10)to say 200 or more and it should make it easier to get a result. But HOW you want to simulate this is up to you. Buttons are cheap and easy to install, but they also bounce (see Debouncing section) which could be an issue.
321  Using Arduino / Programming Questions / Re: Help on: June 23, 2014, 02:29:41 pm
As soon as an LED goes on, a few milliseconds later it goes off. How long do you want it on and off?
322  Using Arduino / Programming Questions / Re: Help on: June 23, 2014, 02:24:21 pm
Look at reply #6, the code I gave didn't have void loop() in it, but the code before it did.
323  Using Arduino / Programming Questions / Re: Help on: June 23, 2014, 02:17:27 pm
@wildbill

I know, but I retraced my steps, found it and brought it home.
324  Using Arduino / Programming Questions / Re: Help on: June 23, 2014, 01:50:46 pm
How short, because you can change the code to this and it will make the LEDs come on and go off in a fraction of a second.

Edit: I made a mistake, I had abs( SensorOne_timer - SensorTwo_timer) > 10, instead of <=
Make sure you fix your code.

Code:
boolean GotFirstSensor= false, GotSecondSensor = false;
unsigned long SensorOne_timer = 0, SensorTwo_timer = 0;

void setup()
{
  Serial.begin(115200);
  attachInterrupt(0, SensorOneState, RISING); // digital pin 2
  attachInterrupt(1, SensorTwoState, RISING); // digital pin 3
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop()
{
  if(GotFirstSensor & GotSecondSensor) //both must be true for this condition to be true
  {
    if( (SensorOne_timer > SensorTwo_timer) && abs( SensorOne_timer - SensorTwo_timer) <= 10 /*millisSeconds */)
    {
      digitalWrite(12, HIGH);
      digitalWrite(13, LOW);
    }
    else if( (SensorOne_timer < SensorTwo_timer) && abs( SensorOne_timer - SensorTwo_timer) <= 10 /*millisSeconds */)
    {
      digitalWrite(12, LOW);
      digitalWrite(13, HIGH);
    }
    // LEDs off
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);

    ClearStates(); // clear the states for next use
  }
}

void ClearStates()
{
  GotFirstSensor= false;
  GotSecondSensor = false;
}

void SensorOneState()
{
  SensorOne_timer = millis(); //record the time the car reaches the first gate
  GotFirstSensor = true;
}

void SensorTwoState()
{
  SensorTwo_timer = millis(); //record the time the car reaches the second gate
  GotSecondSensor = true;
}
325  Using Arduino / Programming Questions / Re: Help on: June 23, 2014, 01:32:28 pm
Ah, it would help if the code was in the loop function. Sorry about that.

Code:
boolean GotFirstSensor= false, GotSecondSensor = false;
unsigned long SensorOne_timer = 0, SensorTwo_timer = 0;

void setup()
{
  Serial.begin(115200);
  attachInterrupt(0, SensorOneState, RISING); // digital pin 2
  attachInterrupt(1, SensorTwoState, RISING); // digital pin 3
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop()
{
  if(GotFirstSensor & GotSecondSensor) //both must be true for this condition to be true
  {
    if( (SensorOne_timer > SensorTwo_timer) && abs( SensorOne_timer - SensorTwo_timer) < 10 /*millisSeconds */)
    {
      digitalWrite(12, HIGH);
      digitalWrite(13, LOW);
    }
    else if( (SensorOne_timer < SensorTwo_timer) && abs( SensorOne_timer - SensorTwo_timer) < 10 /*millisSeconds */)
    {
      digitalWrite(12, LOW);
      digitalWrite(13, HIGH);
    }
    else /* now it does nothing if the time difference is greater than 10 */
    {
      digitalWrite(12, LOW);
      digitalWrite(13, LOW);
    }

    ClearStates(); // clear the states for next use
  }
}

void ClearStates()
{
  GotFirstSensor= false;
  GotSecondSensor = false;
}

void SensorOneState()
{
  SensorOne_timer = millis(); //record the time the car reaches the first gate
  GotFirstSensor = true;
}

void SensorTwoState()
{
  SensorTwo_timer = millis(); //record the time the car reaches the second gate
  GotSecondSensor = true;
}
326  Using Arduino / Programming Questions / Re: Help on: June 23, 2014, 01:22:45 pm
Did you combine the first code and the new one?

Try it now.
Code:
boolean GotFirstSensor= false, GotSecondSensor = false;
unsigned long SensorOne_timer = 0, SensorTwo_timer = 0;

void setup()
{
  Serial.begin(115200);
  attachInterrupt(0, SensorOneState, RISING); // digital pin 2
  attachInterrupt(1, SensorTwoState, RISING); // digital pin 3
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

if(GotFirstSensor & GotSecondSensor) //both must be true for this condition to be true
{
   if( (SensorOne_timer > SensorTwo_timer) && abs( SensorOne_timer - SensorTwo_timer) > 10 /*millisSeconds */)
   {
       digitalWrite(12, HIGH);
       digitalWrite(13, LOW);
   }
   else if( (SensorOne_timer < SensorTwo_timer) && abs( SensorOne_timer - SensorTwo_timer) > 10 /*millisSeconds */)
   {
       digitalWrite(12, LOW);
       digitalWrite(13, HIGH);
   }
   else /* now it does nothing if the time difference is greater than 10 */
   {
       digitalWrite(12, LOW);
       digitalWrite(13, LOW);
   }
   
   ClearStates(); // clear the states for next use
}

void ClearStates()
{
  GotFirstSensor= false;
  GotSecondSensor = false;
}

void SensorOneState()
{
  SensorOne_timer = millis(); //record the time the car reaches the first gate
  GotFirstSensor = true;
}

void SensorTwoState()
{
  SensorTwo_timer = millis(); //record the time the car reaches the second gate
  GotSecondSensor = true;
}
327  Using Arduino / Project Guidance / Re: I need some options for 12V battery supply on: June 23, 2014, 01:02:09 pm
I use this one,(6800mhA actually) but there are many others like this for less. 12V Super Battery

They also last for a VERY long time (charge wise).
328  Using Arduino / Project Guidance / Re: LCD menu's on: June 23, 2014, 12:42:05 pm
Code:
byte counter = 0;
byte maxMenus = 3; // 0, 1, 2

// In loop with rest of code
if(switchVar1 == 32) // you might want make it so the counter is only incremented if the button is pressed & released and not held.
{
   counter+1 < maxMenus?  counter++ : counter = 0;
}

switch(counter)
{
  case 0:
     // something
     break;
 
  case 1:
     // something
     break;

  case 2:
     // something
     break;
}
329  Using Arduino / Programming Questions / Re: Help on: June 23, 2014, 12:26:52 pm
Quote
But i have one last question just to prove that it works question: what does the code does when pin3 is activated and 2 is activited 3 milisecondes later?
Pin 12 goes HIGH, 13 goes LOW

And what does the code does when 3 is activated and 2 is activated 9 milliseconds later?
Same

And what does the code does when pin3 is activated and pin2 14 miliseconds later?
Nothing, the time difference is greater than 10 milliseconds

Is this homework or a school project?
330  Using Arduino / Project Guidance / Re: Counting Person entering a room on: June 23, 2014, 10:49:51 am
To increment and decrement a counter based on the number of people who entered or left will need a two sensor gate.

If sensor one is triggered first then sensor two, a person has entered.
if sensor two is triggered first then sensor one, a person has left.

You will have issues if two people are rude and try to enter and leave at the same time.
Pages: 1 ... 20 21 [22] 23 24 ... 239