Go Down

Topic: randomly activating a solenoid within 15 second time frame (Read 745 times) previous topic - next topic

stivheys

hi my this is my first post here i am quite new to the scene i have a little experience with arduino but also a limited amount (i have build a 3d printer from tutorials with one)

my current project is this:

i want to be able to press a push button switch to activate a timer that randomly triggers a solenoid once within 15 seconds of it being pressed and the ability to return the solenoid to its original posistion with the press of another push button switch without a delay (i am under the impression that a decent sized solenoid cant be triggered directly from the arduino because of the limited power available this is fine as i will use a car battery to supply the power for this)

how easy is this going to be,what components will i need (if any) besides my arduino wires battery switch and solenoid and where do i start?

i am also new to the coding too as i have used open source code for previous projects

im sorry if i have been a little vague here if i have missed any important information please let me know and i will try to give all the info i can

your time and help is much appreciated  :)

thanks in advance and have a good day all

tylernt

how easy is this going to be,
Easy.

Quote
what components will i need (if any) besides my arduino wires battery switch and solenoid and where do i start?
Well depending on the solenoid, you're going to want either a transistor or a reed relay or a relay module to trigger it. Do you have the datasheet for the solenoid? We need to know how many (milli)amps it draws or how many Ohms of resistance its coil has.

If you have one button to energize the solenoid and another one to retract it, you do not need to debounce the buttons (that makes things easier). It's best to use the INPUT_PULLUP pinMode for these and look for them to go LOW with digitalRead() when the button is pressed.

Also:
http://arduino.cc/en/Reference/Random
http://arduino.cc/en/Reference/RandomSeed
You may wish to connect a scrap of wire to an Analog pin that goes nowhere (so it acts as an antenna) and use analogRead on it for the random seed.

terryking228

Hi,

Yes, need info on solenoid.. And how long will the solenoid usually be activated?   What does it do?
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

stivheys

hi ,thanks for the reply's
i have not bought the solenoid yet, basically im helping a friend its for a motocross start gate  he has bought, in racing he will get a sign that the race is about to start and anytime within that 15 secs the gate will drop and he is wanting to replicate this at home so he can practice, he has the gate and the solenoid is so it will remove the pin from the gate.

i was kinda wanting to make sure the concept will work so i was wanting to get it pretty much setup and programmed before getting the solenoid (i was thinking mabye have the program loaded and prove  it by getting the arduino to flash an led or something after the button was within the 15 secs to simulate the signal being sent the the solenoid)


stivheys

#4
Dec 02, 2013, 09:26 pm Last Edit: Dec 02, 2013, 09:54 pm by stivheys Reason: 1
hi again ok so looking at a few other tutorials etc i have come up with this so far (im not sure if it is correct so would be grateful of guidance, it may be completely wrong but im kinda new too this)

the photo is not my own wiring but one i am going to mimic (if it is correct) the code i have attempted to write is a combination of examples i have come across and edited.



if my understanding is right will this trigger the solenoid a single time with a random period of 15 seconds of the rest button being pressed?

thanks :)


just incase you can see the code here it is :)


/*
 Solenoid
*/

// Pin 13 has an solenoid connected on Arduino board.
// give it a name:
int solenoid = 13;

// the setup routine runs once when you press reset:
void setup() {                
 // initialize the digital pin as an output.
 pinMode(solenoid, OUTPUT);    
}

// print a random number from 0 to 15000
void loop() {
 randNumber = random(0,15000);
 Serial.println(randNumber);
 
 delay(50);
}



also the soenoid i will be using is a push  1-15n, 12v dc solenoid 8m150262

tylernt

I don't see a digitalWrite() statment in your code to actually activate the solenoid?

Also, I assume you'll want to delay(randNumber) before digitalWrite(). ;)

stivheys


I don't see a digitalWrite() statment in your code to actually activate the solenoid?

Also, I assume you'll want to delay(randNumber) before digitalWrite(). ;)


ok so could you give an exmple of the digital write statement and its pracement in the code?

and by delaying randnumber is that a pause before it starts generating the rand number. e.g. press the button and then after lets say 5 secs the randnumber will start?

I am fairly new to this so forgive me if i am asking dumb questions,

thanks for your input :)

tylernt

Code: [Select]

/*
 Solenoid
*/

#define ACTIVATED LOW // this makes dealing with buttons using INPUT_PULLUP easier

// Pin 13 has an solenoid connected on Arduino board.
// give it a name:
const byte solenoid = 13;

// pin 12 has the 'go' pushbutton switch connected
const byte goButton = 12;

// pin 11 has the 'stop' pushbutton switch connected
const byte stopButton = 11;

// the setup routine runs once when you press reset:
void setup() {                
 // initialize the digital pin as an output.
pinMode(solenoid, OUTPUT);    
 // initialize the digital pins as inputs with the internal pullup resistors enabled
pinMode(goButton, INPUT_PULLUP);
pinMode(stopButton, INPUT_PULLUP);
}

void loop() {
if(digitalRead(goButton) == ACTIVATED)
 {
 int randNumber = random(0,15000);
 delay(randNumber);
 digitalWrite(solenoid, HIGH);
 while(digitalRead(stopButton) != ACTIVATED); // <-- notice the semicolon, which is normally not used in a while loop. this semicolon makes the arduino sit and do nothing until the stopButton switch is activated
 digitalWrite(solenoid, LOW);
 }
}


Please note that this simplistic code is blocking, that is, the Arduino can't do anything else while it's looking at the buttons. If you need your Arduino to multi-task, a wholly different programming approach is needed.

stivheys

Wow, thank you ever so much :) i cannot wait to get this tried and tested! i dont think i will need to multi task (well at least not yet anyway lol).

thank you  for your time it is greatly appreciated, have a great day :)

stivheys

hi thanks again i have this working flawlessly, although intead of activating at a random time within 15 seconds it seems to activate 3 seconds after the go button is pressed, any ideas? :)

tylernt

'Random' on a computer isn't really random. Each time you reset the Arduino, it will play back the same "random" sequence again. You will want to use the randomSeed() function and some source of entropy -- entropy can come from an analogRead on an unconnected pin, or from micros(), or some other way.

stivheys

ok, i thought i may need to random seed is it just a case of changing randomnumber to randomSeed in the loop?

tylernt


ok, i thought i may need to random seed is it just a case of changing randomnumber to randomSeed in the loop?
No, random() stays the same. randomSeed goes in setup().

Here are the links to the documentation:

http://arduino.cc/en/Reference/Random
http://arduino.cc/en/Reference/RandomSeed


Go Up