# One Shot?

If you are familiar with PLC then you probably know what I mean by a one shot, but if not it basically causes your instruction to only perform once per toggle of a bit.

Exa

if (int == HIGH) { x = x +1; }

if this were a one shot it would only add 1 to x and wait until "int" went low then if it went high again it would add one more. Instead of adding continuously to x.

Is there a way to do this with an arduino or is this just a pipe dream?

If you’re using an input, you can go for interrupts. Setting the condition to RISING, FALLING, or CHANGE will do what you’re thinking of!

If not, you might have to use a second variable:

``````bool int,int2;
void setup(){
//...
}
void loop(){
//Aquire "int"
int=<value>;
if(int&&!int2){
x++;//Instead of "x=x+1", use this!
}
int2=int;
}
``````

Ok that doesn't sound too bad, I just had a thought though and think this might work.

if (PB == HIGH) { t = t + 1; }

if (t == 1) { x = x + 1; }

And then some reset at some point for t to go back to 0 although obviously if you hold down the PB for long enough the t value will roll back but I think I'll be within that range

Thank you Fuz!! a karma for you suir

Oh!
In that case, you could use a counter variable:

``````bool int,int2;
void setup(){
//...
}
void loop(){
//Aquire "int"
int=<value>;
if(int&&!int2){
x++;
int2=true;
}else{
int2=false;
}
}
``````

It only makes your code longer though, and still uses a second variable. Not sure why you’d use it, but it’s available!

I'm always the guy swimming up stream, I can't help it. I need my unique originality lol. Thanks again.

michaelszabo:
Is there a way to do this with an arduino or is this just a pipe dream?

This code is directly translatable to ladder logic. You’ll probably recognize it.

``````//----- Generate a positive going one-shot pulse on signal 0-1 transition

OSRise = (signal and OSRSetup); //
OSRSetup = !signal;

//---- and another positive going one-shot pulse on signal 1-0 transition

OSFall = (!signal and OSFSetup); //
OSFSetup = signal;
``````