elapsedMillis help

Hello anyone !
I have made a program using two libraries plcLib.h and elapsedMillis.h . I am wsing elapsedMillis to measure the time thet an input is turned ON and this time is recorded as an variable . This variabel is used to change two timers TON and TOFF . This is not using corect and i dont understand why ??? Anyone help me please ???

#include <plcLib.h>
#include <elapsedMillis.h>

const int switchPin = A3;

unsigned long TIMER0 = 0;  // Variable to hold elapsed time for Timer 0
unsigned long TIMER1 = 0;  // Variable to hold elapsed time for Timer 0
unsigned long TIMER3 = 0;

unsigned int AUX0;        // Latch output variable
unsigned int AUX1;        // Latch reset variable
unsigned int AUX2;
unsigned int AUX3;
unsigned int AUX4;

long val;

void setup() {
  Serial.begin(9600);
  setupPLC();  // Setup inputs and outputs
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH); // turn on pull-up resistor
Serial.begin(9600);
Serial.println("Press and release switch" );
}

void loop() {
  
  val = pulseIn(switchPin, LOW , 5000);
if( val != 0) // timeout returns 0
{
Serial.print("switch pressed for ");
Serial.print(val);
Serial.println(" microseconds");
  }
  AUX1 = in(X2);          // AUX1 (Reset) controlled by input X1
  in(X0);
  latch(AUX3,X1);
  
  in(AUX1);                 // Read switch connected to Input 0 (Set input - Input 0)
  latch(AUX0,AUX2);	  // Latch command, Output = AUX0, Reset = AUX1 (Input 1)

  in(AUX0);
  timerOn(TIMER0,val*2);
  out(AUX2);
  
  in(AUX2);
  timerOff(TIMER1, 800);
  outNot(AUX4);
  
  in(AUX3);
  andBit(AUX4);
  out(Y0);

}

Moderator edit: illegible yellow removed
Moderator edit: Code posted to save hassle of downloading.

kontrolli_me_timera.ino (1.22 KB)

To recreate the problem people need to know where to get those
libraries and which version each is...

Where does the code use "elapsedMillis". It doesn't seem to use this library at all.
It merely includes it.

Also your code structure looks like there is something odd going on

The button code purely causes text to be printed on the Serial output, it doesnt affect the flow of the rest of the loop() function.

val = pulseIn(switchPin, LOW , 5000);
if( val != 0) // timeout returns 0
{
Serial.print("switch pressed for ");
Serial.print(val);
Serial.println(" microseconds");
  }

if you want this code

AUX1 = in(X2);          // AUX1 (Reset) controlled by input X1
  in(X0);
  latch(AUX3,X1);
  
  in(AUX1);                 // Read switch connected to Input 0 (Set input - Input 0)
  latch(AUX0,AUX2);	  // Latch command, Output = AUX0, Reset = AUX1 (Input 1)

  in(AUX0);
  timerOn(TIMER0,val*2);
  out(AUX2);
  
  in(AUX2);
  timerOff(TIMER1, 800);
  outNot(AUX4);
  
  in(AUX3);
  andBit(AUX4);
  out(Y0);

Only to be run when the switch is pushed, then it needs to go inside the same block of code (inside the brackets) {}
That the serial.print code is in.

Why use a library for timing (presumably that's what elapsedMillis.h is for) when it is easy and much easier to debug if you just use the technique in the Blink Without Delay example sketch?

...R

Your code has a segment that looks like it's trying to time a button press down to microseconds.

What kind of button or switch are you using?

Any metal to metal contact switch "bounces" for some time as it makes contact. It's a fact.
Some have built-in or added circuit to prevent or at least deal with it, deadzone or otherwise.

I've been writing button code for worst-case by testing with jumpers that I touch to grounded metal and even slide the tip along for a bit. That's for code I want to work for as many others as I can make it so. The technique is called "debouncing" and there are many ways that people do it with varying degrees of success.

So when I see what looks like pulseIn() being used to time a button push and release I have to wonder about the button used. A typical cheap button will ON-OFF in many sub-milli to 100 or more milli bounces before the press stabilizes which is still typically way faster than the human pressing the button knows that the button has actually made contact. What you feel at your finger didn't make it to your head through wire.

What I would expect with a typical button press is for pulseIn() to get that first bounce, which is good data but won't tell you how long you held the button down.

If you properly debounce then you can tell when the last bounce ended but that you need to save that first contact time, then debounce and finally get the last if any bounce on release and call that the whole duration.

What blows my mind is the idea of timing a human pressing a button to micros, unless you're using the low bits to seed randoms in which case hey, not bad!

OTHER kinds of switches like light detection and beam interrupt or properly solid capacitive touch or piezo touch, you can be a lot more definite about yet still if it's finger action time then less than 20 millis probably has no meaning which is why those can be used for randoms.

I am using a photocell (Photo electric sensor ) like a digital input .

Sorry but i have a wrong program , same codes are missing there so i have updated the code below :

#include <plcLib.h>
#include <elapsedMillis.h>

const int switchPin = A3;

unsigned long TIMER0 = 0; // Variable to hold elapsed time for Timer 0
unsigned long TIMER1 = 0; // Variable to hold elapsed time for Timer 0
unsigned long TIMER3 = 0;

unsigned int AUX0; // Latch output variable
unsigned int AUX1; // Latch reset variable
unsigned int AUX2;
unsigned int AUX3;
unsigned int AUX4;

long startTime;
long duration;

void setup() {
Serial.begin(9600);
setupPLC(); // Setup inputs and outputs
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH); // turn on pull-up resistor
Serial.begin(9600);

}

void loop() {

if( digitalRead(switchPin)==HIGH)while(digitalRead(switchPin)==LOW); // timeout returns 0
long duration=millis()-startTime;

{

Serial.print(duration);
Serial.println(" microseconds");
}
AUX1 = in(X2); // AUX1 (Reset) controlled by input X1
in(X0);
latch(AUX3,X1);

in(AUX1); // Read switch connected to Input 0 (Set input - Input 0)
latch(AUX0,AUX2); // Latch command, Output = AUX0, Reset = AUX1 (Input 1)

in(AUX0);
timerOn(TIMER0,duration);
out(AUX2);

in(AUX2);
timerOff(TIMER1, 800);
outNot(AUX4);

in(AUX3);
andBit(AUX4);
out(Y0);

}

  1. you need to post code inside of code tags. In the post edit window, the # button makes code tags.
    without the tags not everything shows properly.

  2. I don't know your libraries which complicates things a bit.
    I haven't the foggiest about what you are doing with PLC's.

if( digitalRead(switchPin)==HIGH)while(digitalRead(switchPin)==LOW); // timeout returns 0
long duration=millis()-startTime;

{
  

Serial.print(duration);
Serial.println(" microseconds");
  }

What do you think this section does and are you trying to measure to millis or micros?

i wont to measure miliseconds

if( digitalRead(switchPin)==HIGH)while(digitalRead(switchPin)==LOW);

You check to see whether the switch pin is HIGH and if it is, enter a loop that is only executed if it is LOW. Have I read that right ?

if( digitalRead(switchPin)==HIGH)
{
while(digitalRead(switchPin)==LOW);
}

Why do you need a library for elapsed millis?
Just make two long variables to get the start and end readings.
Then subtract the end from the start like this example.

long s;
long e;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
s = millis();
delay(100);
e = millis();
Serial.println(e - s);
}

Hook your sensor up to this and see what it tells you. I have it measuring 16+ usec bounce spikes as well as contact held and released.

// Sensor Timer 

// I have this timing micros() so you can get a good idea of how fast loop() can be.
// Use a jumper wire in pin 4 and scratch the other end across the USB connector box,
// you will see durations below 20 usecs measured as you watch the bounce.

// To connect to a sensor, pin 4 to sensor and sensor to ground.

const byte ledPin =  13;      // the number of the LED pin
byte ledState;                // ledState used to set the LED

const byte sensePin = 4;      // the number of the sensor pin
byte       senseData = 3;     // binary data, current read in bit 0, last read in bit 1

unsigned long tStart, tNow;   // event times 

void setup() 
{
  Serial.begin( 115200 );
  Serial.println( F( "\n Time pin 4 data HIGH to LOW\n" ));
  
  pinMode( ledPin, OUTPUT );      
  pinMode( sensePin, INPUT_PULLUP );  // When the switch is closed, sensePin GROUNDS to LOW    
} 

void loop()
{
  senseData = ( senseData << 1 ) & 3; // move the last reads 1 bit higher then cuts off bit 2 and higher
  senseData += digitalRead( sensePin );
  tNow = micros();

  if ( senseData == 2 )       // sensePin went from HIGH to LOW
  {
    tStart = tNow;
  }
  else if  ( senseData == 1 ) // sensePin went from LOW to HIGH
  {
    tNow -= tStart;         // tNow changed to elapsed
    Serial.println( tNow ); // this actually slows fast back to back sensing, keep it small
  }
  
  digitalWrite(ledPin, senseData & 1 ); // ledPin state is set to bit 0 of senseData
}