It is currently Sat Apr 29, 2017 3:20 pm

All times are UTC - 8 hours




 Page 1 of 1 [ 11 posts ] 
Author Message
 Post subject: Arduino motor control with proximity sensor help
PostPosted: Wed Feb 15, 2017 10:25 am 

Joined: Mon Feb 13, 2017 6:05 pm
Posts: 5
Hello


Basically if it was an LED I want the LED to be turned off when a proximity sensor detects an object, then after 2 seconds I want the LED to come back on while the prox is still sensing the object, so that it can clear the prox and start the code over for the next object to be picked up by the prox. I have changed my code so many times, and all I can come up with is the prox senses the object and turns off led but with object in front of prox i cant get nothing else to happen.

I was just useing led as an example, it is actually a motor that is turning an arm that is going in front of prox to stop motor at 12:00 position, but once arm is in front of the prox motor stops. I need something to turn on motor after a 2 second delay to clear prox and restart again. I want motor to stop for 2 seconds at 12:00 position then turn motor on till it reaches 12:00 again. I have learned a lot in this Venture but I am stuck and I need to get this project going. I am sure it's quite easy but I am new to this.


I have been working on a project, while trying to figure out how to control it, that is how i found out about Arduino, needless to say the Arduino world is new to me. The project consists of controlling a wheel chair motor via a 100 amp solid state relay with a proximity sensor. What i am trying to accomplish is the shaft of the motor will be at the center of an arm so that the prox sensor will detect when the motor has rotated to the 12:00 position. Once the prox senses the arm I want the motor to stop for 2 seconds and then rotate again until prox senses the arm again which will be 1/2 revolution being that it will see both ends of the arm in 1 rev pausing for 2 seconds each time before rotating again. The code i wrote allows me to stop the motor when the prox senses the arm but i have been trying for a week to figure out how to get it to only stop for 2 seconds then rotate again. Pin 12 is going to control side of the solid state relay not the actual motor. Code is below.

int sensorVal;


void setup(){
//configure pin2 as an input and enable the internal pull-up resistor

pinMode(2, INPUT_PULLUP);
pinMode(12, OUTPUT);

}

void loop(){
sensorVal = digitalRead(2);
// The Logic is inverted a low on pin 2 means a sinking switch is activated
// and a high on pin 2 means the switch is unactivated and pulled up by the internal resistor
// this is not a problem since the controller can interpret the data any way we tell it to

if (sensorVal == HIGH){
digitalWrite(12, HIGH);
}
if (sensorVal == LOW ){
digitalWrite (12, LOW );

}
}

Can this even be done with just the Arduino alone or do i need a motor or relay shield. I have tried putting in delays but it does not react the way i need it to.

Thanks in advance


Offline
 Profile Send private message  
 
 Post subject: Re: Arduino motor control with proximity sensor help
PostPosted: Tue Feb 21, 2017 7:11 pm 
User avatar

Joined: Sat Nov 08, 2003 1:01 am
Posts: 1556
Location: Phoenixville, PA USA
I don't proclaim to be a programmer (DOSBASIC and some VB4 are about as far as I went), but one way to accomplish your goal is to:

When the sensor detects the arm, set the sensor output to stop the rotation.
"Pause" for 2 seconds (probably something like PAUSE 2000)
When PAUSE is done, disable sensor.
Reset motor to start turning.
Re-enable sensor.
Loop

That should put you on the correct path.

CeaSaR



_________________
Hey, what do I know?
Offline
 Profile Send private message  
 
 Post subject: Re: Arduino motor control with proximity sensor help
PostPosted: Wed Feb 22, 2017 9:27 pm 

Joined: Mon Feb 13, 2017 6:05 pm
Posts: 5
Thanks for the reply i have tried delay after the prox senses the arm and it stops the motor, what i am haveing trouble with is since the arm stops in front of the prox, i dont know how to get the arduino to ignore the signal after the 2 seconds elapses so that it can repeat. I have to have the motor start again, i have studied this for more hours than i can count these last two weeks, i either dont understand it or am missing something.


int proxPin = 2; // signal pin
int proxVal;  //reading from proxPin
  int motorPin = 12;  // pin controlling motor
  unsigned long lastTimeProxTriggered = 0;  //the last time the prox was triggered
  unsigned long interval = 2000;  // how long the motor will delay
  int motorPinState = HIGH;
void setup(){
 //configure pin2 as an input and enable the internal pull-up resistor
 pinMode(proxPin, INPUT_PULLUP);
 pinMode(motorPin, OUTPUT);}void loop(){
 unsigned long currentMillis = millis();
 proxVal = digitalRead (proxPin);
digitalRead (motorPin);
 // The Logic is inverted a low on pin 2 means a sinking switch is activated
 // and a high on pin 2 means the switch is unactivated and pulled up by the internal resistor
 // this is not a problem since the controller can interpret the data any way we tell it to if (proxVal == HIGH){
 digitalWrite (motorPin, HIGH);
 
}
 if (proxVal == LOW ){
   digitalWrite (motorPin, LOW);
  
 }
  else if (proxVal == LOW && currentMillis-lastTimeProxTriggered >= interval ){
    lastTimeProxTriggered = currentMillis;
  
  digitalWrite (motorPin, HIGH); 
  }
 }

All it does is stop wont turn motor on i know im missing something.


Offline
 Profile Send private message  
 
 Post subject: Re: Arduino motor control with proximity sensor help
PostPosted: Fri Feb 24, 2017 4:06 am 
User avatar

Joined: Sat Nov 08, 2003 1:01 am
Posts: 1556
Location: Phoenixville, PA USA
I believe it is your IF THEN statement. You are continuously reading the output of the sensor which holds it in a low state. At that point, you need to stop reading the sensor and force the program to flow through the pause and then start reading the sensor again after you've given the arm enough time to clear the sensor.

CeaSaR



_________________
Hey, what do I know?
Offline
 Profile Send private message  
 
 Post subject: Re: Arduino motor control with proximity sensor help
PostPosted: Sat Feb 25, 2017 10:35 am 

Joined: Mon Feb 13, 2017 6:05 pm
Posts: 5
Yeah you are right, only thing is i dont understand how to make the arduino ignore the sensor. I know it is probably simple to someone who knows how to write code, but i have studied this every day after work for the last 3 weeks and i just dont understand thinking about throwing arduino in the trash and getting a plc off ebay i can at least program that.


Offline
 Profile Send private message  
 
 Post subject: Re: Arduino motor control with proximity sensor help
PostPosted: Sat Feb 25, 2017 1:52 pm 
User avatar

Joined: Sat Nov 08, 2003 1:01 am
Posts: 1556
Location: Phoenixville, PA USA
Old school was that after an input, you would set a variable. It was this that you would work with.
In BASIC, it would look something like this:

IF Input > 0
THEN "variable #1" = A
WHILE "variable #1" = A
DO (XYZ)
#after WHILE / DO is complete, reset "variable #1" to original state.

During the WHILE / DO section (whatever that is in Arduino code), you would pause for 2 seconds, then step the arm away from the sensor, reset the sensor to 0 (or just let it go low/non-sensed state) and then loop back to reading the sensor.

Rinse and repeat.

Hope that helps. (I also hope that someone who works with Arduino pops in too! :) )

CeaSaR



_________________
Hey, what do I know?
Offline
 Profile Send private message  
 
 Post subject: Re: Arduino motor control with proximity sensor help
PostPosted: Sat Feb 25, 2017 2:08 pm 
User avatar

Joined: Sat Nov 08, 2003 1:01 am
Posts: 1556
Location: Phoenixville, PA USA
Another question I have is:
Have you drawn out a flow chart of your program?

I believe that will help you better follow what you are trying to accomplish and help spot potential errors or conflicts, such as the IF/THEN problem. That way you'll know whether you are in a linear situation or if a branch is required.

CeaSaR



_________________
Hey, what do I know?
Offline
 Profile Send private message  
 
 Post subject: Re: Arduino motor control with proximity sensor help
PostPosted: Sat Feb 25, 2017 3:26 pm 

Joined: Mon Feb 13, 2017 6:05 pm
Posts: 5
I have drawn it out im not familiar with the terminology, and by following examples the code looks right to me but its not. Its just aggravating with all the time i spent studying about the problem as much as i have and not seeing any results. Thanks a bunch for your help i will try to implement what you said in my code.


Offline
 Profile Send private message  
 
 Post subject: Re: Arduino motor control with proximity sensor help
PostPosted: Mon Feb 27, 2017 11:24 am 

Joined: Fri Jan 29, 2016 6:38 am
Posts: 16
Location: Central Michigan
I'm not a programmer either (BASIC, COBOL, various PLC's, and trying to follow programming examples in N&V), but I think I see your problem, CeaSaR was on the right track.

if (proxVal == LOW ){
digitalWrite (motorPin, LOW);


I think this keeps the motor stopped as long as the sensor is activated (forever?), ignoring the "ELSE" because the "IF" compare is true. You need a "buffer" between input and output.

if (proxVal == LOW AND (currentMillis-lastTimeProxTriggered<interval)){
digitalWrite (motorPin, LOW);

This might give you a "stutter start" until the arm clears the prox, unless you also add some time delay between motor start and "lastTimeProxTriggered = currentMillis" (effectively clearing your timer).

I think CeaSaR was pointing toward a more elegant approach, in that when the prox input goes low, it simply "sets" an internal working bit. That internal bit then stops the motor, starts the timer, waits for the "interval", restarts the motor, all while ignoring the prox input, and only gets "cleared" after the prox no longer senses the arm. Then you can start looking at the prox input again.

I don't know if Arduino has a "one-shot" function but there are other ways to "set" an internal bit for one pass through your program loop when an input changes state, for example to trigger your motor pause cycle.

I'm definitely not an Arduino programmer, but I hope this points you in a positive direction. Of course, you need to translate it into "Arduino speak", syntax and punctuation,etc. Good luck.

Dale


Offline
 Profile Send private message  
 
 Post subject: Re: Arduino motor control with proximity sensor help
PostPosted: Mon Feb 27, 2017 4:04 pm 
User avatar

Joined: Sat Nov 08, 2003 1:01 am
Posts: 1556
Location: Phoenixville, PA USA
Dale,

Thanks for jumping in. Not knowing the syntax of the Arduino language makes my replies a bit on the vague side with a hint of insight. ;)

The beauty of setting a variable to control things instead of directly reading the sensor is that all you need is the initial change in state. Once that happens, the variable controls everything, effectively removing the sensor even though it is still connected. The next best thing is that you can keep reading the sensor until it doesn't see the arm anymore and then reset the variable to its original state.

For example:
While Variable = 0, motor runs and sensor is read until arm is in position. Then the variable gets set to Variable =1.

This starts the subroutine of Pause for 2 seconds, restart the motor, read the sensor. When the arm clears the sensor, the variable gets reset to Variable = 0 and then you exit the subroutine back to reading the senor for the arm to come back.

A big loop until you kill the progam. Should work like a well oiled clock. You just need to translate that into Arduino speak.

Hope this further helps.

CeaSaR

One thing I forgot to mention is the conditional of reading the sensor and reading the variable.

IF sensor =arm AND variable =0, THEN subroutine, set variable to 1.
IF sensor =no arm AND variable =1, THEN exit subroutine and set variable to 0.
Loop



_________________
Hey, what do I know?
Offline
 Profile Send private message  
 
 Post subject: Re: Arduino motor control with proximity sensor help
PostPosted: Mon Feb 27, 2017 11:11 pm 

Joined: Mon Feb 13, 2017 6:05 pm
Posts: 5
Thanks alot guys I really appreciate everyones input, you are opening my mind and eyes for sure. I am going to take what you said and study some more. Thanks a bunch.


Offline
 Profile Send private message  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 11 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: