I want to keyboardPrint a "+" or "-" when my 6 position (5 + off) lever goes up or down. How can I do that?
My Lever is a build-in potentiometer. I know both the ohms resistance values when not connected to power and I know the voltage outputs when connected to 5v on the Arduino so I can use either the Voltage Reads or the Resistance reeds to determine the current handle position.
Now these handlePos are in the Arduino memory, I need:
When the lever goes DOWN one position, I need one HID keyboardPrint("+");
When the lever goes DOWN two positions, I need two HID keyboardPrint("+");s When the lever goes DOWN three positions, I need three HID keyboardPrint("+");s
When the lever goes DOWN four positions, I need four HID keyboardPrint("+");s When the lever goes DOWN five positions, I need five HID keyboardPrint("+");s
When the lever goes UP one position, I need one HID keyboardPrint("-")
When the lever goes UP two positions, I need two HID keyboardPrint("-");s When the lever goes UP three positions, I need three HID keyboardPrint("-");s
When the lever goes UP four positions, I need four HID keyboardPrint("-");s When the lever goes UP five positions, I need five HID keyboardPrint("-");s
When the lever is in position 3, and I move the lever to position five, I need two HID keyboardPrint("+");
Do you know how to connect a potentiometer and read the value that it returns ? If so then all you need to do is read the value, determine the range that it is in and print the appropriate message. As a start simply print the value returned by analogRead() then add the range tests.
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1000); // delay in between reads for stability
}
Where I connected PIN 2 to GROUND and PIN 6 to 5v and PIN 7 to A0. The read is always 0 zo I guess my unit doesn't work with 5v (Truck is 24v) or I connected them wrong.
I will try this tutorial as soon as I received new solder thin and post the result. My ultimate goals would be to do an if/else and OUTPUT a keyboard character (HID).
Then you need one of the Arduinos with native USB to do HID output. My favourite is the Micro. The Teensy is also good. Leonardo and Due can do it too.
The ohm meter tutorial is not appropriate. Just connect the 0-5V signal to any analog pin. Don't forget to connect the grounds.
MorganS:
Then you need one of the Arduinos with native USB to do HID output. My favourite is the Micro. The Teensy is also good. Leonardo and Due can do it too.
The ohm meter tutorial is not appropriate. Just connect the 0-5V signal to any analog pin. Don't forget to connect the grounds.
I got both Leonardo and Pro Micro (my fav) on the shelf. Can you please give me more details on "connect 0-5v signal to the analog pins"? Sorry Im such a noob but I dont want to burn my Arduinos like I did once already
Hi,
Have you got a DMM, you will need one.
The spec image you have shown in your first post, shows resistance values for lever position, they are not correct if the graph and the link to the spec sheet are correct.
If your lever is a potential resistive type, check the resistance values between pins 2 and 6 first, it should be a constant value no matter where the lever is.
Then check resistance between 7 and 6 , this value should vary with the lever.
Can you please post your result.
Whether the unit is 24V or 5V, the output of the lever, if it is a pot, will still produce a variable voltage.
The lever could however be a hall effect device, in which case your 5V may not be enough and resistance values make no sense.
TomGeorge:
Hi,
Have you got a DMM, you will need one.
The spec image you have shown in your first post, shows resistance values for lever position, they are not correct if the graph and the link to the spec sheet are correct.
If your lever is a potential resistive type, check the resistance values between pins 2 and 6 first, it should be a constant value no matter where the lever is.
Then check resistance between 7 and 6 , this value should vary with the lever.
Can you please post your result.
Whether the unit is 24V or 5V, the output of the lever, if it is a pot, will still produce a variable voltage.
The lever could however be a hall effect device, in which case your 5V may not be enough and resistance values make no sense.
Tom....
Hi Tom,
1# Yes I got a simple DMM, the values in the first picture are measured by my DMM. I measured again this morning, see the results below.
2# Graphs are from the official Workshop Manuals. The separate picture from my first post is made by me, I put the Resistance Values there. These Values are not described in the workshop manuals, they just describe voltage differences which is common for Retarder Handles because most of them are Hall Effect Devices.
DMM results PIN 2 and 6:
handle pos 0 = 0 ohm (I do get a weird reading of an inch of a second between 500 and 1950 ohm)
handle pos 1 = 1633 ohm
handle pos 2 = 1006 ohm
handle pos 3 = 728 ohm
handle pos 4 = 589 ohm
handle pos 5 = 487 ohm
So I guess I should be able to keep it a two wire config and with my Arduino Leonardo or Pro Micro I should be able to OUTPUT a key character based upon these resistances right?
Could you guys please help me adjust the code above? I want my code to (Keyboard.print("a"); when the handle goes up and (Keyboard.print("b"); when the lever goes down.
The code should (Keyboard.print); the right amounts of as en bs according to the lever position. So if the Lever is in position "0", and it moves to position "5", I need five (Keyboard.print("b");.
MorganS:
Well, the key idea you need is identifying changes. Remember where the lever is and only output the keys when it moves to a new position.
If I manage to do this:
IF Lever is in Position 0, THEN Serial.println(Position 0);
IF Lever is in Position 1, THEN Serial.println(Position 1);
IF Lever is in Position 2, THEN Serial.println(Position 2);
IF Lever is in Position 3, THEN Serial.println(Position 3);
IF Lever is in Position 4, THEN Serial.println(Position 4);
IF Lever is in Position 5, THEN Serial.println(Position 5);
Not quite. That will print a never ending stream of repetitive information. Above all that, you need to check that the current position is different to the previousposition. At the bottom, save the current position into the variable called previousposition.
Does that mean 0 Ohms or open circuit? The instructions say " measure voltage between pin 7 and earth", which pin is connected to earth? With handle in position 3 what is the resistance between pins 7 and 6? 7 and 2? 2 and 6?
outsider:
You say:Does that mean 0 Ohms or open circuit? The instructions say " measure voltage between pin 7 and earth", which pin is connected to earth?
Yes 0 ohms is an open circuit. According to the scheme below PIN5 is Black. So far all wiring black is GROUND.
outsider:
With handle in position 3 what is the resistance between pins 7 and 6? 7 and 2? 2 and 6?
Pin 7 and 6 (handle pos 3) is 430 ohm.
Pin 7 and 2 (handle pos 3) is 301 ohm.
Pin 2 and 6 (handle pos 3) is 729 ohm.
Well don`t worry about it to much. We can write code (IF resistance drops below 10 ohm, Lever POS = 0).
So I need to figure out how to store the value in the Arduino. I guess that would be something like this:
const int analogPin0 = A0; // pin that the sensor is attached to
const int ledPin = 13; // pin that the LED is attached to
const int threshold0 = 10; // an arbitrary threshold level that's in the range of the analog input
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin0);
// if the analog value is high enough, turn on the LED:
if (analogValue > threshold0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// print the analog value:
Serial.println ("analogValue 0 =");
Serial.println(analogValue);
delay(5000); // delay in between reads for stability
}
This code will check A0 and make LED 13 go ON when the value is higher than 10 ohm and serialPrint a message which contains the A0 Value. The problem with this code is that it will be a loop. Every 5000 / 5 seconds (delay) it will send this message. If I put in a keyboardPrint "a" it will send an "a" to my simulator every 5 seconds and that`s not what I need. I need code that waits for a change of analogValue.
jeroenV1982:
I need code that waits for a change of analogValue.
So you record the analog value and see whether it has changed. But you have to decide, changed by how much? Because there is a bit of noise in the analogue reading. You have to implement hysteresis, or a "guard zone" to allow a little bit of change without a response.
No, 0 Ohms is a closed circuit, I was trying to find out what you meant by "no reading" between pins 2 & 7.
Anyway, looks like you have 430 Ohms between pin 7 and one end of the pot and 301 from pin 7 to the other end, so the total pot resistance is about 730 between pins 2 & 6 (you measured 729).
So, you should be able to put 5V on pin 2 or 6, GND on the other (6 or 2) and measure a gradually increasing or decreasing voltage between pin 7 & GND as you move the handle, can you do that?
outsider:
No, 0 Ohms is a closed circuit, I was trying to find out what you meant by "no reading" between pins 2 & 7.
Anyway, looks like you have 430 Ohms between pin 7 and one end of the pot and 301 from pin 7 to the other end, so the total pot resistance is about 730 between pins 2 & 6 (you measured 729).
So, you should be able to put 5V on pin 2 or 6, GND on the other (6 or 2) and measure a gradually increasing or decreasing voltage between pin 7 & GND as you move the handle, can you do that?
I did!
Results:`
With 5v connected to PIN 2 and 6 (PIN 2 +) (PIN 6 -):
So yes, this is how the original part is meant to work, we found it!
So next big question;
How can I use these result to keyboardPrint letter "a" when the lever goes down and keyboardPrint letter "b" when it goes up without repeating the keyboardPrint until the next position change?