Grove - Thumb Joystick
Version från den 4 februari 2021 kl. 07.30 av Christian (diskussion | bidrag)
Thumb Joystick är en Grove-kompatibel modul som mycket liknar den "analoga" joysticken på PS2-kontroller. Joysticken har också en tryckknapp som kan användas för speciella applikationer. När modulen är i arbetsläge, kommer den att mata ut två analoga värden, som representerar två riktningar.
Kompatibilitet
- Arduino
- Raspberry Pi
Port
- Analog
Exempelkod
Arduino
void setup()
{
Serial.begin(9600);
}
void loop()
{
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
Serial.print("The X and Y coordinate is:");
Serial.print(sensorValue1, DEC);
Serial.print(",");
Serial.println(sensorValue2, DEC);
Serial.println(" ");
delay(200);
}
Raspberry Pi
import time
import grovepi
# Connect the Grove Thumb Joystick to analog port A0
# GrovePi Port A0 uses Arduino pins 0 and 1
# GrovePi Port A1 uses Arduino pins 1 and 2
# Don't plug anything into port A1 that uses pin 1
# Most Grove sensors only use 3 of their 4 pins, which is why the GrovePi shares Arduino pins between adjacent ports
# If the sensor has a pin definition SIG,NC,VCC,GND, the second (white) pin is not connected to anything
# If you wish to connect two joysticks, use ports A0 and A2 (skip A1)
# Uses two pins - one for the X axis and one for the Y axis
# This configuration means you are using port A0
xPin = 0
yPin = 1
grovepi.pinMode(xPin,"INPUT")
grovepi.pinMode(yPin,"INPUT")
# The Grove Thumb Joystick is an analog device that outputs analog signal ranging from 0 to 1023
# The X and Y axes are two ~10k potentiometers and a momentary push button which shorts the x axis
# My joystick produces slightly different results to the specifications found on the url above
# I've listed both here:
# Specifications
# Min Typ Max Click
# X 206 516 798 1023
# Y 203 507 797
# My Joystick
# Min Typ Max Click
# X 253 513 766 1020-1023
# Y 250 505 769
while True:
try:
# Get X/Y coordinates
x = grovepi.analogRead(xPin)
y = grovepi.analogRead(yPin)
# Calculate X/Y resistance
Rx = (float)(1023 - x) * 10 / x
Ry = (float)(1023 - y) * 10 / y
# Was a click detected on the X axis?
click = 1 if x >= 1020 else 0
print "x =", x, " y =", y, " Rx =", Rx, " Ry =", Ry, " click =", click
time.sleep(.5)
except IOError:
print "Error"