Hi guys I'm currently working on a project where I'm trying to convert a typewriter into a keyboard, I'm using a due.
I was planning on attaching tilt sensors to the underside of each lever on the typewriter so when it's pressed down it types a letter depending on the lever pressed. However I'm having a little trouble with the coding, I currently only have one tilt sensor though. When I'm using the code below it continually prints streams of A's at a rate of around 1/sec then when it is active it prints them even quicker.
/Users/john/Documents/Arduino/sketch_sep22a/sketch_sep22a.ino: In function 'void loop()':
sketch_sep22a:17: error: expected primary-expression before '==' token
if (LOW) == digitalVal)
^
exit status 1
How is your tilt switch wired?
Did you put an external pull-up or pull-down resistor on your input pin?
Do you want to output a stream of characters while the A lever IS pressed? Or do you want to output one character when the A lever BECOMES pressed (or released)? If you want one character when the lever changes position, see the state change detection example.
Apologies for the delay in response, I realise you probably won't see this but figured I owed you it.
Paul, I would like one character when the lever is pressed rather than a stream. I've had a look at the state change detection example and that's very uesful. I could definitely use something similar to what they've done. I'll have a go at coding that tomorrow.
John, I made an error when creating that code, it should have been made to be an input pullup on the pin, that I used in a different code. I'll recreate it shortly and post an update of where I'm at.
You're missing {} for the if. Only the delay(500) is conditionally executed if the pin is LOW hence slowing down the process. The three lines after that are always executed regardless if the condition is true or false.
sterretje:
You're missing {} for the if. Only the delay(500) is conditionally executed if the pin is LOW hence slowing down the process. The three lines after that are always executed regardless if the condition is true or false.
I've fixed the code, as seen below and this seems to work well.
I've attempted to edit the example for state edge detection using a tilt switch instead of a button, only it seems to print out streams and streams rapidly that the button state is changing state on the serial monitor. Does anyone know how I could fix this?
const int buttonPin = 2; // the pin that the pushbutton is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
How, EXACTLY, is the switch wired? The internal pullup resistors are not being used; God only knows why not. So, you MUST have external pullup or pulldown resistors. What value, and how are they wired?
PaulS:
How, EXACTLY, is the switch wired? The internal pullup resistors are not being used; God only knows why not. So, you MUST have external pullup or pulldown resistors. What value, and how are they wired?
I'm not using any resistors... I've just got the tilt switch connected to pin 2 and ground.
I'm new to electronics and my knowledge is very limited, should I be using resistors here?