Last active
November 5, 2020 12:30
-
-
Save jasongao97/6db0303967e0bac42fd4a70bf7ad755d to your computer and use it in GitHub Desktop.
Balance Ball game with two vibration motor
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
/* | |
Balancing Ball - Arduino part | |
More info: https://itp.jasongao.me/physical-computing/week-8-synchronous-serial | |
Arranged from LSM6DS3 example by Riccardo Rizzo | |
*/ | |
#include <Arduino_LSM6DS3.h> | |
const int leftVibPin = 2; | |
const int rightVibPin = 3; | |
int vibration[2] = {0, 0}; | |
void setup() { | |
Serial.begin(9600); | |
while (!Serial); | |
if (!IMU.begin()) { | |
Serial.println("Failed to initialize IMU!"); | |
while (1); | |
} | |
} | |
void loop() { | |
int index = 0; | |
while (Serial.available() > 0) { | |
int inByte = Serial.read(); | |
if (inByte == '\n') break; | |
vibration[index] = inByte; | |
index++; | |
} | |
analogWrite(leftVibPin, vibration[0]); | |
analogWrite(rightVibPin, vibration[1]); | |
float x, y, z; | |
if (IMU.accelerationAvailable()) { | |
IMU.readAcceleration(x, y, z); | |
Serial.print(x); | |
Serial.print(','); | |
Serial.print(y); | |
Serial.print(','); | |
Serial.println(z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment