Skip to content

Instantly share code, notes, and snippets.

@amokan
Last active December 14, 2015 10:39
Show Gist options
  • Save amokan/5073819 to your computer and use it in GitHub Desktop.
Save amokan/5073819 to your computer and use it in GitHub Desktop.
#include <Shieldbot.h>
#include <Wire.h>
#define UNO32_DEVICE 4
#define SENSOR_1 1
#define SENSOR_2 2
#define SENSOR_3 3
#define SENSOR_4 4
#define SENSOR_5 5
#define MOTOR_FORWARD 127
Shieldbot sbot = Shieldbot();
void setup() {
Serial.begin(9600); // serial for debugging (or sending to Processing)
Wire.begin(); // join i2c bus
sbot.setMaxSpeed(50);
sbot.drive(MOTOR_FORWARD, MOTOR_FORWARD);
}
void loop() {
// read RPR-220 sensor and broadcast values
sendSensorPayload();
// pause (may have to change this when doing motor stuff
delay(500);
}
void sendSensorPayload() {
// simple bitmask to send that will indicate state of sensors
byte payload = B00000000;
// read sensor 1
if (sbot.readS1()) {
payload |= (1 << SENSOR_1);
}
//read sensor 2
if (sbot.readS2()) {
payload |= (1 << SENSOR_2);
}
// read sensor 3
if (sbot.readS3()) {
payload |= (1 << SENSOR_3);
}
// read sensor 4
if (sbot.readS4()) {
payload |= (1 << SENSOR_4);
}
// read sensor 5
if (sbot.readS5()) {
payload |= (1 << SENSOR_5);
}
Serial.println(payload); // debugging info and/or processing
// send payload to UNO32, which we marked as device #4
Wire.beginTransmission(UNO32_DEVICE);
Wire.write(payload);
Wire.endTransmission();
}
#include <Wire.h>
void setup() {
Wire.begin(4);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void loop() {
delay(100);
}
void receiveEvent(int howMany) {
byte payload = Wire.receive();
if(payload & (1<<1)) {
Serial.println("Sensor 1");
}
if(payload & (1<<3)) {
Serial.println("Sensor 3");
}
if(payload & (1<<5)) {
Serial.println("Sensor 5");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment