I'm new to Audrino but not new to programming & Microcontrollers, so far I'm loving the Audrino.
I'm having an issue with PulseIn() I can read pulses but it slows my program down to the point that it isn't useable. I have a simple program blinking an led. if i use the PulseIn() function anywhere in my program the blinking of the led will not blink at the desired frequency instead it will just idle and blink once every second. I've tried changing the timeout time on the PulseIn() but it will limit my blinking frequency of the led to whatever i set the timeout. Basically the PulseIn() function is not letting my program run like i want it to. Thanks in advance for your help!
Sounds like the issue is caused by whatever the delay is between pulses. Interrupts are another possibility - if you're going to poll the pin though you're going to have to get rid of the delays in your code. That of course means a trip to take a look at the blink without delay example.
I just did the "blink without delay" example and if I insert the pulseIn() function anywhere in the loop I get the exact same result. Blinking slows down to whatever the timeout on PulseIn() is. This leads me to beleive it's a problem with PulseIn() am I missing something here? This should be a very simplet thing to do, read an input single and blink an led??
const int ledPin = 8; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
unsigned long duration;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(7, INPUT);
Serial.begin(9600);
}
void loop()
{
Serial.println(duration);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
duration = pulseIn(7, HIGH);
Serial.println(duration);
}
}
I just did the "blink without delay" example and if I insert the pulseIn() function anywhere in the loop I get the exact same result. Blinking slows down to whatever the timeout on PulseIn() is. That because PluseIn block waiting for the pulses before returning control to loop. Look up polling in the playground.
Thanks for the Reply Mark. I've searched around for Polling but haven't found much help in learnig the basics. I'll keep searching and see what I can find. Thanks for your help!
pulseIn() is a blocking call. Nothing else will run while pulseIn() waits for a pulse to happen.
If the code is getting stuck for long periods of time, it would suggest something is preventing a good pulse from coming in. What's the code on the other Arduino?
Which pin are you trying to monitor? With all of those delay()s, the total cycle time here is over 400ms. Also, the code isn't complete (doesn't compile as is), so I'm assuming there is just a closing brace and no other code.
Yes you are correct the code is missing a } it didn't copy over. I was monitoring all of them one at a time to see if pulseIn() could read the different pulse signals. I'll try it with one pulse signal and delete all the others in the code so it doesn't slow it down. I see what you are saying that due to the delays it's slowing the complete program down.