Skip to content

Instantly share code, notes, and snippets.

@tobozo
Last active September 1, 2018 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobozo/f735e492aac27caddc8824c24eeb892b to your computer and use it in GitHub Desktop.
Save tobozo/f735e492aac27caddc8824c24eeb892b to your computer and use it in GitHub Desktop.
Sketch used by the Gyro unit as in https://www.youtube.com/watch?v=0tYRnR2kAIQ
/*
Gyro controls for MittisBootloop's Brick Breaker clone.
See https://github.com/MittisBootloop/ESP8266_webserver_brickbreaker
Also see https://www.reddit.com/r/arduino/comments/9bdozs/my_brick_breaker_clone_runs_on_esp8266_webserver/
Copyleft (c+) 2018 tobozo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <ESP8266WiFi.h>
#include "MPU6050_6Axis_MotionApps20.h" // https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050
#include "Wire.h"
MPU6050 mpu;
WiFiClient client;
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
bool dmpReady = false; // set true if DMP init was successful
bool tilted = false;
bool WiFiConnected = false;
const char* host = "2.2.2.2";
const char* ssid = "abcabc";
const char* password = "12341234";
const int httpPort = 80;
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
int orientation = 0, lastorientation = 0; // gyro to joystick value
int16_t ax, ay, az, gx, gy, gz;
unsigned long lasthid = millis(), debounce = 300;
void dmpDataReady() {
mpuInterrupt = true;
}
void sendGloveSignal(int orientation) {
if(!WiFiConnected) return;
String url;
switch(orientation) {
case 3:
url = "/start";
break;
case 0:
url = "/stop";
break;
case 1:
url = "/rinc";
break;
case -1:
url = "/linc";
break;
default:
return;
break;
}
if (!client.connect(host, httpPort)) {
Serial.println("Client connection failed");
return;
}
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
client.stop(); // don't wait for a response
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
unsigned long maxwaitpoint = millis() + 5000;
while (WiFi.status() != WL_CONNECTED && maxwaitpoint > millis()) {
delay(500);
Serial.print(".");
}
if(WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
if (!client.connect(host, httpPort)) {
Serial.println("Client connection failed");
} else {
client.stop();
}
WiFiConnected = true;
} else {
Serial.println("No WiFi, Serial debug only");
WiFiConnected = false;
}
Wire.begin(); // change this to match your SDA/SCL pins i.e Wire.begin(SDA, SCL)
Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
mpu.initialize();
if (mpu.dmpInitialize() == 0) {
Serial.println("MPU initialized!");
// turn on the DMP, now that it's ready
mpu.setDMPEnabled(true);
// supply your own gyro offsets here, scaled for min sensitivity
mpu.setXGyroOffset(62);
mpu.setYGyroOffset(100);
mpu.setZGyroOffset(82);
mpu.setZAccelOffset(-580); // 1688 factory default for my test chip
attachInterrupt(digitalPinToInterrupt(D2), dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();
// set our DMP Ready flag so the main loop() function knows it's okay to use it
Serial.println(F("DMP ready! Waiting for first interrupt..."));
dmpReady = true;
}
}
void loop() {
if(mpuInterrupt) {
mpuInterrupt = false;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
if(abs(ay)>500 && abs(gy)>100){
if(ay>0) {//orientation = -1;
if(orientation > -1) orientation--;
} else {//orientation = 1;
if(orientation < 1) orientation++;
}
} else {
if(abs(ay) < 150) {
orientation = 0;
}
}
if(abs(ay)>20000) {
tilted = true;
}
}
if(orientation != lastorientation) {
if(lasthid + debounce < millis()) {
lasthid = millis();
lastorientation = orientation;
if(tilted) {
if(orientation==0) {
tilted = false;
Serial.println("tilt");
sendGloveSignal(3);
}
} else {
Serial.println(String(orientation) + "\t" + String(ay) + "\t" + String(gy));
sendGloveSignal(orientation);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment