Skip to content

Instantly share code, notes, and snippets.

@opylnev
Created January 27, 2016 06:39
Show Gist options
  • Save opylnev/2a96d4899e48585ad634 to your computer and use it in GitHub Desktop.
Save opylnev/2a96d4899e48585ad634 to your computer and use it in GitHub Desktop.
#include <Brain.h>
#include <Servo.h>
#include <SimpleTimer.h> // Source code here: http://playground.arduino.cc/Code/SimpleTimer
Brain brain(Serial);
const int greenPin = 2;
const int yellowPin = 4;
const int redPin = 7;
long interval = 500;
long previousMillis = 0;
int ledState = LOW;
int attValue;
////////////////////////////////////////////
int servoPin = 10;
Servo servo;
////////////////////////////////////////////
// the timer object
SimpleTimer timer;
int angle = 0; // servo position in degrees
bool movingArm = false;
bool positiveBrainSignal = false;
void checkAndMoveArm() {
if (movingArm) return;
if (!positiveBrainSignal) return;
movingArm = true;
// scan from 0 to 180 degrees
for(angle = 0; angle < 180; angle++)
{
servo.write(angle);
delay(15);
}
// now scan back from 180 to 0 degrees
for(angle = 180; angle > 0; angle--)
{
servo.write(angle);
delay(15);
}
movingArm = false;
}
void setup() {
// Set up the LED pin.
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(redPin, OUTPUT);
// Start the hardware serial.
Serial.begin(9600);
// Init servo
servo.attach(servoPin);
// Start arm timer (100 ms scan interval)
timer.setInterval(100, checkAndMoveArm);
}
void loop() {
// Expect packets about once per second.
if (brain.update()) {
Serial.println(brain.readCSV());
// Attention runs from 0 to 100.
attValue = brain.readAttention();
}
// Make sure we have a signal.
if(brain.readSignalQuality() == 0) {
// Light up the green LED
if (attValue < 50) {
digitalWrite( greenPin, HIGH);
digitalWrite( yellowPin, LOW);
digitalWrite( redPin, LOW);
positiveBrainSignal = true;
}
//Light up the red LED
else if (attValue >70)
{
digitalWrite( redPin, HIGH);
digitalWrite( greenPin, LOW);
digitalWrite( yellowPin, LOW);
positiveBrainSignal = false;
}
//Light up the Yellow LED
else if (attValue < 70 and attValue > 50)
{
digitalWrite( yellowPin, HIGH);
digitalWrite( greenPin, LOW);
digitalWrite( redPin, LOW);
positiveBrainSignal = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment