/*
Example sketch KA_90748_Joystick.ino version 1.0.
From Kjell Academys Sensor kit 35 in 1, Joystick module example.
Written by: Ante Larsson, 2017-08-14
This code is in the public domain.
*/
int xAxis = A0; //Choose a analog pins.
int yAxis = A1;
int sw = A2;
// include the Servo library
#include <Servo.h>
Servo myServo; // create a servo object
// Include the display library for OLED
#include "U8glib.h"
// Create new SSD1306 I2C display with 128x64 resolution
U8GLIB_SSD1306_128X64 myOled(U8G_I2C_OPT_NONE);
// Include the Neopixel library
#include <Adafruit_NeoPixel.h>
// Declare and initialise global GPIO pin constant for Neopixel ring
const byte neoPin = 4;
// Declare and initialise global constant for number of pixels
const byte neoPixels = 24;
// Declare and initialise variable for Neopixel brightness
byte neoBright = 255;
// Create new Neopixel ring object
Adafruit_NeoPixel myRing = Adafruit_NeoPixel(neoPixels, neoPin, NEO_GRB);
//Globala variabler som kan användas i alla funktioner
int angle; // variable to hold the angle for the servo motor
int servoPin=3; // styr servomotorn med GPIO 2
int ledNr; //det antal pixlar jag vill tända i ringen
int sensorVal; // en variabel som sparar sensorvärdet som heltal
int minVal=513; // en variabel för sensorns minsta värde
int maxVal=540; // en varibel för sensorns största värde
int low_level=1.01*minVal; //nedre gränsvärdet som delar in sensorvärdena i tre områden
int high_level=0.99*maxVal; //övre gränsvärdet som delar in sensorvärdena i tre områden
// Declare and initialise global variables for display resolution
const int logo_width = 64;
const int logo_height = 64;
static unsigned char Pil_20upp_bits[] U8G_PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x80, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xe0, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8,
0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff, 0x03, 0x00, 0x00,
0x00, 0x00, 0x00, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc,
0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x07, 0x00, 0x00,
0x00, 0x00, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x1f, 0x00, 0x00,
0x00, 0x00, 0x80, 0xff, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x80, 0xff,
0xff, 0x3f, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x7f, 0x00, 0x00,
0x00, 0x00, 0xc0, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xdf,
0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x9f, 0xff, 0xff, 0x01, 0x00,
0x00, 0x00, 0xe0, 0xdf, 0x7f, 0xff, 0x01, 0x00, 0x00, 0x00, 0xf8, 0x8f,
0x7f, 0xfe, 0x03, 0x00, 0x00, 0x00, 0xf8, 0x87, 0xff, 0xfc, 0x03, 0x00,
0x00, 0x00, 0xfc, 0x87, 0x3f, 0xf8, 0x07, 0x00, 0x00, 0x00, 0xfe, 0xc1,
0x3f, 0xf8, 0x0f, 0x00, 0x00, 0x00, 0xfe, 0x81, 0x7f, 0xf0, 0x0f, 0x00,
0x00, 0x00, 0x7e, 0xc0, 0x3f, 0xe0, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x80,
0x7f, 0xe0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x3f, 0xc0, 0x3f, 0x00,
0x00, 0x00, 0x00, 0xc0, 0x3f, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x80,
0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x3f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xc0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x1f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xc0, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,
0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x3f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xc0, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x1f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xc0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x1f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xe0, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x1f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xc0, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0,
0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
void setup() {
pinMode(xAxis, INPUT); //Set the pins to input.
pinMode(yAxis, INPUT);
pinMode(sw, INPUT);
Serial.begin(9600); //Börjar kommunikation med OLED skärmen
myServo.attach(servoPin); // attaches the servo on servoPin to the servo object
}
void loop() {
myOled.firstPage();
myOled.setFont(u8g_font_helvB10);
String direction = "";
// Bestäm servots vinkel som variabeln angle
myServo.write(angle);
if (analogRead(yAxis) > 550) { //Om y-axis är större än 550 då pekar joysticken syd.
direction = "S";
angle = 180;
}
else if (analogRead(yAxis) < 450) { //Om y-axis är mindre än 450 då pekar joysticken norr.
direction = "N";
angle = 0;
// Rita ut bilden där pilen riktar upp
myOled.drawXBMP(0, 0, logo_width, logo_height, Pil_20upp_bits);
}
else if (analogRead(xAxis) > 550) { //Om y-axis > 550 = west.
direction += "W";
angle = 45;
}
else if (analogRead(xAxis) < 450) { //Om y-axis < 450 = east.
direction += "E";
angle = 135;
}
// Återvänd till normal läge
else {
angle = 90;
}
if (analogRead(sw) < 10) { //The voltage off the switch is changing depending on the values off the axis, but if it is less then 10 it is pressed.
Serial.println("Pressed");
} else {
Serial.println("Unpressed");
}
Serial.println(direction);
delay(500); //Vänta en halv sekund
// Skriver ut sensorvärdena, Norr, Syd, Öst, Väst.
do {
myOled.drawStr(100, 30, direction.c_str());
} while (myOled.nextPage());
delay(20);
}