Skip to content

Added 2 tutorials #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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));
}
```
Original file line number Diff line number Diff line change
@@ -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);
}
```