├── README.md └── HBR-Proportional-Valve └── HBR-Proportional-Valve.ino /README.md: -------------------------------------------------------------------------------- 1 | # HBR-Proportional-Valve 2 | This is an arduino C sketch for controlling a proportional valve with potentiometer joy stick 3 | Please include code explanation if you are able 4 | Here's what I would use to start my cut & pastry from the Arduino page 5 | 6 | /* Read Jostick 7 | * ------------ 8 | * 9 | * Reads two analog pins that are supposed to be 10 | * connected to a jostick made of two potentiometers 11 | * 12 | * We send three bytes back to the comp: one header and two 13 | * with data as signed bytes, this will take the form: 14 | * Jxy\r\n 15 | * 16 | * x and y are integers and sent in ASCII 17 | * 18 | * http://www.0j0.org | http://arduino.berlios.de 19 | * copyleft 2005 DojoDave for DojoCorp 20 | */ 21 | 22 | int ledPin = 13; 23 | int joyPin1 = 0; // slider variable connecetd to analog pin 0 24 | int joyPin2 = 1; // slider variable connecetd to analog pin 1 25 | int value1 = 0; // variable to read the value from the analog pin 0 26 | int value2 = 0; // variable to read the value from the analog pin 1 27 | 28 | void setup() { 29 | pinMode(ledPin, OUTPUT); // initializes digital pins 0 to 7 as outputs 30 | Serial.begin(9600); 31 | } 32 | 33 | int treatValue(int data) { 34 | return (data * 9 / 1024) + 48; 35 | } 36 | 37 | void loop() { 38 | // reads the value of the variable resistor 39 | value1 = analogRead(joyPin1); 40 | // this small pause is needed between reading 41 | // analog pins, otherwise we get the same value twice 42 | delay(100); 43 | // reads the value of the variable resistor 44 | value2 = analogRead(joyPin2); 45 | 46 | digitalWrite(ledPin, HIGH); 47 | delay(value1); 48 | digitalWrite(ledPin, LOW); 49 | delay(value2); 50 | Serial.print('J'); 51 | Serial.print(treatValue(value1)); 52 | Serial.println(treatValue(value2)); 53 | } 54 | -------------------------------------------------------------------------------- /HBR-Proportional-Valve/HBR-Proportional-Valve.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | //declare the pins for the joystick X and Y axis 6 | const int16_t JOY_X_PIN = 0; 7 | const int16_t JOY_Y_PIN = 1; 8 | 9 | //declare the enable button pin 10 | const int16_t EN_BTN_PIN = 10; 11 | 12 | //declare the output pins (must be PWM capable) for solenoid control 13 | const int16_t VALVE_X_POS = 3; 14 | const int16_t VALVE_X_NEG = 5; 15 | const int16_t VALVE_Y_POS = 6; 16 | const int16_t VALVE_Y_NEG = 9; 17 | 18 | 19 | //setup the two joystick axis 20 | ArduinoJoystick joystickX(0, 0); 21 | ArduinoJoystick joystickY(1, 1); 22 | 23 | //setup the button to start/stop 24 | ArduinoButton enableButton(2, 10); 25 | 26 | //setup the transform from the input pots to the output PWM signals for the valves 27 | LinearTransform transform(-128, 126, -512, 512); 28 | 29 | int16_t xValue = 0; 30 | int16_t yValue = 0; 31 | 32 | 33 | int pwmPins[] = {VALVE_X_POS, VALVE_X_NEG, VALVE_Y_POS, VALVE_Y_NEG}; 34 | 35 | void setup() 36 | { 37 | Serial.begin(9600); 38 | 39 | //calibrate for joystick found here: http://uk.rs-online.com/web/p/products/8430838/ 40 | //set Low/Center/High positions 41 | joystickX.setPoints(390, 520, 640); 42 | joystickY.setPoints(390, 520, 640); 43 | 44 | //set the deadbands 45 | joystickX.setDeadbands(5, 5, 5); 46 | joystickY.setDeadbands(5, 5, 5); 47 | 48 | //set the minimum change to trigger an event 49 | joystickX.setThreshold(5); 50 | joystickY.setThreshold(5); 51 | 52 | //set the transform from input values to output values 53 | joystickX.setTransformation(&transform); 54 | joystickY.setTransformation(&transform); 55 | 56 | //get initial readings from the joystick axis 57 | xValue = constrain(joystickX.getValue(), -255, 255); 58 | yValue = constrain(joystickY.getValue(), -255, 255); 59 | 60 | 61 | //set all the pins as outputs and initialize to off 62 | for (int i = 0; i < 4; i++) 63 | { 64 | pinMode(pwmPins[i], OUTPUT); 65 | analogWrite(pwmPins[i], 0); 66 | } 67 | } 68 | 69 | void loop() 70 | { 71 | enableButton.poll(); //check for changes in the button enable state 72 | 73 | //check for updates in the joystick X position 74 | if(joystickX.poll()) 75 | { 76 | //grab the current value and print it out for debugging 77 | xValue = constrain(joystickX.getValue(), -255, 255); 78 | printPositionData(); 79 | 80 | //only allow the system to move if the button is active 81 | if (enableButton.isActive()) 82 | { 83 | if (xValue > 0) 84 | { 85 | analogWrite(VALVE_X_POS, abs(xValue)); 86 | analogWrite(VALVE_X_NEG, 0); 87 | } 88 | else if (xValue < 0) 89 | { 90 | analogWrite(VALVE_X_POS, 0); 91 | analogWrite(VALVE_X_NEG, abs(xValue)); 92 | } 93 | } 94 | 95 | //otherwise turn off both valves for X axis 96 | else 97 | { 98 | analogWrite(VALVE_X_POS, 0); 99 | analogWrite(VALVE_X_NEG, 0); 100 | } 101 | } 102 | 103 | 104 | 105 | //check for updates in the joystick Y position 106 | if(joystickY.poll()) 107 | { 108 | //grab the current value and print it out for debugging 109 | yValue = constrain(joystickY.getValue(), -255, 255); 110 | printPositionData(); 111 | 112 | //only allow the system to move if the button is active 113 | if (enableButton.isActive()) 114 | { 115 | if (yValue > 0) 116 | { 117 | analogWrite(VALVE_Y_POS, abs(yValue)); 118 | analogWrite(VALVE_Y_NEG, 0); 119 | } 120 | else if (yValue < 0) 121 | { 122 | analogWrite(VALVE_Y_POS, 0); 123 | analogWrite(VALVE_Y_NEG, abs(yValue)); 124 | } 125 | } 126 | 127 | //otherwise turn off both valves for Y axis 128 | else 129 | { 130 | analogWrite(VALVE_Y_POS, 0); 131 | analogWrite(VALVE_Y_NEG, 0); 132 | } 133 | } 134 | 135 | } 136 | 137 | 138 | void printPositionData(){ 139 | Serial.print("Joystick Position - X: "); 140 | Serial.print(xValue); 141 | Serial.print("\t Y: "); 142 | Serial.println(yValue); 143 | } 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | --------------------------------------------------------------------------------