Go Down

### Topic: Apply Constant Voltage with Switch (Read 1 time)previous topic - next topic

#### stevekap25

##### Feb 19, 2013, 01:43 am
For our project, we need to apply constant voltage to an actuator using an Arduino Mega when the joystick is pushed either forward or backward
joystick: http://na.suzohapp.com/joysticks/competition_joy.htm

The goal is for the actuator the either pull forward or retract depending on the direction the joystick is pushed. So far we have a basic code for the actuator to just constantly pull forward when it is attached to the Arduino.  The problem is we don't know how to initiate an If loop to say "if the joystick is pushed in positive y then positive voltage is applied and if joystick is pushed in negative y then negative voltage is applied an the joystick retracts." Please if someone can just lead us in the right the direction that would be great.

int actuator = 13;
void setup() {
pinMode(actuator, OUTPUT);
}
void loop() {
digitalWrite(actuator, HIGH);

#### mcleung

#1
##### Feb 19, 2013, 02:57 am
By "Negative Voltage" I'll assume you mean 0V, as the arduino can not output negative voltage. If that is what you want/require, then you will need to set up an exteral circuit. Also, I'm not sure what the current draw of your servo requires, but again, I will assume that it is within the limits of the Arduino.

You'll need to set up two input pins, one to know when the joystick is +y, and one when it is -y.

From what I can see from your joystick is that they are simple togglebuttons, they will either be opencircuit when pushed or be short circuit when pushed. You will need to experiment to determine which it is.

Also, just a suggestion but you should use #DEFINE for the actuator pin since it doesn't change throughout your sketch.
See http://arduino.cc/en/Reference/Define

One thing with this sketch is that it will stay in the positive/negative state until you tell it to inverse, the arduino is digital (1 or 0) there is no positive, zero and negative voltage available unless you use more then one pin.

Code: [Select]
`#DEFINE actuator 13#DEFINE YposPin 10#DEFINE YnegPin 11void setup() {                  pinMode(actuator, OUTPUT);  pinMode(YposPin , INPUT);  pinMode(YposPin , INPUT);}void loop(){  if (YposPin)  {    digitalWrite(actuator, HIGH);  }  if (YnegPin)  {    digitalWrite(actuator, LOW);  }}`

#### stevekap25

#2
##### Feb 19, 2013, 03:22 am
Thank you.  Our only question is what exactly do you mean to "define"?  We looked at the tutorial you posted but are not sure how to define any of the pins. Also, how exactly does the Arduino know if the button is getting pushed?

#### mcleung

#3
##### Feb 19, 2013, 04:12 am
Define will basically find and search the first "word" and replace it with the second "word"

Thus in your sketch, during compilation it would replace all the instances of "actuator" with "13". For example, it would be digitalWrite(13, HIGH);
There is no point to make "actuator" a variable if it will not change during the entire sketch.

Sorry, you are right, I was missing a digitalRead command first. Mail loop should be:

boolean Ypos = 0;
boolean Yneg = 0;
Code: [Select]
`void loop(){Ypos = digitalRead(YposPin);Yneg = digitalRead(YnegPin);  if (Ypos)  {    digitalWrite(actuator, HIGH);  }  if (Yneg)  {    digitalWrite(actuator, LOW);  }}`

#### PeterH

#4
##### Feb 19, 2013, 04:24 am

"if the joystick is pushed in positive y then positive voltage is applied and if joystick is pushed in negative y then negative voltage is applied an the joystick retracts."

The code shows you just turning an output high and low. How is your actuator driven from this, and what sort of actuator is it?

#### stevekap25

#5
##### Feb 19, 2013, 05:24 pm
The linear actuator being used it:
http://www.firgelliauto.com/product_info.php?cPath=82&products_id=52

The problem we are having is that when the joystick is pushed +y, the linear actuator pushes forward, we did that correctly.  However, ideally when in the normal state (joystick is in center) the actuator retracts.  Then we will have another actuator set up for -y movement applied to the brake. We cannot configure how to have the actuator retract when in steady state.  Normally to make the actuator retract we need to apply negative voltage.

The code being used so far is:
* LED attached from pin 13 to ground
* joystick attached to pin 12 from +5V
int buttonPin = 12;     // the number of the pushbutton pin
int actuator =  13;      // the number of the LED pin
int buttonState = 0;

void setup() {
// initialize the LED pin as an output:
pinMode(actuator, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

}

void loop(){
// read the state of the pushbutton value:

// check if the pushbutton is not pressed.
// if it is, the buttonState is LOW:
if (buttonState == LOW) {
// turn LED off:
digitalWrite(actuator, LOW);
}
else {
// turn LED on:
digitalWrite(actuator, HIGH);
}
}

Go Up