diff --git a/content/tutorials/generic/interfacing-a-joystick/assets/joy_sch_480.png b/content/tutorials/generic/interfacing-a-joystick/assets/joy_sch_480.png new file mode 100644 index 0000000000..3b5c70d02a Binary files /dev/null and b/content/tutorials/generic/interfacing-a-joystick/assets/joy_sch_480.png differ diff --git a/content/tutorials/generic/interfacing-a-joystick/interfacing-a-joystick.md b/content/tutorials/generic/interfacing-a-joystick/interfacing-a-joystick.md new file mode 100644 index 0000000000..6f0ee92c06 --- /dev/null +++ b/content/tutorials/generic/interfacing-a-joystick/interfacing-a-joystick.md @@ -0,0 +1,74 @@ +--- +author: 'Arduino' +description: 'This tutorial shows you how to connect a joystick to an Arduino.' +title: 'Interfacing a Joystick' +tags: [Joystick, Potentiometer] +--- +## Introduction +The typical joystick is nothing but two potentiometers that allow us to measure the movement of the stick in 2-D. Potentiometers are variable resistors and, in a way, they act as sensors providing us with a variable voltage depending on the rotation of the device around its shaft. + +The kind of program that we need to monitor the joystick has to make a polling to two of the analog pins. We can send these values back to the computer, but then we face the classic problem that the transmission over the communication port has to be made with 8bit values, while our DAC (Digital to Analog Converter - that is measuring the values from the potentiometers in the joystick) has a resolution of 10bits. In other words this means that our sensors are characterized with a value between 0 and 1024. + +The following code includes a method called treatValue() that is transforming the sensor's measurement into a value between 0 and 9 and sends it in ASCII back to the computer. This allows to easily send the information into e.g. Flash and parse it inside your own code. + +Finally we make the LED blink with the values read from the sensors as a direct visual feedback of how we control the joystick. +## Hardware Required +- Arduino Board ([Link to store](https://store.arduino.cc/)) +- Joystick + + +## Circuit +![Circuit schematic](./assets/joy_sch_480.png) + + +## Code +```arduino +/* Read Joystick + * ------------ + * + * Reads two analog pins that are supposed to be + * connected to a joystick made of two potentiometers + * + * We send three bytes back to the comp: one header and two + * with data as signed bytes, this will take the form: + * Jxy\r\n + * + * x and y are integers and sent in ASCII + * + * http://www.0j0.org | http://arduino.berlios.de + * copyleft 2005 DojoDave for DojoCorp + */ + + int ledPin = 13; + int joyPin1 = A0; // slider variable connected to analog pin 0 + int joyPin2 = A1; // slider variable connected to analog pin 1 + int value1 = 0; // variable to read the value from the analog pin 0 + int value2 = 0; // variable to read the value from the analog pin 1 + + void setup() { + pinMode(ledPin, OUTPUT); // initializes digital pins 0 to 7 as outputs + Serial.begin(9600); + } + + int treatValue(int data) { + return (data * 9 / 1024) + 48; + } + + void loop() { + // reads the value of the variable resistor + value1 = analogRead(joyPin1); + // this small pause is needed between reading + // analog pins, otherwise we get the same value twice + delay(100); + // reads the value of the variable resistor + value2 = analogRead(joyPin2); + + digitalWrite(ledPin, HIGH); + delay(value1); + digitalWrite(ledPin, LOW); + delay(value2); + Serial.print('J'); + Serial.print(treatValue(value1)); + Serial.println(treatValue(value2)); + } + ``` \ No newline at end of file diff --git a/content/tutorials/generic/two-switches-one-pin/two-switches-one-pin.md b/content/tutorials/generic/two-switches-one-pin/two-switches-one-pin.md new file mode 100644 index 0000000000..d2d960f1dd --- /dev/null +++ b/content/tutorials/generic/two-switches-one-pin/two-switches-one-pin.md @@ -0,0 +1,98 @@ +--- +author: 'Arduino' +description: 'This example demonstrate how to use two pushbuttons and distunguish between them using only one pin on your Arduino' +title: 'Two Switches, One Pin' +tags: [Pushbuttons, Resistors] +--- + +## Introduction +There are handy 20K pullup resistors (resistors connected internally between Arduino I/O pins and VCC - +5 volts in the Arduino's case) built into the Atmega chip upon which Arduino boards are based. They are accessible from software by using the digitalWrite() function, when the pin is set to an input. + +This sketch exploits the pullup resistors under software control. The idea is that an external 200K resistor to ground will cause an input pin to report LOW when the internal (20K) pullup resistor is turned off. When the internal pullup resistor is turned on however, it will overwhelm the external 200K resistor and the pin will report HIGH. + +One downside of the scheme (there always has to be a downside doesn't there?) is that one can't tell if both buttons are pushed at the same time. In this case the scheme just reports that sw2 is pushed. The job of the 10K series resistor, incidentally, is to prevent a short circuit if a pesky user pushes both buttons at once. It can be omitted on a center-off slide or toggle switch where the states are mutually exclusive. + + +## Hardware Required +- Arduino Board ([Link to store](https://store.arduino.cc/)) +- 2x Pushbutton +- 1x 10K ohm Resistor +- 1x 200K - 1M ohm resistor + +## Code +```arduino +/* + * Read_Two_Switches_On_One_Pin + * Read two pushbutton switches or one center-off toggle switch with one Arduino pin + * Paul Badger 2008 + * From an idea in Electronic Design News + * + * Exploits the pullup resistors available on each I/O and analog pin + * The idea is that the 200K resistor to ground will cause the input pin to report LOW when the + * (20K) pullup resistor is turned off, but when the pullup resistor is turned on, + * it will overwhelm the 200K resistor and the pin will report HIGH. + * + * Schematic Diagram ( can't believe I drew this funky ascii schematic ) + * + * + * +5 V + * | + * \ + * / + * \ 10K + * / + * \ + * | + * / switch 1 or 1/2 of center-off toggle or slide switch + * / + * | + * digital pin ________+_____________/\/\/\____________ ground + * | + * | 200K to 1M (not critical) + * / + * / switch 2 or 1/2 of center-off toggle or slide switch + * | + * | + * _____ + * ___ ground + * _ + * + */ + + +#define swPin 2 // pin for input - note: no semicolon after #define +int stateA, stateB; // variables to store pin states +int sw1, sw2; // variables to represent switch states + +void setup() +{ + Serial.begin(9600); +} + +void loop() +{ + digitalWrite(swPin, LOW); // make sure the pullup resistors are off + stateA = digitalRead(swPin); + digitalWrite(swPin, HIGH); // turn on the pullup resistors + stateB = digitalRead(swPin); + + if ( stateA == 1 && stateB == 1 ){ // both states HIGH - switch 1 must be pushed + sw1 = 1; + sw2 = 0; + } + else if ( stateA == 0 && stateB == 0 ){ // both states LOW - switch 2 must be pushed + sw1 = 0; + sw2 = 1; + } + else{ // stateA HIGH and stateB LOW + sw1 = 0; // no switches pushed - or center-off toggle in middle position + sw2 = 0; + } + + Serial.print(sw1); + Serial.print(" "); // pad some spaces to format print output + Serial.println(sw2); + + delay(100); +} +``` \ No newline at end of file