-
Notifications
You must be signed in to change notification settings - Fork 0
/
JoystickSerialFeed.ino
97 lines (93 loc) · 3.61 KB
/
JoystickSerialFeed.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// PROJECT :JoystickSerialFeed
// PURPOSE :High-level reading and Serial println of a Thumb Joystick (2 pots)
// :Support for Processing Circular Functions Demo
// COURSE :ICS3U/ICS4U
// AUTHOR :C. D'Arcy
// DATE :Latest: 2020 03 07
// MCU :DDP(84)/(UNO)328P
// STATUS :Working
// REFERENCE:https://www.instructables.com/id/ATtiny8485-In-circuit-Debugging-Using-Serial-Outpu/
// NOTES :DDP: (0,1,2,3,4)=(VCC,VERT,HORZ,SEL,GROUND)
// :UNO: (14,15,16,17,18)=(VCC,VERT,HORZ,SEL,GROUND)
// :SEL: N/A
//Activate one of the two targets...
//#define LOCAL //Serial Monitor or Plotter
#define PROCESSING //...Arduino/MyProcessing/JoyStickTrig/CircularTrigFunctions.pde
// adjust to which MCU is in play...
#if defined (__AVR_ATtiny84__)
#define VCC 0
#define VERT 1 //ADC1
#define HORZ 2 //ADC2
#define SEL 3 //
#define GND 4 //
#define HORZOUT 6 //(Optional BiColor LED)PWM: OC1A (Pin 5:OC1B inaccessbile)
#define VERTOUT 8 //(Optional BiColor LED)PWM: OC0A (avoided pin 7:OC0B)
#define RXPIN 9 // Pin used for Serial receive
#define TXPIN 10 // Pin used for Serial transmit
#define VERTGND 10 //
#include <SoftwareSerial.h> // Arduino SoftwareSerial class
SoftwareSerial mySerial(RXPIN, TXPIN);
#else #if defined(__AVR_ATmega328P__)
#define VCC 14 //A0
#define VERT 15 //ADC1
#define HORZ 16 //ADC2
#define SEL 17 //ADC3
#define GND 18 //ADC4
#define HORZOUT 11 //(Optional BiColor LED) PWM: OC2A (Pin 11)
#define VERTOUT 9 //(Optional BiColor LED) PWM: OC1A (Pin 9)
#define VERTGND 8 //
#endif
#define LOWER 0
#define UPPER 255
uint8_t horzRead, vertRead; //read as [0,1023]>>2 = [0,255]
void setup() {
pinMode(VCC, OUTPUT); //provides 5V (little current draw)
digitalWrite(VCC, HIGH); //
pinMode(GND, OUTPUT); //provides sink, again, little draw
digitalWrite(GND, LOW);
#if defined (__AVR_ATmega328P__)
Serial.begin(9600);
#endif
#if defined (__AVR_ATtiny84__)
mySerial.begin(9600); // Start serial processing
delay(1000); // Give Serial class time to complete initialization.
// otherwise, 1st output likely missing or garbled
// mySerial.println("SETUP Complete - SoftwareSerial Example");
// pinMode(VERTGND, OUTPUT); //provide a pseudo-ground for LED sourced
// digitalWrite(VERTGND, LOW); //by PWM (OC0A) on pin 8
#endif
}
void loop() {
// Avoid 0, used as the registration marker
horzRead = constrain(analogRead(HORZ) >> 2, 1, 255);
delay(10);
vertRead = constrain(analogRead(VERT) >> 2, 1, 255);
delay(10);
//for use with Bicolor LED as indicator...
// analogWrite(HORZOUT, horzRead);
// analogWrite(VERTOUT, vertRead);
#if defined (__AVR_ATmega328P__)
#ifdef PROCESSING
Serial.write(0); //registration marker
Serial.write(horzRead); //
Serial.write(vertRead); //place the byte value in the stream
#else //LOCAL Serial Monitor/Plotter...
// Serial.print(LOWER + String(" "));
// Serial.print(UPPER + String(" "));
Serial.print(horzRead + String(" "));
Serial.println(vertRead);
#endif
#endif
#if defined (__AVR_ATtiny84__)
#ifdef PROCESSING
mySerial.write((byte)0); //registration marker
mySerial.write((byte)horzRead); //
mySerial.write((byte)vertRead); //place the byte value in the stream
#else //LOCAL Serial Monitor/Plotter...
// mySerial.print(LOWER + String(" "));
// mySerial.print(UPPER + String(" "));
mySerial.print(horzRead + String(" "));
mySerial.println(vertRead);
#endif
#endif
}