Skip to content

Commit a406a10

Browse files
authored
Merge pull request #31 from arduino/jacobhylen/add-2tutorials
Added 2 tutorials
2 parents ac68dd5 + a79d110 commit a406a10

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Loading
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
author: 'Arduino'
3+
description: 'This tutorial shows you how to connect a joystick to an Arduino.'
4+
title: 'Interfacing a Joystick'
5+
tags: [Joystick, Potentiometer]
6+
---
7+
## Introduction
8+
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.
9+
10+
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.
11+
12+
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.
13+
14+
Finally we make the LED blink with the values read from the sensors as a direct visual feedback of how we control the joystick.
15+
## Hardware Required
16+
- Arduino Board ([Link to store](https://store.arduino.cc/))
17+
- Joystick
18+
19+
20+
## Circuit
21+
![Circuit schematic](./assets/joy_sch_480.png)
22+
23+
24+
## Code
25+
```arduino
26+
/* Read Joystick
27+
* ------------
28+
*
29+
* Reads two analog pins that are supposed to be
30+
* connected to a joystick made of two potentiometers
31+
*
32+
* We send three bytes back to the comp: one header and two
33+
* with data as signed bytes, this will take the form:
34+
* Jxy\r\n
35+
*
36+
* x and y are integers and sent in ASCII
37+
*
38+
* http://www.0j0.org | http://arduino.berlios.de
39+
* copyleft 2005 DojoDave for DojoCorp
40+
*/
41+
42+
int ledPin = 13;
43+
int joyPin1 = A0; // slider variable connected to analog pin 0
44+
int joyPin2 = A1; // slider variable connected to analog pin 1
45+
int value1 = 0; // variable to read the value from the analog pin 0
46+
int value2 = 0; // variable to read the value from the analog pin 1
47+
48+
void setup() {
49+
pinMode(ledPin, OUTPUT); // initializes digital pins 0 to 7 as outputs
50+
Serial.begin(9600);
51+
}
52+
53+
int treatValue(int data) {
54+
return (data * 9 / 1024) + 48;
55+
}
56+
57+
void loop() {
58+
// reads the value of the variable resistor
59+
value1 = analogRead(joyPin1);
60+
// this small pause is needed between reading
61+
// analog pins, otherwise we get the same value twice
62+
delay(100);
63+
// reads the value of the variable resistor
64+
value2 = analogRead(joyPin2);
65+
66+
digitalWrite(ledPin, HIGH);
67+
delay(value1);
68+
digitalWrite(ledPin, LOW);
69+
delay(value2);
70+
Serial.print('J');
71+
Serial.print(treatValue(value1));
72+
Serial.println(treatValue(value2));
73+
}
74+
```
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
author: 'Arduino'
3+
description: 'This example demonstrate how to use two pushbuttons and distunguish between them using only one pin on your Arduino'
4+
title: 'Two Switches, One Pin'
5+
tags: [Pushbuttons, Resistors]
6+
---
7+
8+
## Introduction
9+
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.
10+
11+
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.
12+
13+
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.
14+
15+
16+
## Hardware Required
17+
- Arduino Board ([Link to store](https://store.arduino.cc/))
18+
- 2x Pushbutton
19+
- 1x 10K ohm Resistor
20+
- 1x 200K - 1M ohm resistor
21+
22+
## Code
23+
```arduino
24+
/*
25+
* Read_Two_Switches_On_One_Pin
26+
* Read two pushbutton switches or one center-off toggle switch with one Arduino pin
27+
* Paul Badger 2008
28+
* From an idea in Electronic Design News
29+
*
30+
* Exploits the pullup resistors available on each I/O and analog pin
31+
* The idea is that the 200K resistor to ground will cause the input pin to report LOW when the
32+
* (20K) pullup resistor is turned off, but when the pullup resistor is turned on,
33+
* it will overwhelm the 200K resistor and the pin will report HIGH.
34+
*
35+
* Schematic Diagram ( can't believe I drew this funky ascii schematic )
36+
*
37+
*
38+
* +5 V
39+
* |
40+
* \
41+
* /
42+
* \ 10K
43+
* /
44+
* \
45+
* |
46+
* / switch 1 or 1/2 of center-off toggle or slide switch
47+
* /
48+
* |
49+
* digital pin ________+_____________/\/\/\____________ ground
50+
* |
51+
* | 200K to 1M (not critical)
52+
* /
53+
* / switch 2 or 1/2 of center-off toggle or slide switch
54+
* |
55+
* |
56+
* _____
57+
* ___ ground
58+
* _
59+
*
60+
*/
61+
62+
63+
#define swPin 2 // pin for input - note: no semicolon after #define
64+
int stateA, stateB; // variables to store pin states
65+
int sw1, sw2; // variables to represent switch states
66+
67+
void setup()
68+
{
69+
Serial.begin(9600);
70+
}
71+
72+
void loop()
73+
{
74+
digitalWrite(swPin, LOW); // make sure the pullup resistors are off
75+
stateA = digitalRead(swPin);
76+
digitalWrite(swPin, HIGH); // turn on the pullup resistors
77+
stateB = digitalRead(swPin);
78+
79+
if ( stateA == 1 && stateB == 1 ){ // both states HIGH - switch 1 must be pushed
80+
sw1 = 1;
81+
sw2 = 0;
82+
}
83+
else if ( stateA == 0 && stateB == 0 ){ // both states LOW - switch 2 must be pushed
84+
sw1 = 0;
85+
sw2 = 1;
86+
}
87+
else{ // stateA HIGH and stateB LOW
88+
sw1 = 0; // no switches pushed - or center-off toggle in middle position
89+
sw2 = 0;
90+
}
91+
92+
Serial.print(sw1);
93+
Serial.print(" "); // pad some spaces to format print output
94+
Serial.println(sw2);
95+
96+
delay(100);
97+
}
98+
```

0 commit comments

Comments
 (0)