Hi. I am trying to figure out how I can use the arduino to read a voltage in order to start the code. Basically, the arduino will powered all the time, and a contactor relay will have 5VDC on one side and the analog pin connected to the other side. When the start button is pushed the contacts would close and then the analog pin would be able to read 5VDC.
How would I code in that I want the arduino to read the analog pin constantly, and once it sees there is 5 Volts it runs the rest of my code. Otherwise it is just doing nothing waiting for it too read 5VDC.
The rest of my code is having pin 13 HIGH and pin 7 LOW, delay for a half second, then pin 13 LOW and pin 7 HIGH, delay for half second. then just keep repeating that process over and over again.
- why use switch AND relay? Switch is enough
- where is the problem? read an example for analog read and use an "if" or "while" to trap execution or free it to run rest of code.
post any code you have
- Because I have other things that need to run at the same time, so I wanted to keep it powered to ensure no delay in starting between the two.
- The problem is I am not that experienced with coding and needed some guidance with how to approach this.
- Also because this is only a process that last about 2 minutes then needs to stop. Its a machine cart moving on a table, then hits a limit switch to stop. Factor of safety, in case operator leaves, dies etc. then machine wont keep going, limit switch will cut power disengaging contactor relay.
Use an "if" statement.
@alesam: I was looking into that. What would I put for the if part. So "if analogRead==LOW".. I would like it to do nothing,just wait for the analogRead to be HIGH. So I am not sure what I would put there, and then the "else" would be my original code I have.
Read up on using mills instead of delays and then its pretty trivial to add in a if pin = high do something for 2 minutes worth of millis...
@Slumpert: Ok, Ill look into that thanks, the only thing is is that the length of time it will be doing the operation changes anywhere from 30 seconds to about 3 minutes. but what would I put if I want it to do nothing while it reads LOW. There is no set time between when it would read HIGH, could be 5 minutes, could be 3 hours. Thanks.
llacroix413:
@alesam: I was looking into that. What would I put for the if part. So "if analogRead==LOW".. I would like it to do nothing,just wait for the analogRead to be HIGH. So I am not sure what I would put there, and then the "else" would be my original code I have.
analogRead doesn't just return zero or one.
AWOL:
analogRead doesn't just return zero or one.
@AWOL: Does digitalRead do that? I could also just have it be if analogRead >=1023...(my code). else.. do nothing.
What about a while loop, would that work?
llacroix413:
When the start button is pushed the contacts would close and then the analog pin would be able to read 5VDC.
Do the contacts stay closed (seal in) when the button is released? Is the Arduino responsible for closing the contactor? Are any other voltages (like 115-220 vac) present on the contactor?
llacroix413:
analogRead >=1023...
That's above analogRead. analogRead returns a value between 0 and 1023. If you want your code to do something when there is an analogRead, you should do
if(analogRead > 0)
{code}
else
{empty}
I don't see why a while loop wouldn't work. You should be able to set it up like
while(analogRead > 0)
{code}
Keep in mind these codes won't stop executing at the exact time you stop getting analogRead. It will finish the loop they are in, I don't know if it's important in your case.
dougp:
Do the contacts stay closed (seal in) when the button is released? Is the Arduino responsible for closing the contactor? Are any other voltages (like 115-220 vac) present on the contactor?
Yes it is wired up so the contacts stay sealed in once the START button is pressed and released, through a normally closed switch(the STOP switch) which will break the seal in when pressed.
The arduino is responsible for powering 2 solenoid valves, so each solenoid is on a seperate pin, 7 and 13, and it activates opposite of eachother.
The contactor has a 120VAC coil so that is what goes through the switch and into the contactor. I need this 120VAC because I have other things in the system that require it.
I have the 5VDC always powering the arduino, and have a seperate 5VDC line going through one contact in the relay. This was what I wanted to to be looking for, there would be no 5V when nothing is pushed since itd be on the NO side. and when it seals in it would read 5VDC and then start the code.
Thanks.
RabbitTheDevil:
That's above analogRead. analogRead returns a value between 0 and 1023. If you want your code to do something when there is an analogRead, you should doif(analogRead > 0)
{code}
else
{empty}I don't see why a while loop wouldn't work. You should be able to set it up like
while(analogRead > 0)
{code}Keep in mind these codes won't stop executing at the exact time you stop getting analogRead. It will finish the loop they are in, I don't know if it's important in your case.
Ok, Ill try and put that into it. Also when you say {empty} what do you mean, just dont put any code in there and just end it? is that basically what the while loop does, while there is a reading it performs the actions, otherwise it just sits there waiting?
Yeah its not to important, actually thinking about it, that would be better if it finished the loop before stopping once it isn't reading analogRead.
Thanks.
If your input is only 5V or 0V use a DIGITAL pin, connect one side of the AUX contact to Arduino GND, the other to a digital pin, let's say pin 4 for example:
void setup()
{
pinmode(4,INPUT_PULLUP);
}
void loop()
{
if(digitalRead(4) == LOW)
{
do_something;
...
...
...
}
}
Analog pins are basically digital I/O pins that happen to have an analog reading ability as well (except A6 and A7 which are analog input only).
So if you're connected to any of A0-A5 pins, you can just use digitalRead.
Note that the pin is NOT reliably returning 0 or 1023 with various things attached. It may be 1-2 off. So if you really want it to do that way make it something like >900 is high, <100 is low.
llacroix413:
Ok, Ill try and put that into it. Also when you say {empty} what do you mean, just dont put any code in there and just end it? is that basically what the while loop does, while there is a reading it performs the actions, otherwise it just sits there waiting?Yeah its not to important, actually thinking about it, that would be better if it finished the loop before stopping once it isn't reading analogRead.
Thanks.
You won't need anything in there, not even else statement to be honest. That is from my point of view and I believe that you only and only want to do something when you have a reading. While loops does what you tell them to do while their initial statement is true, which means if you configure it it like
While(true=true)
it'll execute what's inside, otherwise just skip over the while loop and continue to the rest of the code.
@RAbbitTheDEvil:Thank you, I played around with it and figured out I didn't need anything in the else part.
The only problem I am having now is, basically when I hit start, the arduino isnt already powered so it take a second to warm up then run code and it doesn't start from the top of the loop. I think it is how I hooked up the wiring for it to measure. I am going to have it act like it would be reading voltage and then have it be like "if V < 5 {do nothing} else {code}"
If you get rid of the bootloader (and program via ISP rather than Serial) you will get faster reaction.
I got everything to work fine. I had a little trouble with the wiring and finally figured it out. Thanks for all the help. Here is my code that I used:
void setup()
{
pinMode(13,OUTPUT);
pinMode(7,OUTPUT);
pinMode(4,INPUT);
}
void loop()
{
while (digitalRead(4)== HIGH) {}
digitalWrite(13,HIGH);
digitalWrite(7,LOW);
delay(175);
digitalWrite(13,LOW);
digitalWrite(7,HIGH);
delay(175);;
}