134 lines
2.7 KiB
C++
134 lines
2.7 KiB
C++
/*
|
|
* HybridPlay sensor firmware 1.0.0 | 11/2015
|
|
* -------------------------
|
|
*
|
|
* Read HybridPlay sensors and
|
|
* send the data via serial/bluetooth
|
|
*
|
|
* rev 08:
|
|
* - TESTING RN4020 Bluetooth Module
|
|
*
|
|
* PINs Mapping:
|
|
*
|
|
* pin 11 digital --> speaker
|
|
* pin 10 digital --> power LED
|
|
* pin ? analog --> battery
|
|
*
|
|
* (cc) 2016 Lalalab | Emanuele Mazza aka n3m3da
|
|
* http://www.lalalab.org
|
|
* http://www.hybridplay.com
|
|
* http://www.d3cod3.org
|
|
*/
|
|
|
|
// External Libraries
|
|
#include "pitches.h"
|
|
#include <Wire.h>
|
|
|
|
|
|
// DEBUG vars
|
|
boolean debugMode = true;
|
|
|
|
////////////////////////////////////////////////
|
|
// Hardware interface
|
|
#define speaker 9
|
|
#define ledRed 6
|
|
#define ledGreen 10
|
|
#define ledBlue 5
|
|
#define batteryPin 5
|
|
|
|
#define CMD 4 //Command input Ble
|
|
#define int_accel 7
|
|
|
|
long timer = 0; //general purpuse timer
|
|
long timerBattery = 0;
|
|
long timerBT = 0;
|
|
int batteryCapacity = 1000; // mAh
|
|
int powerLedBrightness = 0;
|
|
int powerLedFade = 1;
|
|
|
|
int sensorON_melody[] = {
|
|
NOTE_C4, NOTE_C4, NOTE_C4
|
|
};
|
|
// note durations: 4 = quarter note, 8 = eighth note, etc.:
|
|
int sensorON_durations[] = {
|
|
16, 16, 16
|
|
};
|
|
////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////
|
|
// GP2Y0E02B IR proximity sensor Variables
|
|
#define IR_ADDRESS 0x80 >> 1 // Arduino uses 7 bit addressing so we shift address right one bit
|
|
#define IR_DISTANCE_REG 0x5E
|
|
#define IR_SHIFT 0x35
|
|
|
|
int ir_distance = 0; // Stores the calculated distance
|
|
byte ir_high,ir_low = 0; // High and low byte of distance
|
|
int ir_shift = 0; // Value in shift bit register
|
|
////////////////////////////////////////////////
|
|
|
|
// Serial packet
|
|
#define SERIAL_BAUD_VEL 38400
|
|
const char HEADER = 'H';
|
|
const char FOOTER = 'F';
|
|
|
|
// 2D DIRECTIONS
|
|
|
|
// TILT SENSOR
|
|
|
|
// IR SENSOR
|
|
int valIR = 0;
|
|
int prevValIR = -1;
|
|
// BATTERY
|
|
int valBattery = 0;
|
|
int prevValBattery = -1;
|
|
|
|
void setup() {
|
|
// Serial
|
|
initSerial();
|
|
|
|
// Bluetooth
|
|
pinMode(CMD,OUTPUT);
|
|
initBluetooth();
|
|
// Power Led Mode
|
|
pinMode(ledGreen,OUTPUT);
|
|
pinMode(ledRed, OUTPUT);
|
|
pinMode(ledBlue, OUTPUT);
|
|
digitalWrite(ledRed, HIGH);
|
|
digitalWrite(ledBlue, HIGH);
|
|
digitalWrite(ledGreen, LOW);
|
|
|
|
// sound on start
|
|
sensorON_play();
|
|
|
|
// Battery
|
|
readBattery();
|
|
|
|
// Time
|
|
timer = millis();
|
|
timerBattery = millis();
|
|
timerBT = millis();
|
|
delay(20);
|
|
|
|
}
|
|
|
|
void loop() {
|
|
if((millis()-timer) >= 40){ // Main loop runs at 25Hz (same as 25 FPS)
|
|
// POWER LED
|
|
//fadeLed();
|
|
|
|
// IR
|
|
updateIR();
|
|
|
|
// print on usb (Serial)
|
|
if(debugMode){
|
|
//printData();
|
|
receiveBluetoothSerial();
|
|
}
|
|
|
|
// data send via BLUETOOTH (Serial1)
|
|
updateBTServices();
|
|
|
|
}
|
|
|
|
}
|