Go Down

Topic: How to monitor a switch's state and do X if switch has remained in state B (Read 1 time) previous topic - next topic

Arrch

Code: [Select]
switch_timestamp = millis() + ( 6000 );

What this means (assuming timestamp isn't changed), is that after 6 seconds, the timestamp will will be equal to the current time, so that means overall, it will run again 6 + interval seconds. If you want it to run every minute, then you simply need to add 1 minute to what the current timestamp is:
Code: [Select]
switch_timestamp += 60000;

Dane


Code: [Select]
switch_timestamp = millis() + ( 6000 );

What this means (assuming timestamp isn't changed), is that after 6 seconds, the timestamp will will be equal to the current time, so that means overall, it will run again 6 + interval seconds. If you want it to run every minute, then you simply need to add 1 minute to what the current timestamp is:
Code: [Select]
switch_timestamp += 60000;


Hmmm - tried that - it didn't do the trick either. Thanks though!

Techone

I don't know this code may give you some idea about the state of a switch and help you for some insight...

Here a code. The code is a gear shifter. It got a Up and Down button. The switch go from HIGH to LOW. I hope it give you some insight.

Code: [Select]

/*
  size : 1774 byte
 
  Version 2.1
 
  file name : gearshifter.pde
 
  It simulate a gear shifter using 2 push-button switch.
  The circuit use a 7 segment display and 2 led for the
  simulated selenoid.
 
  Gear sequence:   Gear Number   A   B
                       1         1   1
                       2         0   1
                       3         0   0
                       4         1   0
 
  Here the parts you need :

  1 Commun Anode 7 segment Display
  2 Red LED
  2 Push Button Switch
  9 2N3904 NPN Transistor
  11 1 K resistor
  9 330 ohms resistor

  How it work :

  1. Init the gear at 1.
  2. Press Upshift Button to increase the gears
  3. Press Downshift Button to decrease the gears.
 
  Program by Serge J Desjardins aka techone / tech37

  Compile and Tested <-- Test on a breadboard

  Disclaimer:

  I am not responsible for any damages, losses, injuries or death.
  The user is responsabe for any damages, losses, injuries or death.
 
  And Murphy's Law is always there, bear that in mind.
   
*/
// Arduino Digital pins
const byte outpin[9] = {4,5,6,7,8,9,10,11,12};
const byte inpin[2] = {2,3};
// Selenoid and Display array pattern
boolean gearone[9] = {1,1,0,0,0,0,1,1,0};
boolean geartwo[9] = {0,1,1,0,1,1,0,1,1};
boolean gearthree[9] = {0,0,1,0,0,1,1,1,1};
boolean gearfour[9] = {1,0,1,1,0,0,1,1,0};

byte gear;

int upshift=1;
int downshift=1;
int oldupshift=1;
int olddownshift=1;

boolean state=0;

void setup()
{
  // init the digital pins
  for (int i=0; i<9; i++)
  {
  pinMode(outpin[i],OUTPUT);
  }
  pinMode(inpin[0],INPUT);
  pinMode(inpin[1],INPUT);
  // Put in gear one
  gear=1;
 
  for (int i=0;i<9;i++)
  {
    digitalWrite(outpin[i],gearone[i]);
  }
}

void loop()
{
  readswitch();
  while(state==0)
  {
    readswitch();     
  }
  // a switch as been pressed
  // Check for the upshift
  if ((upshift==0) && (state==1))
  { 
     gear++;
     if ((gear>=1) && (gear<=4))
     {
       switch(gear)
       {
         case 1: selectone();break;
         case 2: selecttwo();break;
         case 3: selectthree();break;
         case 4: selectfour();break;
       }
     }
     else gear=4;
   }
  // check for the downshift 
  if ((downshift==0) && (state==1))
  { 
     gear--;
     if ((gear>=1) && (gear<=4))
     {
       switch(gear)
       {
         case 1: selectone();break;
         case 2: selecttwo();break;
         case 3: selectthree();break;
         case 4: selectfour();break;
       }
     }
     else gear=1;
   }   
   
}

void selectone()
{
   for (int i=0;i<9;i++)
   {
     digitalWrite(outpin[i],gearone[i]);
   }
   state=0; 
}

void selecttwo()
{
   for (int i=0;i<9;i++)
   {
     digitalWrite(outpin[i],geartwo[i]);
   }
   state=0; 
}

void selectthree()
{
   for (int i=0;i<9;i++)
   {
     digitalWrite(outpin[i],gearthree[i]);
   }
   state=0; 
}

void selectfour()
{
   for (int i=0;i<9;i++)
   {
     digitalWrite(outpin[i],gearfour[i]);
   }
   state=0; 
}

/* The subroutine is a modify switch example in page
   51 of the book "Getting Started with Arduino"
   
   The switch is configure with a pull-up resistor,
   so it always a 1, when press, it is a 0.
*/   
void readswitch()
{
  upshift=digitalRead(inpin[0]);
  // check upshift transition
  if ((upshift==0) && (oldupshift==1))
  {
    state=1;
    delay(50);
  } 
  oldupshift=upshift;
  downshift=digitalRead(inpin[1]);
  // check downshift transition
  if ((downshift==0) && (olddownshift==1))
  {
    state=1;
    delay(50);
  } 
  olddownshift=downshift; 
}

Arrch


Hmmm - tried that - it didn't do the trick either. Thanks though!

I've done that in my code and it's worked, so it sounds like there is another issue with another part of your code, then.

Dane


so it sounds like there is another issue with another part of your code, then.


Maybe I put your snippet in the wrong place?
Code: [Select]
long switchPinsWaitTime = 30000; //30 seconds
Code: [Select]
  static unsigned long switch_timestamp1 = 0;

  if(value2 && switch_timestamp1 && (millis() - switch_timestamp1) >= switchPinsWaitTime)   
  {
    //    playTone(100, 1000);
    //    playTone(100, 3000);
    //    playTone(100, 1000);
    //    playTone(100, 3000);
    switch_timestamp1 += 60000;
  }

  else if (!switch_timestamp1 && value2)   
  {
    //The !switch_timestamp1 is to make sure we don't refresh the timer if the switch remained on.
    //The pin just changed to on, start the timer
    switch_timestamp1 = millis();
  }
  else if(!value2 && switch_timestamp1)   
  {
    //Switch went off, while timer was running, disable the timer
    switch_timestamp1 = 0;
  }

Go Up