Commit 5baf4e59 authored by O'Reilly Media, Inc's avatar O'Reilly Media, Inc
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

README.md

0 → 100644
+15 −0
Changes for README.md: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Example files for the title:  
  
# Make a Mind-Controlled Arduino Robot, by Tero Karvinen
  
[![Make a Mind-Controlled Arduino Robot, by Tero Karvinen](http://akamaicovers.oreilly.com/images/9781449311537/cat.gif)](https://www.safaribooksonline.com/library/view/title/9781449312800//)
  
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
  
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
  
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
  
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
  
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.

README.txt

0 → 100755
+1 −0
Changes for README.txt: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
Here is the example code for Make a Mind-Controlled Arduino Robot. The authors also maintain a site, http://Mindcontrol.Botbook.com, where you can more information on the book, including a copy of this sample code. From time-to-time, the author's copy of the sample code may be more up-to-date than what you find in this directory.
 No newline at end of file

logo.png

0 → 100644
+3.84 KiB
Loading image diff...
+15 −0
Changes for mindcontrol-code/README: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
Source code for the book "Make a Mind Controlled Arduino Robot" 
http://MindControl.BotBook.com (c) Kimmo Karvinen & Tero Karvinen

Think about it - and it moves. Think harder, the robot moves faster. 
But don't worry; as a robot it knows to avoid the black border so it 
doesn't fall off the table.

"Make a Mind Controlled Arduino Robot" shows you how to build your 
own. You learn to measure attention level with a NeuroSky headband and
send this information into Arduino. You will also build a 
line-avoiding system into the bot. And, of course, you will build the 
chassis of your robot from scratch.

http://www.amazon.com/gp/product/1449311547?ie=UTF8&tag=sulautetut-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1449389716
+138 −0
Changes for mindcontrol-code/allbutmind/allbutmind.pde: 138 added lines, 0 removed lines.
Original line number Diff line number Diff line
// allbutmind.pde - a mind controlled robot without mind control 
// (c) Tero Karvinen & Kimmo Karvinen http://MindControl.BotBook.com


const int linePin=4;
const int servoLeftPin=3;
const int servoRightPin=2;
const int potPin=A0;
const int redPin=9;
const int greenPin=10;
const int bluePin=11;
const float potMin=14.0-1;        // <1>
const float potMax=236.0-5;
const int speakerPin=12;

float speed=0.0;        // <2>

void setup()
{
        pinMode(linePin, INPUT);
        pinMode(servoLeftPin, OUTPUT);
        pinMode(servoRightPin, OUTPUT);
        pinMode(potPin, INPUT);
        digitalWrite(potPin, HIGH); // internal pullup
        pinMode(speakerPin, OUTPUT);
        
        sayReady();        // <3>
}

void loop()
{
        while (lineDetected()) turn();
        
        float at=getAttention();        // <4>
        float tr=getThreshold();        // <5>
        speed=fakeInertia(speed, at, tr);        // <6>
        
        forward(speed);
}

float fakeInertia(float speed, float at, float tr)
{
        if (at<tr)         // <7>
                speed-=0.05;
        else
                speed=at;        // <8>
        if (speed<0) speed=0.0;        // <9>
        return speed;
}

/*** Inputs ***/

bool lineDetected()
{
        return ! digitalRead(linePin);
}

float getAttention()
{
        return 0.5; // dummy value for testing        // <10>
}

float getThreshold()
{
        int x=analogRead(potPin);        // <11>
        float tr=(x-potMin)/potMax;      // <12>
        setBlueToRed(tr);
        return tr;
}

/*** Outputs ***/

void forward(float speed)
{
        for (int i = 0; i < 10; i++) { 
                pulseServo(servoLeftPin, 1500+500*speed);
                pulseServo(servoRightPin, 1500-500*speed);
        }
}

void turn()
{
        for (int i = 0; i < 30; i++) {
                pulseServo(servoLeftPin, 1500+500);
                pulseServo(servoRightPin, 1500+500);
        }
}

void pulseServo(int pin, int microseconds)
{
        digitalWrite(pin, HIGH);
        delayMicroseconds(microseconds);
        digitalWrite(pin, LOW);
        delay(5);
}

void setBlueToRed(float redPercent)
{
        int red=redPercent*255;        // <13>
        int blue=(1-redPercent)*255;
        setColor(red, 0, blue);
}

void setColor(int red, int green, int blue)
{
        analogWrite(redPin, 255-red);
        analogWrite(greenPin, 255-green);
        analogWrite(bluePin, 255-blue);
}

void sayReady()
{
        wave(speakerPin, 440, 40);
        delay(25);
        wave(speakerPin, 300, 20);
        wave(speakerPin, 540, 40);
        delay(25);
        wave(speakerPin, 440, 20);
        wave(speakerPin, 640, 40);
        delay(25);
        wave(speakerPin, 540, 40);
        delay(25);
}

void wave(int pin, float frequency, int duration)
{
        float period = 1/frequency * 1000 * 1000; // microseconds
        long int startTime = millis();
        while (millis()-startTime < duration) {
                digitalWrite(pin, HIGH);
                delayMicroseconds(period / 2);
                digitalWrite(pin, LOW);
                delayMicroseconds(period / 2);
        }
}


Loading