Skip to content

Instantly share code, notes, and snippets.

@jasongao97
Last active November 5, 2020 12:30
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 jasongao97/6db0303967e0bac42fd4a70bf7ad755d to your computer and use it in GitHub Desktop.
Save jasongao97/6db0303967e0bac42fd4a70bf7ad755d to your computer and use it in GitHub Desktop.
Balance Ball game with two vibration motor
/*
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