Created
October 8, 2019 09:49
-
-
Save RobolinkAkademi/3554e06397dd58d716ba406b1a11962d to your computer and use it in GitHub Desktop.
Jlinker_Nrf_joystick_control.ino
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 <SPI.h> | |
#include <nRF24L01.h> | |
#include <RF24.h> | |
#include "U8glib.h" | |
#include <Wire.h> //I2C Arduino Library | |
#include "I2Cdev.h" | |
#include "MPU6050.h" | |
#define Joy1X A0 | |
#define Joy1Y A1 | |
#define Joy2X A2 | |
#define Joy2Y A3 | |
#define POT_1 A6 | |
#define POT_2 A7 | |
#define Buton_1 0 | |
#define Buton_2 1 | |
#define Buton_3 2 | |
#define Buton_4 3 | |
#define Joy1But 4 | |
#define Joy2But 5 | |
#define SW_1 6 | |
#define SW_2 7 | |
#define User_LED 8 | |
#define address 0x1E //0011110b, I2C 7bit address of HMC5883 | |
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0); | |
MPU6050 accelgyro; | |
int16_t ax, ay, az; | |
int16_t gx, gy, gz; | |
int bsd = 0; | |
char bufferX [20]; | |
char bufferY [20]; | |
char bufferZ [20]; | |
int transmit[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | |
int distance; | |
int buton_j1 = 0, buton_j2 = 0, buton_1 = 0, buton_2 = 0, buton_3 = 0, buton_4 = 0, | |
switch_1 = 0, switch_2 = 0, joy1_x = 0, joy1_y = 0, joy2_x = 0, joy2_y = 0, pot_1 = 0, pot_2 = 0; | |
RF24 JOYSTICK(9, 10); // CE, CSN | |
const byte nrf_address[][6] = {"00001", "00002"}; | |
void setup() { | |
JOYSTICK.begin(); | |
JOYSTICK.openWritingPipe(nrf_address[1]); // 00002 | |
JOYSTICK.openReadingPipe(1, nrf_address[0]); // 00001 | |
JOYSTICK.setPALevel(RF24_PA_MIN); | |
JOYSTICK.stopListening(); | |
Serial.begin(9600); | |
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE | |
Wire.begin(); | |
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE | |
Fastwire::setup(400, true); | |
#endif | |
accelgyro.initialize(); | |
pinMode(Joy1X, INPUT); | |
pinMode(Joy1Y, INPUT); | |
pinMode(Joy2X, INPUT); | |
pinMode(Joy2Y, INPUT); | |
pinMode(POT_1, INPUT); | |
pinMode(POT_2, INPUT); | |
pinMode(Joy1But, INPUT_PULLUP); | |
pinMode(Joy2But, INPUT_PULLUP); | |
pinMode(Buton_1, INPUT_PULLUP); | |
pinMode(Buton_2, INPUT_PULLUP); | |
pinMode(Buton_3, INPUT_PULLUP); | |
pinMode(Buton_4, INPUT_PULLUP); | |
pinMode(SW_1, INPUT); | |
pinMode(SW_2, INPUT); | |
pinMode(User_LED , OUTPUT); | |
digitalWrite(User_LED , HIGH); | |
Serial.println("Testing device connections..."); | |
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); | |
} | |
void loop() { | |
ValueRead(); | |
Decide(); | |
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); | |
JOYSTICK.write(&transmit, sizeof(transmit)); | |
sprintf(bufferX, " Robolink Market "); | |
sprintf(bufferY, " Jlinker "); | |
sprintf(bufferZ, " Rodeo-2.4G"); | |
u8g.firstPage(); | |
do { | |
draw(); | |
} while ( u8g.nextPage() ); | |
delay(5); | |
} | |
void ValueRead() | |
{ | |
joy1_x = analogRead(Joy1X); | |
joy1_y = analogRead(Joy1Y); | |
joy2_x = analogRead(Joy2X); | |
joy2_y = analogRead(Joy2Y); | |
pot_1 = analogRead(POT_1); | |
pot_2 = analogRead(POT_2); | |
buton_j1 = digitalRead(Joy1But); | |
buton_j2 = digitalRead(Joy2But); | |
buton_1 = digitalRead(Buton_1); | |
buton_2 = digitalRead(Buton_2); | |
buton_3 = digitalRead(Buton_3); | |
buton_4 = digitalRead(Buton_4); | |
switch_1 = digitalRead(SW_1); | |
switch_2 = digitalRead(SW_2); | |
} | |
int Decide() | |
{ | |
for (int i = 0; i <= 9; i++) | |
transmit[i] = 0; | |
if (switch_1 == 1 && switch_2 == 1)//Çizgi | |
{ | |
transmit[0] = 1; | |
} | |
if (switch_1 == 0 && switch_2 == 1)//Işık | |
{ transmit[1] = 1; | |
} | |
if (switch_1 == 1 && switch_2 == 0)//Engel | |
{ transmit[2] = 1; | |
} | |
if (switch_1 == 0 && switch_2 == 0) //Manuel | |
{ | |
if ( joy2_y > 530) //ileri | |
{ transmit[3] = 1; | |
} | |
if ( joy2_y < 370) //geri | |
{ transmit[4] = 1; | |
} | |
if (joy1_x > 550 ) //sağ | |
{ transmit[5] = 1; | |
} | |
if (joy1_x < 490) // sol | |
{ transmit[6] = 1; | |
} | |
} | |
if (buton_3 == 0 ) //hızlı | |
{ transmit[7] = 1; | |
} | |
if (buton_4 == 0 ) //buzzer | |
{ transmit[8] = 1; | |
} | |
if (buton_1 == 0 && buton_2 == 0 ) | |
{ | |
ValueRead(); | |
if (ay < -3000) //ileri | |
{ transmit[3] = 1; | |
} | |
if (ay > 5000) // geri | |
{ transmit[4] = 1; | |
} | |
if ( ax < -4500) //sağ | |
{ | |
transmit[6] = 1; | |
} | |
if ( ax > 4000) //sol | |
{ transmit[5] = 1; | |
} | |
} | |
transmit[9] = map(pot_2, 0, 1023, 255, 0); | |
} | |
void draw(void) { | |
u8g.setFont(u8g_font_unifont); | |
u8g.drawStr( 0, 20, bufferX); | |
u8g.drawStr( 0, 40, bufferY); | |
u8g.drawStr( 0, 60, bufferZ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment