mindflex

Dependencies:   mbed Arduino millis

Committer:
kbeck8
Date:
Thu Oct 11 18:19:51 2018 +0000
Revision:
0:6466440bff3a
please edit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kbeck8 0:6466440bff3a 1 // Arduino Brain Library - Brain Blinker
kbeck8 0:6466440bff3a 2
kbeck8 0:6466440bff3a 3 // Description: Basic brain example, blinks an LED on pin 13 faster as your "attention" value increases.
kbeck8 0:6466440bff3a 4 // Adapted from the Blink without Delay example distributed with Arduino environment.
kbeck8 0:6466440bff3a 5 // More info: https://github.com/kitschpatrol/Arduino-Brain-Library
kbeck8 0:6466440bff3a 6 // Author: Eric Mika, 2010 revised in 2014
kbeck8 0:6466440bff3a 7
kbeck8 0:6466440bff3a 8 #include <Brain.h>
kbeck8 0:6466440bff3a 9
kbeck8 0:6466440bff3a 10 // Set up the brain reader, pass it the hardware serial object you want to listen on.
kbeck8 0:6466440bff3a 11 Brain brain(Serial);
kbeck8 0:6466440bff3a 12
kbeck8 0:6466440bff3a 13 const int ledPin = 13; // 13 is handy because it's on the board.
kbeck8 0:6466440bff3a 14 long interval = 500; // Changes based on attention value.
kbeck8 0:6466440bff3a 15 long previousMillis = 0;
kbeck8 0:6466440bff3a 16 int ledState = LOW;
kbeck8 0:6466440bff3a 17
kbeck8 0:6466440bff3a 18 void setup() {
kbeck8 0:6466440bff3a 19 // Set up the LED pin.
kbeck8 0:6466440bff3a 20 pinMode(ledPin, OUTPUT);
kbeck8 0:6466440bff3a 21
kbeck8 0:6466440bff3a 22 // Start the hardware serial.
kbeck8 0:6466440bff3a 23 Serial.begin(9600);
kbeck8 0:6466440bff3a 24 }
kbeck8 0:6466440bff3a 25
kbeck8 0:6466440bff3a 26 void loop() {
kbeck8 0:6466440bff3a 27 // Expect packets about once per second.
kbeck8 0:6466440bff3a 28 if (brain.update()) {
kbeck8 0:6466440bff3a 29 Serial.println(brain.readCSV());
kbeck8 0:6466440bff3a 30
kbeck8 0:6466440bff3a 31 // Attention runs from 0 to 100.
kbeck8 0:6466440bff3a 32 interval = (100 - brain.readAttention()) * 10;
kbeck8 0:6466440bff3a 33 }
kbeck8 0:6466440bff3a 34
kbeck8 0:6466440bff3a 35 // Make sure we have a signal.
kbeck8 0:6466440bff3a 36 if(brain.readSignalQuality() == 0) {
kbeck8 0:6466440bff3a 37
kbeck8 0:6466440bff3a 38 // Blink the LED.
kbeck8 0:6466440bff3a 39 if (millis() - previousMillis > interval) {
kbeck8 0:6466440bff3a 40 // Save the last time you blinked the LED.
kbeck8 0:6466440bff3a 41 previousMillis = millis();
kbeck8 0:6466440bff3a 42
kbeck8 0:6466440bff3a 43 // If the LED is off turn it on and vice-versa:
kbeck8 0:6466440bff3a 44 if (ledState == LOW)
kbeck8 0:6466440bff3a 45 ledState = HIGH;
kbeck8 0:6466440bff3a 46 else
kbeck8 0:6466440bff3a 47 ledState = LOW;
kbeck8 0:6466440bff3a 48
kbeck8 0:6466440bff3a 49 // Set the LED with the ledState of the variable:
kbeck8 0:6466440bff3a 50 digitalWrite(ledPin, ledState);
kbeck8 0:6466440bff3a 51 }
kbeck8 0:6466440bff3a 52 }
kbeck8 0:6466440bff3a 53 else {
kbeck8 0:6466440bff3a 54 digitalWrite(ledPin, LOW);
kbeck8 0:6466440bff3a 55 }
kbeck8 0:6466440bff3a 56
kbeck8 0:6466440bff3a 57 }