【树莓派+arduino实验记录11 】操纵杆传感器
2020-08-19 本文已影响0人
Geekero
Arduino
/*******************************************************
* name:Joystick PS2
* function:push the joystick and the coordinates of X and Y axes displayed on Serial Monitor will change accordingly;
* press down the joystick, and the coordingly of Z=0 will also be displayed.
* connection:
* Joystick PS2 Arduino Uno R3
* GND GND
* VCC 5V
* SW 7
* x A0
* Y A1
* ******************************************************/
const int xPin = A0; //X attach to A0
const int yPin = A1; //Y attach to A1
const int btPin = 7; //Bt attach to digital 7
void setup()
{
pinMode(btPin, INPUT);
digitalWrite(btPin, HIGH);
Serial.begin(9600);
}
void loop()
{
Serial.print("X: ");
Serial.print(analogRead(xPin), DEC); //read the value of A0 and print it in decimal
Serial.print("\tY: ");
Serial.print(analogRead(yPin), DEC);
Serial.print("\tZ: ");
Serial.println(digitalRead(btPin));
delay(100);//100ms
}
树莓派
C
#include <stdio.h>
#include <wiringPi.h>
#include <pcf8591.h>
#define PCF 120
#define uchar unsigned char
int AIN0 = PCF + 0;
int AIN1 = PCF + 1;
int AIN2 = PCF + 2;
//char *state[7] = {"home", "up", "down", "left", "right", "pressed"};
int direction(){
int x, y, b;
int tmp;
x = analogRead(AIN0);
y = analogRead(AIN1);
b = analogRead(AIN2);
if (y == 0)
tmp = 1; //up
if (y == 255)
tmp = 2; //down
if (x == 255)
tmp = 3; //left
if (x == 0)
tmp = 4; //right
if (y>=125 && b==0)
tmp = 5; //button pressed
if (x-125<15 && x-125>-15 && y-125<15 && y-125>-15 && b==255)
tmp = 0; //home position
return tmp;
}
int main(void)
{
int tmp;
int status = 0;
wiringPiSetup();
//Setup pcf8591 on base pin 120, and address 0x48
pcf8591Setup(PCF, 0x48);
while(1) //loop forever
{
tmp = direction();
if (tmp != status)
{
switch(tmp)
{
case 0: printf("home\n");break;
case 1: printf("up\n");break;
case 2: printf("down\n");break;
case 3: printf("left\n");break;
case 4: printf("right\n");break;
case 5: printf("pressed\n");break;
}
status = tmp;
}
}
return 0;
}
Python
#!/usr/bin/env python
#---------------------------------------------------
#
# This is a programe for JoystickPS2 Module.
#
# This program depend on PCF8591 ADC chip.
# Follow the instruction book to connect the module and
# ADC0832 to your Raspberry Pi.
#
#---------------------------------------------------
import PCF8591 as ADC
import time
def setup():
ADC.setup(0x48)
global state
def direction(): #get joystick result
state = ['home', 'up', 'down', 'left', 'right', 'pressed']
i = 0
if ADC.read(0) <= 5:
i = 1 #up
if ADC.read(0) >= 250:
i = 2 #down
if ADC.read(1) >= 250:
i = 3 #left
if ADC.read(1) <= 5:
i = 4 #right
if ADC.read(1)>=250 and ADC.read(2)==0:
i = 5 #button pressed
if ADC.read(0)-125 < 15 and ADC.read(0)-125 > -15 and ADC.read(1)-125 < 15 and ADC.read(1)-125 > -15 and ADC.read(2) == 255:
i = 0 #home
return state[i]
def loop():
status = ''
while True:
tmp = direction()
if tmp != None and tmp != status:
print(tmp)
status = tmp
def destroy():
pass
if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt:
destroy()