How to do: different TimeOuts for PulseIn() and digitalRead ?

I have a strange Problem. I have 2 switches, one (#1) works, one doesn’t (#2). But when I hold switch #2 down and press #1 then it works.

Looks like #2 is not triggering any loop action by itself.

One strange thing is, when I remove all LCD code then it works fine.

Here is my code, a bit slimmed for better reading:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int pin = 7;                                 // this pin works fine
unsigned long duration;

const int buttonPin = 8;               // the number of the pushbutton pin
const int ledPin =  10;                  // the number of the LED pin
int buttonState = 0;  

void setup() {
  
 lcd.begin(16, 2);		// set up the LCD's number of columns and rows
    
  pinMode(buttonPin, INPUT);  

  pinMode(pin, INPUT);  // i/o 7 - works
  
  pinMode(ledPin, OUTPUT);      
 
  
 
            }


void loop() 
{
  
  buttonState = digitalRead(buttonPin);

                                                           // check if the pushbutton is pressed  --   if it is, the buttonState is HIGH:

  if (buttonState == HIGH) {     
                                                            // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
  
        duration = pulseIn(pin, HIGH, timeout);

         float msxx;
         msxx = (float)duration / 1000.0;  // msxx = ms

         lcd.setCursor(0, 1);	
         lcd.print(msxx/1000,3);	 
  
    }

That code won't compile because the timeout variable is not declared. Which button does not work and how is it wired to the Arduino ? Has it got any resistors connected to it to pull its voltage up or down when not pressed ?

UKHeliBob:
That code won’t compile because the timeout variable is not declared. Which button does not work and how is it wired to the Arduino ? Has it got any resistors connected to it to pull its voltage up or down when not pressed ?

Sorry, I removed most of the timeout for easier reading, but forgot one.

Again, when I trigger the pulseIn() function while the PIN 8 is HIGH then it works fine. Looks like PIN 8 will not go into a loop by itself.

Update: When I remove pinMode(pin, INPUT); (PIN 7) the switch input works fine. I can’t have more then one input?

Connection like here, but to Pin 8. R is 10K:

As the timeout variable was not declared you presumably had not tried the code you posted. It sounds from your description of the problem like you have the scope of an if statement wrong and that the pulseIn() is being included in the scope of the test of whether the button is pressed. Can you confirm that the code as posted, but with timeout declared, exhibits the problem ?

When I remove ALL timeouts it seem to work. However, the connected LED responds quite slow, maybe 500ms or so.

I will study what the timeout exactly does.....

Update: I made the timeout shorter and it works.

Thanks for pointing me in the right direction!

I still have problem. It looks like I have only one TimeOut for everything, but I need:

A very long TimeOut (10s) for Pin#7 (the PulseIn() pin) A short TimeOut for Pin#8 - were is switch is connected too.

How can that be coded?

In the code that you posted you only had a timeout value on the pulseIn() and digitalRead() does inherently support a timeout anyway. You can do something yourself by looking for a button press within a certain period and reacting appropriately if it does/does not occur.

I am sorry to ask this, but you do realise that the third parameter to pulseIn() does not have to be named timeout or even be a variable, don't you ?

Post your code which uses timeout multiple times and an explanation of what you want the code to do.

Board: Arduino UNO - compatible (some China copy) PC: Windows Xp

Test 1: I removed all timeouts - no change. Still bad. I need to keep the button pushed for about 1 Second to get the desired action. BTW, I used 1.0.4 so far.

Test 2: I removed pulseIn and put just a number in its place. The switch input is now very fast.

Test 3: Same program, with pulseIn, loaded via arduino-1.0.1 - same problem!

Test 4: Same program, with pulseIn, loaded via arduino-0022 - works perfectly!

Test 5: Same program, with pulseIn, loaded via arduino-0023 - works perfectly!

Test 6: Same program, with pulseIn, loaded via arduino-1.0.5 - same problem as 1.0.4!

There must be a a performance issue with the pulseIn command from the 1.0.1 and later versions as old version work perfectly.

Seems I will use 0023 for now then.

There must be a a performance issue with the pulseIn command.

Or with your understand of what it does. I’m putting money on the latter.

Why don’t you explain what your understanding of pulseIn() is, and then we can help you with:
a) a correct understanding
b) alternatives to using pulseIn(), if possible.

PaulS:

There must be a a performance issue with the pulseIn command.

Or with your understand of what it does. I'm putting money on the latter.

Why don't you explain what your understanding of pulseIn() is, and then we can help you with: a) a correct understanding b) alternatives to using pulseIn(), if possible.

a.) I want to measure one digital pulse, from up edge, to down edge. That's what I believe pulseIn() is designed for.

b.) I am thinking about alternatives. Not because of that issue. An alternative would be to send a pulsed signal. i.e. 100KHz, and count the pulses. I will try that later.

Please please also see the message that I posted just before yours. My sketch works in 0022 and 0023 flawlessly, but not in 1.0.1 and 1.0.4