Created
June 21, 2017 12:34
-
-
Save windix/6bfbad5fffe4109374a326c7c35e6c5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> // This library allows you to communicate with I2C | |
#include <Adafruit_GFX.h> // Adafruit GFX graphics core library | |
#include <Adafruit_SSD1306.h> // Driver library for 'monochrome' 128x64 and 128x32 OLEDs | |
#define OLED_RESET 4 | |
Adafruit_SSD1306 display(OLED_RESET); | |
#if (SSD1306_LCDHEIGHT != 64) | |
#error("Height incorrect, please fix Adafruit_SSD1306.h!"); | |
#endif | |
int analogPinX = 0; // A0 | |
int analogPinY = 1; // A1 | |
int buttonPin = 7; | |
int torlerance = 10; | |
int wait = 10; | |
int centerX = 530; | |
int centerY = 530; | |
int previousX = 0; | |
int previousY = 0; | |
int x, y, button; | |
int changed = false; | |
int displayHeight = 0; | |
int displayWidth = 0; | |
int displayX = 0; | |
int displayY = 0; | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(analogPinX, INPUT); | |
pinMode(analogPinY, INPUT); | |
pinMode(buttonPin, INPUT); | |
Serial.begin(9600); | |
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64) | |
display.clearDisplay(); | |
display.display(); | |
displayHeight = display.height() - 1; | |
displayWidth = display.width() - 1; | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
x = analogRead(analogPinX); | |
y = analogRead(analogPinY); | |
button = digitalRead(buttonPin); | |
if (abs(0 - x) <= torlerance || abs(0 - y) <= torlerance || | |
abs(1023 - x) <= torlerance || abs(1023 - y) <= torlerance || | |
abs(previousX - x) >= torlerance || abs(previousY - y) >= torlerance) { | |
// Serial.print("X="); | |
// Serial.print(x); | |
// Serial.print(", Y="); | |
// Serial.print(y); | |
if (x - previousX > 0 || x == 1023) { | |
displayX += 1; | |
} | |
if (x - previousX < 0 || x == 0) { | |
displayX -= 1; | |
} | |
if (y - previousY > 0 || y == 1023) { | |
displayY += 1; | |
} | |
if (y - previousY < 0 || y == 0) { | |
displayY -= 1; | |
} | |
if (displayX <= 0) { | |
displayX = 0; | |
} | |
if (displayX >= displayWidth) { | |
displayX = displayWidth; | |
} | |
if (displayY <= 0) { | |
displayY = 0; | |
} | |
if (displayY >= displayHeight) { | |
displayY = displayHeight; | |
} | |
//display.drawPixel(displayX, displayY, WHITE); | |
//display.fillRect(displayX, displayY, 4, 4, WHITE); | |
display.drawCircle(displayX, displayY, 10, WHITE); | |
//display.drawPixel(map(x, 0, 1023, 0, displayWidth), map(y, 0, 1023, 0, displayHeight), WHITE); | |
display.display(); | |
display.clearDisplay(); | |
previousX = x; | |
previousY = y; | |
} | |
delay(wait); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment