I am a little fustrated so would love it if someone could help. I have been trying to write my second Sketch. Let me give you an outline as to what i am trying to do. We have a controller (from a locomotive) and want to hook it up to a computer for use in a train simulator. At the minute I am just concentrating on the throttle. in the game you would just use the A and D keys to increase/decrease the throttle but want movement of the controller to represent what key to push.
on every iteration of the loop if needs to know if the pot changed and what way it was turned to know what key to fire, it must then fire that key and wait until there's another change. is there a way of putting a buffer around the value .... eg a small movement (say a hand resting on a lever which might move the pot slightly) can be ignored. Also I wonder am i doing this the right way. I watch the serial window and sometimes I'm doing nothing and i get the "somethings changed" message as per my sketch and yet nothin has. What i have worked out is as i decrease my delay times it gets vary scary as very soon its writing all over my sketch and then becomes quite hard to get a newer or updated sketch back onto the leonardo because its just spamming me which A's or D's. I thought perhaps the best thing to do i ask and see if anyone has done this type of think and can share what they have done or how i might improve what i have done. Any help would be appreciated.
#include <Keyboard.h>
#include <KeyboardLayout.h>
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
int OldsensorValue = 0;
delay(10000);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// declare variable
// int oldSV;
// print out the value you read:
// Serial.println(sensorValue);
if(analogRead(A0) != sensorValue) {
// somethings changed
Serial.print("somethingschanged\n");
if(analogRead(A0) > sensorValue)
{
// run trigger upfunction
Serial.print("UP");
triggerUP();
}
if(analogRead(A0) < sensorValue)
{
// run trigger downfunction
Serial.print("DOWN");
triggerDown();
}
// do nothing
}
// OldsensorValue = sensorValue;
Serial.println(sensorValue);
Serial.print("Hello\n");
delay(400); // delay in between reads for stability
}
// Functions used in main loop
void triggerUP() {
Keyboard.press('a'); // Press and hold the 'r' key.
delay(120); // Wait for the computer to register the press.
Keyboard.release('a'); // Release both of the above keys.
}
void triggerDown() {
Keyboard.press('d'); // Press and hold the 'r' key.
delay(120); // Wait for the computer to register the press.
Keyboard.release('d'); // Release the key.
}
My first post here so if i have done something wrong forgive me.
#include <Keyboard.h>
#include <KeyboardLayout.h>
int OldsensorValue = 0;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
delay(10000);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// declare variable
// int oldSV;
// print out the value you read:
// Serial.println(sensorValue);
if(abs(sensorValue - OldsensorValue) > 10) {
// somethings changed
Serial.print("somethingschanged\n");
if(sensorValue > OldsensorValue)
{
// run trigger upfunction
Serial.print("UP");
triggerUP();
}
else
{
// run trigger downfunction
Serial.print("DOWN");
triggerDown();
}
OldsensorValue = sensorValue;
}
Serial.println(sensorValue);
Serial.print("Hello\n");
delay(400); // delay in between reads for stability
}
// Functions used in main loop
void triggerUP() {
Keyboard.press('a'); // Press and hold the 'r' key.
delay(120); // Wait for the computer to register the press.
Keyboard.release('a'); // Release both of the above keys.
}
void triggerDown() {
Keyboard.press('d'); // Press and hold the 'r' key.
delay(120); // Wait for the computer to register the press.
Keyboard.release('d'); // Release the key.
}
Please Auto-Format your code before you post it again.
Paul, I had some trouble .. while alot of then saw you edited your post, let me play with that but that's already an improvement! Alot less noise (if that's what i call it). Cheers
Paul and or anyone .. so have have spent a lot more time improving and testing my sketch but now another problem i am not sure how to fix. When i power up always without touching a thing i will get an A Keypress I suspect digitalWrite(A0, HIGH); or Low might have changed things but it doesn't seem to go away .. failing that's its actually pretty good start for any noob.
#include <Keyboard.h>
#include <KeyboardLayout.h>
// not all arduinos support the keyboard library.
/*
AnalogReadSerial
Reads an analog input on pin A0, prints the result to the Serial Monitor.
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.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogReadSerial
*/
// my stuff
//
int OldsensorValue = 0;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
delay(20000); // i have use a 20 sec power up in the hope of stopping it from spamming the computer just in case it needs rewriting or tried to.
digitalWrite(A0, LOW); // trying to get rid of the initial A which is activated on first power up.
}
// ** The loop routine runs over and over again forever: be careful as you might create a keyboard spammer which is very hard to stop / reprogram.
// keypresses outside main loop ** thats important i think.
void loop() {
// read the input on analog pin 0: actual numbers are : 0 to 935 from my potentiometer.
int sensorValue = analogRead(A0); // declare variables
// int oldSenval; not used. just helps my brain? should
// print out the value you read:
// Serial.println(sensorValue);
if (abs(sensorValue - OldsensorValue) > 15) {
// somethings changed
Serial.print("Something has changed\n");
if (sensorValue > OldsensorValue) {
// run trigger upfunction
Serial.print("UP\n");
triggerUP();
} else {
// run trigger downfunction
Serial.print("DOWN\n");
triggerDown();
}
OldsensorValue = sensorValue;
}
// output to the serial port for now
Serial.println(sensorValue); // the sensorval
Serial.print("Hello\n");
delay(400); // delay in between reads for stability
}
// Functions/keypresses called from main repetitive loop
void triggerUP() {
Keyboard.press('a'); // Press and hold the 'a' key.
delay(170); // Wait for the computer to register the press. I have used 150ms based on me hitting keyboard keys (my avg was 180ms.) (https://github.com/undoLogic/SimuCheckToolSet) d/l: SimuCheck-Keyboard-detect.exe
Keyboard.release('a'); // Release a key.
delay(450); // lets see if we can stop what i am calling double presses REM out to see effect.
}
void triggerDown() {
Keyboard.press('d'); // Press and hold the 'd' key.
delay(170); // Wait for the computer to register the press.
Keyboard.release('d'); // Release d key.
delay(450); // lets see if we can stop what i am calling double presses or noise as the selector settles into its notches.
}
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
delay(20000); // i have use a 20 sec power up in the hope of stopping it from spamming the computer just in case it needs rewriting or tried to.
Keyboard.print('a'); // to get rid of the initial A which is activated on first power up.
}
Firstly Thank you, you sorted out a couple of things that I struggled a bit with and specifically that's the comparison and use of 'OldsensorValue' as the loop iterates. With your help and the A thing which happens when power reset. I'm gonna say mission complete? some of the delays might need a little more tuning and i am yet to try develop this into 4 pots and see how that goes. You're honestly a legend!
From my first post I'd like to think this project could be helpful to someone else in the future ... basically use a pot ... and DIY, and Train sims. Maybe today some 3d printed levers and away you go. I cannot tell you the hours i have spent looking for solutions to do this. To share what i have learned was also an objective and what better company