# Programming an arduino (newbie)

Hello everyone. I'm pretty new to programming, so I hope I can get some help here.

I have a machine that gets an error if a pulse is given very fast after another pulse (30ms). So, I would like the arduino to ignore the pulse if it is given within 30ms of the previous pulse.

I have bought an arduino and started playing a little bit with it, but I don't really know how to start this problem. Any ideas? Thanks in advance!

Imagine you had the same problem, but stretch the timescale. You have to raise your hand if someone kicks you every 30 minutes, but not if they kick you if less than 30 minutes have elapsed since you were last kicked. You have a pencil, a paper and a wall-clock.

How would you solve that?

once you resolved the real life kicks problem,

you will also need the following in your arduino program

millis()

http://www.arduino.cc/en/Reference/Millis

best

I'd poke the kicker in the eye with the pencil. Problem solved ;D

I would write down when he kicked me, watched the clock untill 30 minutes have passed so I would know when to raise my hand. If he kickes me again I would ignore it and write it down again.

So you're saying I should start counting 30ms when I get the pulse. Then have a delay or something untill the 30ms are passed. Then start over again?

Then have a delay or something untill the 30ms are passed

I'd go with the "or something" option. If you're in a "delay()" you can't do anything else, like checking to see if you've been kicked.

Have a look at the "blink without delay" example for clues.

Alright. I have looked at it. So I should keep checking if a long enough interval (30ms for me) has passed. If it has I should start the loop over again, like the blink without delay does. Am I right?

I have made this program now by using the blink without delay program:

``````/* Program which makes sure two
pulses can not be given within 30 ms.

The circuit
....
*/

// constants won't change. Used here to
// set pin numbers:
const int pulsePin =  11;      // the number of the pin

// Variables will change:
int pulseState = LOW;             // ledState used to set pulse
long previousMillis = 0;        // will store last time pulse was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 30;          // interval between two pulses (microseconds)

void setup() {
// set the digital pin as output:
pinMode(pulsePin, OUTPUT);
}

void loop()
{
// check to see if it's time to give another pulse that is, if the
// difference between the current time and last time a pulse has been given
// is bigger than the interval at which you want to give a pulse.
unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
// save the last time a pulse has been given
previousMillis = currentMillis;

// if the pulse is not given, give it and vice-versa:
if (pulseState == LOW)
pulseState = HIGH;
else
pulseState = LOW;

// set the pulse with the pulseState of the variable:
digitalWrite(pulsePin, pulseState);
}
}
``````

Does this look ok as a start? I picked pin 11 as the output. Now I should use a digital input with which I get the pulse, right?

Sorry for all the questions, but I’m new to this.

``````[glow]unsigned[/glow] long previousMillis;        // will store last time pulse was updated

void setup() {
// set the digital pin as output:
pinMode(pulsePin, OUTPUT);
[glow]previousMillis = millis();[/glow]
}

void loop()
{
if(currentMillis - previousMillis >[glow]=[/glow] interval) {
``````

I have changed it. Thanks a lot.

A friend of mine has made this circuit for me:

What to do now? Any help is appreciated.

I don't really know how to go further with the program. All I have at the moment is the blink without delay which I edited for my purpose:

``````/* Program which makes sure two
pulses can not be given within 30 ms.

The circuit
....
*/

// constants won't change. Used here to
// set pin numbers:
const int pulsePin =  11;      // the number of the pin

// Variables will change:
int pulseState = LOW;             // ledState used to set pulse
long previousMillis = 0;        // will store last time pulse was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 30;          // interval between two pulses (microseconds)

void setup() {
// set the digital pin as output:
pinMode(pulsePin, OUTPUT);
}

void loop()
{
// check to see if it's time to give another pulse that is, if the
// difference between the current time and last time a pulse has been given
// is bigger than the interval at which you want to give a pulse.
unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
// save the last time a pulse has been given
previousMillis = currentMillis;

// if the pulse is not given, give it and vice-versa:
if (pulseState == LOW)
pulseState = HIGH;
else
pulseState = LOW;

// set the pulse with the pulseState of the variable:
digitalWrite(pulsePin, pulseState);
}
}
``````

Hope I can get some help here, don't know what to do now.

Is it milliseconds or microseconds? Your code and comments seem at odds.

pulses can not be given within 30 ms.

// interval between two pulses (microseconds

The code suggest milliseconds.

I don't see where you're testing for an incoming pulse.

``````if (pulseState == LOW)
pulseState = HIGH;
else
pulseState = LOW;
``````

can be written

``````pulseState = !pulseState;
``````

It is milliseconds, have to adjust my comments. I changed that code.

You're right, I'm not testing for an incoming pulse. I was watching at the other part of the code too much. How should I write in code to keep testing for an incoming pulse? Can't really find it in the examples. Thank you!

Anyone? Sorry for all the questions, I know i'm a newbie.

Detecting a pulse is probably as simple as detecting a change of state on a digital input. To do this, all you need to know is the state of the input last time you looked at it. If the values are different, you've detected the start of a pulse, but you haven't told us what polarity of pulse to expect, nor how long it is expected to be (probably less than 30ms)

I don't really know how long the pulse is. Let's say it is 1ms, how should I program this?