Discuss Scratch
- Discussion Forums
- » Connecting to the Physical World
- » Arduino-controlled arrow keys
- jheetland
- Scratcher
2 posts
Arduino-controlled arrow keys
Hello, everyone,
I am using an Arduino board programmed to send arrow-key keystrokes to run my Scratch game. The Arduino programming is working fine.
My question:
When I program the arrow keys using the brown event blocks (When up arrow pressed, etc.), it works fine.
BUT
When I program the arrow keys using a blue sensing block within an IF block (IF Key <up arrow pressed?>), it doesn't work reliably at all.
Why is this? What is the difference between the two methods?
Thanks!
Jesse
I am using an Arduino board programmed to send arrow-key keystrokes to run my Scratch game. The Arduino programming is working fine.
My question:
When I program the arrow keys using the brown event blocks (When up arrow pressed, etc.), it works fine.
BUT
When I program the arrow keys using a blue sensing block within an IF block (IF Key <up arrow pressed?>), it doesn't work reliably at all.
Why is this? What is the difference between the two methods?
Thanks!
Jesse
- iwotastic
- Scratcher
100+ posts
Arduino-controlled arrow keys
If you are using the Hello, everyone,Keyboard.press() function on your Arduino, you probably have to just have it pressed for longer because < key pressed?> only checks if the key is pressed at that exact instant. If you want it to work correctly, just put a delay(1000) in between the Keyboard.press() and the Keyboard.release(). Another option is to wait for the button to be released and then do a Keyboard.release().
I am using an Arduino board programmed to send arrow-key keystrokes to run my Scratch game. The Arduino programming is working fine.
My question:
When I program the arrow keys using the brown event blocks (When up arrow pressed, etc.), it works fine.
BUT
When I program the arrow keys using a blue sensing block within an IF block (IF Key <up arrow pressed?>), it doesn't work reliably at all.
Why is this? What is the difference between the two methods?
Thanks!
Jesse
Edit: It'd be cool to see the sketch (and project) once you're done since I have an Arduino too
Last edited by iwotastic (Aug. 28, 2017 01:33:42)
- jheetland
- Scratcher
2 posts
Arduino-controlled arrow keys
Here is the sketch for the Arduino arrow keys. It is based on the capacitive_sense_library_demo, since I thought it would be cooler to set it up as a capacitive touch sensor (i.e. square of aluminum foil!) rather than a button completing a circuit.
I will try your edits and see if they work. Thanks!
(Basic setup is for each pair of pins (2&3, 4&5, etc.) there is a 100K ohm resistor, leading out from that resistor is another wire taped to a piece of foil. When you touch the foil, the capacitance changes. The sketch includes serial printing of the values of each of the four sensors.)
/*
* CapacitiveSense Library Demo Sketch
* Paul Badger 2008
* Uses a high value resistor e.g. 10M between send pin and receive pin
* Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
* Receive pin is the sensor pin - try different amounts of foil/metal on this pin
CapacitiveSense.h - Capacitive Sensing Library for 'duino / Wiring
https://github.com/PaulStoffregen/CapacitiveSensor
http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html
http://playground.arduino.cc/Main/CapacitiveSensor
Copyright © 2009 Paul Bagder
*/
// Edited toward using this as a KB/mouse controller by Jesse Heetland
#include <CapacitiveSensor.h>
#include “Keyboard.h”
#include “Mouse.h” // if you later want to use this sketch for mouse control
// The following functions do…something I don't quite understand yet
CapacitiveSensor cs_2_3 = CapacitiveSensor(2,3); // Used for up
CapacitiveSensor cs_4_5 = CapacitiveSensor(4,5); // Used for down
CapacitiveSensor cs_6_7 = CapacitiveSensor(6,7); // Used for left
CapacitiveSensor cs_8_9 = CapacitiveSensor(8,9); // Used for right
// Declare variables
long sensor_reading_up;
long sensor_reading_down;
long sensor_reading_left;
long sensor_reading_right;
int not_touched_value = 10; // 10 is a good number to act as a “not touched” value, chosen by looking at the serial output
void setup()
{
Serial.begin(9600); // Open serial port for outputting data
Keyboard.begin(); // Needed to allow outputting keyboard characters
// Mouse.begin(); // This would be needed if you are going to use this program for mouse control
}
void loop()
{
sensor_reading_up = cs_2_3.capacitiveSensor(30);
sensor_reading_down = cs_4_5.capacitiveSensor(30);
sensor_reading_left = cs_6_7.capacitiveSensor(30);
sensor_reading_right = cs_8_9.capacitiveSensor(30);
Serial.print(sensor_reading_up); // This prints the values of the up sensor
Serial.print(“\t”); // tab
Serial.print(sensor_reading_down); // This prints the values of the down sensor
Serial.print(“\t”); // tab
Serial.print(sensor_reading_left); // This prints the values of the left sensor
Serial.print(“\t”); // tab
Serial.print(sensor_reading_right); // This prints the values of the right sensor
Serial.print(“\n”); // new line
// HERE IS THE SECTION WHERE IT SENDS THE BUTTON PRESSES:
if (sensor_reading_up > not_touched_value) {
Keyboard.press(218); // up button
}
if (sensor_reading_down > not_touched_value) {
Keyboard.press(217); // down button
}
if (sensor_reading_left > not_touched_value) {
Keyboard.press(216); // left button
}
if (sensor_reading_right > not_touched_value) {
Keyboard.press(215); // right button
}
Keyboard.releaseAll(); // quit pressing whatever key was just pressed
delay(10); // 10 ms arbitrary delay to limit data to serial port
}
I will try your edits and see if they work. Thanks!
(Basic setup is for each pair of pins (2&3, 4&5, etc.) there is a 100K ohm resistor, leading out from that resistor is another wire taped to a piece of foil. When you touch the foil, the capacitance changes. The sketch includes serial printing of the values of each of the four sensors.)
/*
* CapacitiveSense Library Demo Sketch
* Paul Badger 2008
* Uses a high value resistor e.g. 10M between send pin and receive pin
* Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
* Receive pin is the sensor pin - try different amounts of foil/metal on this pin
CapacitiveSense.h - Capacitive Sensing Library for 'duino / Wiring
https://github.com/PaulStoffregen/CapacitiveSensor
http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html
http://playground.arduino.cc/Main/CapacitiveSensor
Copyright © 2009 Paul Bagder
*/
// Edited toward using this as a KB/mouse controller by Jesse Heetland
#include <CapacitiveSensor.h>
#include “Keyboard.h”
#include “Mouse.h” // if you later want to use this sketch for mouse control
// The following functions do…something I don't quite understand yet
CapacitiveSensor cs_2_3 = CapacitiveSensor(2,3); // Used for up
CapacitiveSensor cs_4_5 = CapacitiveSensor(4,5); // Used for down
CapacitiveSensor cs_6_7 = CapacitiveSensor(6,7); // Used for left
CapacitiveSensor cs_8_9 = CapacitiveSensor(8,9); // Used for right
// Declare variables
long sensor_reading_up;
long sensor_reading_down;
long sensor_reading_left;
long sensor_reading_right;
int not_touched_value = 10; // 10 is a good number to act as a “not touched” value, chosen by looking at the serial output
void setup()
{
Serial.begin(9600); // Open serial port for outputting data
Keyboard.begin(); // Needed to allow outputting keyboard characters
// Mouse.begin(); // This would be needed if you are going to use this program for mouse control
}
void loop()
{
sensor_reading_up = cs_2_3.capacitiveSensor(30);
sensor_reading_down = cs_4_5.capacitiveSensor(30);
sensor_reading_left = cs_6_7.capacitiveSensor(30);
sensor_reading_right = cs_8_9.capacitiveSensor(30);
Serial.print(sensor_reading_up); // This prints the values of the up sensor
Serial.print(“\t”); // tab
Serial.print(sensor_reading_down); // This prints the values of the down sensor
Serial.print(“\t”); // tab
Serial.print(sensor_reading_left); // This prints the values of the left sensor
Serial.print(“\t”); // tab
Serial.print(sensor_reading_right); // This prints the values of the right sensor
Serial.print(“\n”); // new line
// HERE IS THE SECTION WHERE IT SENDS THE BUTTON PRESSES:
if (sensor_reading_up > not_touched_value) {
Keyboard.press(218); // up button
}
if (sensor_reading_down > not_touched_value) {
Keyboard.press(217); // down button
}
if (sensor_reading_left > not_touched_value) {
Keyboard.press(216); // left button
}
if (sensor_reading_right > not_touched_value) {
Keyboard.press(215); // right button
}
Keyboard.releaseAll(); // quit pressing whatever key was just pressed
delay(10); // 10 ms arbitrary delay to limit data to serial port
}
- Discussion Forums
- » Connecting to the Physical World
- » Arduino-controlled arrow keys