I want to keyboardPrint (HID) a letter based upon voltage or resistance change in Analog input A0. Does anyone know how to achieve this without the code ending in a repeated keyboardPrint? I just want to send a letter once after a change and wait for another change before sending a letter again.
I want:
When the lever goes UP, keyboardPrint “a”
When the lever goes DOWN, keyboardPrint “b”
int sensorPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
int storeValue = 0;
void setup() {
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
if (storeValue == sensorValue) {
storeValue = sensorValue;
}
else {
//your code here
}
}
int getAnalog()
{
val = analogRead(0);
if(val < 187) handlePos = 0;
else if(val < 350) handlePos = 1;
else if(val < 518) handlePos = 2;
else if(val < 679) handlePos = 3;
else if(val < 861) handlePos = 4;
else handlePos = 5;
return val;
}
// by Jeroen van der Velden
// https://hackaday.io/project/8448-real-dashboard-truck-simulator
// Credits to Arduino Forum member “OUTSIDER”[/code]
I guess I need something like this;
IF handlepos is = 1 and previous handlepos = 4 THEN keyboardPrint("-") three times)
IF handlepos is = 5 and previous handlepos = 0 THEN keyboardPrint("+") five times)
The code below will now print - and + depending on direction movement of the Retarder Lever (up or down) and if it goes up or down multiple positions at once it will calculate and send the appropriate - and + characters to your Simulator.
Topic may close now…
[code]unsigned long startTime, endTime = 1000;
byte handlePos, oldHandlePos, currentPos;
int8_t diff;
int val;
bool posSent = true;
void setup()
{
Serial.begin(9600);
val = getAnalog();
Serial.print("\nCurrent handle position = “);
Serial.print(handlePos);
Serial.println(”\n\n");
currentPos = oldHandlePos = handlePos;
}
// if diff is negative number to positive number
if(diff < 0)
{
for (int i = 0; i < (diff * -1); i++)
{
Keyboard.print("-");
}
}
// if diff is positive number to positive number
if(diff > 0)
{
for (int i = 0; i < diff; i++)
{
Keyboard.print("+");
}
}
oldHandlePos = handlePos;
posSent = true;
}