Hi folks.
Just looking for a few pointers, tutorials on starting a cycle from a button press.
Project is a controller for a solenoid for an airsoft gun. Mechanically its up and running, based on kuba mk1 internals.
Basically what i need is, press button solenoid opens for x amount of time (eg .1 second) then closes even if the button is still pressed. Then repeat as the button is pressed again. It only needs to be semi automatic for this project. so one repetition per press.
Super simple but after a heap of googling around i cant find anything that is helping me with this. Im sure thats a failing on my part.
Ive got the solenoid running on a 11.1v lipo through a gate on the button push no problems there. but its no good if it just stays open. i need a measured pulse of gas.
Thanks in advance.
You need to set a flag when the button is pressed then check the flag so as not to reopen the valve until the button is released and flag reset.
If button not pressed
Flag = false
If button pressed and flag is false
Flag = true
Open valve for time required
Weedpharma
Your present implementation presumably uses the pushbutton to directly activate the solenoid.
To use an arduino to do this you'll need a power device to supply the current - which we don't know. Before we recommend such a device, perhaps you could measure the resistance of the solenoid and report back...
regards
Allan.
i will read up on flags thanks weedpharma
Allan
Im using a bd233 transistor as a gate activated by the arduino. So current is supplied direct from battery.
Im just looking to control the length of time the solenoid is open and only allow it to open once per button push.
Thanks again
Ok so this is what i have. simple led and a button.
const int buttonPin = 3;
const int ledPin = 2;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
delay (100);
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Led dosent turn off with the button held. that what im trying to make happen. Forgive my crappyness.
You could do this with a good old 555 .. not that hard
regards
Allan
Your program is not using a flag.
The purpose of the flag is to stop the circuit retriggering after the first output, until the switch is released.
This involves checking the button is pressed AND the flag is false before you enable the output.
You only reset the flag once the button is read.
Weedpharma
I cant find much about using a flags that i can understand with my feeble machinist brain. If anyone feels kind enough to edit this to work for me id be really grateful. it might even make sense to me once i see it in the context a [program i understand.
Thanks
const int buttonPin = 3;
const int ledPin = 2;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, LOW);
}
}
A flag is a variable that you declare and change according to the state of what you need to record.
You can declare a Boolean buttonPressed = FALSE
In the main loop
If button is pressed and buttonPressed is FALSE. If not already pressed
buttonPressed = TRUE. Will stop future activation
Turn on solenoid for time required
If button is released
buttonPressed = FALSE. Will allow to operate next time
Weedpharma
Thanks again weedpharma. I understand the principal, just dont get how to apply it. Do you know of any tutorials or simple projects i could use to teach me how to use flags. would be really helpful if i could build a little project so i can see how it works. I keep getting errors and im probly making a mess of the sketch.
Thanks for being patient.
I am only using an iPad so cannot run a IDE that I need to check my syntax. Hopefully someone else will be able to give you an example with correct syntax.
Weedpharma
still searching for tutorials on using flags and not finding anything useful. so heres what i have.
const int buttonPin = 3;
const int ledPin = 2;
Boolean buttonPressed = FALSE;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
if button is pressed and buttonPressed is FALSE; // If not already pressed
buttonPressed = TRUE; // Will stop future activation
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, LOW);
if button is released;
buttonPressed = FALSE; //Will allow to operate next time
}
}
millions of errors but im flailing in the dark here. there must be some kind of examples that i can try out and learn from. I bet its super easy when u know how.
if button is pressed and buttonPressed is FALSE; // If not already pressed
if ( (buttonState == HIGH) && (buttonPressed == FALSE) ){
all the int could be byte
Boolean -> small b?
"still searching for tutorials on using flags " and yet here you are successfully using the flag buttonPressed.
Don't read too much into it - a flag is just a variable that typically contains a 0/1 state, or LOW/HIGH, or FALSE/TRUE (false/true? - check whether these are caps or not. I can never remember so I just use 0/1 myself).
Kingleigh, my suggestion is in psuedocode. Not literal C code. It is used to describe what is to happen in plain English.
CrossRoads has changed a line in your code that you had used my pseudocode, into actual code.
Weedpharma
haha im so crap at this.
ok so now the only errors i get are buttonpressed and false not declared in this scope. so does that mean i need to define buttonpressed as buttonpin high in the setup?
const int buttonPin = 3;
const int ledPin = 2;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if ( (buttonState == HIGH) && (buttonPressed == FALSE) ){
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, LOW);
}
You have not:
Declared buttonPressed as a variable of type Boolean
Set buttonPressed as TRUE once button is pressed
Looked for button being released and resetting buttonPressed to FALSE
I have told you what to do twice but you still have not done it. Read what has been said previously and change your code accordingly.
Please put your code in its own window as seen in other posts. This can be done by placing [code] and [/code] around the code or use the </> icon. This makes it easier for others to read.
How to use this forum
Weedpharma
Sorry, is this closer to what you mean
const int buttonPin = 3;
const int ledPin = 2;
boolean buttonPressed = false;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
if ( (buttonState == HIGH) && (buttonPressed == false) )
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
if ( (buttonState == LOW) && (buttonPressed == false) )
{}
}
thanks for being patient with me.
I don't have a PC to run the IDE so can only comment on your code.
Once you have detected the button is pressed and flag is false, you need to change the flag to true. Put this just before you activate the output. Then use the flag in another check to test if the trigger is released.
if ( (buttonState == LOW) && (buttonPressed == true) ).
//button has been released and output has been activated
buttonPressed = false
// reset flag. this will then allow the output to be triggered next time
Get it working using "delay" then convert to using "millis".
Your code uses the button to apply 5v to the input pin. The preferred way is to apply a gnd. Switching 5v risks shorting the supply. You also need to tie the input to either supply (depending on which polarity you switch). Preferred to use a R (10k) to 5v, to tie input to 5v then look for a change to gnd. You can also use an internal R connected via software.
Weedpharma