Single shot

is it possible to make a switch "Single Shot" with arduino... I.e it gives a high once.. even when the input is held high... it wont set an op high again until the switch is released and set high?

any ideas..

i want to make a soil meter which triggers an led once when the soil is dry. not latched.

i fully understand the analogue side, but not how to mke it single shot, other than use delays..

is it possible to make a switch "Single Shot" with arduino..

Yes, just don't put an "else" clause in your "if" statement.

(deleted)

Ok so could you apply your thery to this

int sensorPin = A0;            // select the input pin for the soil detector
unsigned int sensorValue = 0;  // variable to store the value coming from the soil detector

void setup()
{
  pinMode(13, OUTPUT);
  //Start Serial port
  Serial.begin(9600);        // start serial for output - for testing
}

void loop()
{
  // read the value from the soil detector:
  sensorValue = analogRead(sensorPin);     
  if(sensorValue<400) digitalWrite(13, HIGH);   // set the LED on
  else digitalWrite(13, LOW);   // set the LED on

I want the led just to blink once say 1 second even if the input condition stays the same.. obviously if the input changed >400 then this should reset..

does this make any sense..

please help

david

Pseudo example:

byte latch = 0; //initial variable
If(switch == HIGH) { //looks at the switch, if it is high for the first time, LED is ON. If the switch changes then goes high again, LED is OFF

!latch; // This detects whether the switch changed or not.

latch ? (do this if TRUE) : (do this if FALSE); //conditional statement, compressed IF/ELSE statement
}

HazardsMind:
Pseudo example:

byte latch = 0; //initial variable
If(switch == HIGH) { //looks at the switch, if it is high for the first time, LED is ON. If the switch changes then goes high again, LED is OFF

!latch; // This detects whether the switch changed or not.

latch ? (do this if TRUE) : (do this if FALSE); //conditional statement, compressed IF/ELSE statement
}

thankss for the quick reply "the penny is satrting to drop" :wink:
please could you modify my analogue code so I can see where this drops in..
Cheers david.

just replace switch == HIGH with sensorValue<400 and replace (do this if TRUE) with digitalWrite(13, HIGH) and likewise for (do this if false).

We only provide examples, the rest is up to you.

int sensorPin = A0;            // select the input pin for the soil detector
unsigned int sensorValue = 0;  // variable to store the value coming from the soil detector

void setup()
{
  pinMode(13, OUTPUT);
  //Start Serial port
  Serial.begin(9600);        // start serial for output - for testing
}

void loop()
{
byte latch = 0;  //initial variable

  // read the value from the soil detector:
  sensorValue = analogRead(sensorPin);     
  if(sensorValue<400) 
!latch; // This detects whether the switch changed or not 
latch ? (digitalwrite(13, HIGH) : (digitalWrite(13, LOW);  //conditional statement,  compressed IF/ELSE statement
}

is this correct.. not sure on the brackets?

so in thery the lED will come on oonce,, will it go off if the input is still high? as this is what i want just a blip from the led.. i.e not on continuous if the input is held in the <400 condition.

cheers david

You want the led on just once? then this is not for you. You want

if(sensorValue<400)
{ 
!latch; // This detects whether the switch changed or not 
latch ? LED_Burst()  : (/*Just smile and wave*/ );  //conditional statement,  compressed IF/ELSE statement
}

void LED_Burst() //outside of LOOP()
{
digitalwrite(13, HIGH);
delay(1000); // 1 second delay  
digitalWrite(13, LOW);
return;
}

"byte latch" must be moved and go up with sensorPin and sensorValue

int sensorPin = A0;            // select the input pin for the soil detector
unsigned int sensorValue = 0;  // variable to store the value coming from the soil detector
byte latch = 0;  //initial variable

void setup()
{
  pinMode(13, OUTPUT);
  //Start Serial port
  Serial.begin(9600);        // start serial for output - for testing
}

void loop()
{
  // read the value from the soil detector:
  sensorValue = analogRead(sensorPin);     
  if(sensorValue<400) 
{ 
!latch; // This detects whether the switch changed or not 
latch ? LED_Burst()  : (/*Just smile and wave*/ );  //conditional statement,  compressed IF/ELSE statement
}

void LED_Burst() //outside of LOOP()
{
digitalwrite(13, HIGH);
delay(1000); // 1 second delay  
digitalWrite(13, LOW);
return;
}

so is this correct? I do hope so.. sorry just learning the ropes

Your missing a closing bracket somewhere in your loop()

So close.. can't see the mistake ;-(

void loop()
{
  // read the value from the soil detector:
  sensorValue = analogRead(sensorPin);     
  if(sensorValue<400) 
     { 
       !latch; // This detects whether the switch changed or not 
       latch ? LED_Burst()  : (/*Just smile and wave*/ );  //conditional statement,  compressed IF/ELSE statement
     }
}   //problem was here, you needed this

If you tried to compile it in the arduino software, you would have gotten an error in the LOOP() telling you, you need a closing bracket " } " before void LED_Burst()

still getting an error
sketch_jan03a.ino: In function 'void loop()':
sketch_jan03a:18: error: expected primary-expression before ')' token

what needs to go here? (in BOLD)

!latch; // This detects whether the switch changed or not 
       latch ? LED_Burst()  : ([b]/*Just smile and wave*/ [/b]);  //conditional statement,  compressed IF/ELSE statement
latch ? LED_Burst()  : ([b]/*Just smile and wave*/ [/b]);  //conditional statement,  compressed IF/ELSE statement

That's a gross misuse of the ternary operator; it's much more simply written like this:

if (latch)
{
  LED_Burst();
}

Ok thanks guys for you help... still no led though... if I pull A0 high or low :frowning:

This is my full code

int sensorPin = A0;            // select the input pin for the soil detector
unsigned int sensorValue = 0;  // variable to store the value coming from the soil detector
byte latch = 0;  //initial variable

void setup()
{
  pinMode(13, OUTPUT);
  //Start Serial port
  Serial.begin(9600);        // start serial for output - for testing
}
void loop()
{
  // read the value from the soil detector:
  sensorValue = analogRead(sensorPin);     
  if(sensorValue<1000) 
     { 
       !latch; // This detects whether the switch changed or not 
       if (latch)
{
  LED_Burst();
}
     }
}   //problem was here, you needed this

void LED_Burst() //outside of LOOP()
{
digitalWrite(13, HIGH);
delay(1000); // 1 second delay  
digitalWrite(13, LOW);
return;
}

I sent you what to change. Keep in mind that what I gave you before was only meant to be pseudocode, an example, not the actual working code.

I am now a GOD member!

       !latch; // This detects whether the switch changed or not

No, it doesn't. It applies the NOT operator to latch, but then you discard the result, so nothing happens. You might as well delete this statement.

byte latch = 0;  //initial variable

Why are you using a byte variable for boolean operations? Make it clear that you MEAN boolean by using boolean. And initialize it to true or false correctly.

@everyone

Again it was only meant to be pseudo code, not a working code! The OP sent me a personal message saying it wasn't working, so it tested it myself, and it DIDN'T work. (Obviously)
So I messaged him back the actual working code, and I got back "Thankyou it works!"

@PaulS

I did and I did.