Go Down

Topic: Single shot (Read 1 time) previous topic - next topic

Hackdub

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..

AWOL

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

Yes, just don't put an "else" clause in your "if" statement.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

spycatcher2k

Follow this logic :
if I read high - set (triggered=TRUE)
if I read low while triggered=TRUE set (triggered=FALSE)
if triggered=HIGH and done=FALSE - DO SOMETHING,then set done=TRUE
start again

Hope this helps
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

Hackdub

Ok so could you apply your thery to this

Code: [Select]
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

HazardsMind

#4
Jan 03, 2013, 04:25 pm Last Edit: Jan 03, 2013, 04:27 pm by HazardsMind Reason: 1
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
}
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Hackdub


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" ;-)
please could you modify my analogue code so I can see where this drops in..
Cheers david.

HazardsMind

#6
Jan 03, 2013, 04:55 pm Last Edit: Jan 03, 2013, 04:56 pm by HazardsMind Reason: 1
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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Hackdub

Code: [Select]
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

HazardsMind

#8
Jan 03, 2013, 05:22 pm Last Edit: Jan 03, 2013, 05:33 pm by HazardsMind Reason: 1
You want the led on just once? then this is not for you. You want


Code: [Select]

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
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Hackdub

Code: [Select]


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

HazardsMind

Your missing a closing bracket somewhere in your loop()
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Hackdub

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

HazardsMind

Code: [Select]
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()
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Hackdub

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)

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



Arrch

Code: [Select]
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:

Code: [Select]
if (latch)
{
  LED_Burst();
}


Go Up