Maker Pro

Mind & Infra Red Controlled Robot Car

Status
Not open for further replies.
On the programming side of things, last night I wrote a short, simple sketch to use the Brain Library to retrieve "Signal Strength", "Attention" and "Meditation" values from the TGAM1 chip/board in the headsets. It's intended for use with a direct wired connection to the headset board, but should work with the data stream that you'll receive via Bluetooth, after it's been processed by the Brain Library.

Because this code will run on a small custom Arduino-based board connected directly to the headset board, after extraction using the Brain Library, the data is sent out serially at 19200 baud to an APC220 433MHz RF transceiver module, also in the headset, that will pass it on to a separate "base" board for use in controlling 'whatever'.
S@#t, I'll be happy if I can turn a LED on and off initially, or brighten and dim one with PWM, (first step to motor speed control).
I'm only using the "Signal Strength", "Attention" and "Meditation" values, as mentioned. It's really the "Attention" value that matters at first.

I didn't do it like in the example I posted the other day, which retrieved comma-separated values from the Brain Library in ASCII format, which then had to be re-parsed to extract the values. Way too slow for my liking.
Instead I went for data values only, in "uint8_t" form. Much quicker, and much more usable.
I noticed when reading through the Brain Library files that they didn't clear the error string between packets, so for my purpose I modified their library, at the very beginning of the "Brain::update()" function, to clear any error from parsing the previous packet, with this line:-
Code:
latestError[0]='\0';

Here's the Arduino sketch, in a mix of C and Arduino languages. It compiles fine in the Arduino IDE, but is otherwise untested. (I'll test it for bugs and modify as necessary when my Arduino arrives.)
Note that it's written using the 'SoftwareSerial' library, and not with the hardware serial library, but it's easily converted. I wanted to test out the Arduino 'SoftwareSerial' library.
Pin 5 connects to the TGAM1 board in the headset for receiving the raw data at 9600 baud, and Pin6 connects to the APC220 module for transmitting it to the 'base board' at 19200. I added liberal comments so it's easy for you to follow:-
Code:
// MyBrainLibTest.ino
// My first Brain Library test.
// Written by Steve Carroll, 12.9.2015.

#include <Brain.h>
#include <SoftwareSerial.h>

SoftwareSerial MySerial(5,6);  // RX on p5, TX on p6.
Brain TestBrain(MySerial);
uint8_t BrainBands[3];    // An array to hold the data.

void setup()
{
    MySerial.begin(9600);
}

void loop()
{
    if(TestBrain.update())    // Get the latest headset data.
    {
        char* pErrors=TestBrain.readErrors(); // Only use the data if there were no errors.
        if(strcmp(pErrors,"")==0)
        {   // Collect the required data from the library:-
            BrainBands[0]=TestBrain.readSignalQuality();
            BrainBands[1]=TestBrain.readAttention();
            BrainBands[2]=TestBrain.readMeditation();
            SendDataOut();
        }
    }
}

void SendDataOut()
{
    MySerial.end();            // Close the serial port and
    MySerial.begin(19200);        // change to 19200 baud.
    MySerial.write(BrainBands,3);    // Send the data to the APC220 module.
    MySerial.end();            // Close the serial port and
    MySerial.begin(9600);        //  change back to 9600 baud.
}

I haven't written any code for the 'base board' yet. And I'll probably modify this sketch to add 'sync' bytes at the beginning of each 'packet', as qualifiers to know when valid data is arriving. Something simple, like "QUAL", immediately befor the data bytes.

Next up, I'll write a sketch in 'Processing' to take the Brain Library's full-data csv output and display it graphically on the PC screen, for diagnostics and testing. Good, too, for helping to train the brain to concentrate and meditate to improve those aspects for control purposes.
It'll display "signal strength, attention, meditation, delta, theta, low alpha, high alpha, low beta, high beta, low gamma, high gamma".
This is the format of the data from the Brain Library:-
200,0,0,70022,1415627,296609,25469,7758,19291,6073,220596

I've never used 'Processing' before, apart from a quick test a few days ago, but I'll soon get the hang of it, (I hope).
 
Last edited:
AGAIN Steve, You've clicked onto just how much of a novice i am! I'm getting RF & IR mixed up in my excitement!!!!!! It WAS actually RF i was thinking about, but in my mis-wired space cadet brain, i was going for IR!!!! Nice one my mate!!!! Carry on like this, you're gonna get a bleeding god complex!!!!!!! HA! HA! HA! only jestin' my mate! I'm pretty chuffed with how its turning out. I'm planning on finishing construction today, Then starting coding. So PLEASE keep the coding advice coming!!! PLEASE don't ever think its not well received cos it IS!!!!!!!!!!
 
AGAIN Steve, You've clicked onto just how much of a novice i am! I'm getting RF & IR mixed up in my excitement!!!!!! It WAS actually RF i was thinking about, but in my mis-wired space cadet brain, i was going for IR!!!! Nice one my mate!!!! Carry on like this, you're gonna get a bleeding god complex!!!!!!! HA! HA! HA! only jestin' my mate! I'm pretty chuffed with how its turning out. I'm planning on finishing construction today, Then starting coding. So PLEASE keep the coding advice coming!!! PLEASE don't ever think its not well received cos it IS!!!!!!!!!!
Are you planning to use the Brain Library?
And I'm glad to hear you're planning to use RF.
 
Actually, I'm not. I'm planning to take chunks from one book & chunks from another combined with my own contributions, HOPEFULLY I'll be able to make it work. I just had a quick glance at a couple of books I've downloaded about C & am I right in assuming it uses a lot of mathematics? Because if it does, I'm absolutely SHIT at maths! I'm competent at physics, English, spelling, blagging, lockpicking, Archery, driving, even love making(apparently! Lol!) but MATHS! I think I'm numerically dyslexic!
 
Actually, I'm not. I'm planning to take chunks from one book & chunks from another combined with my own contributions, HOPEFULLY I'll be able to make it work. I just had a quick glance at a couple of books I've downloaded about C & am I right in assuming it uses a lot of mathematics? Because if it does, I'm absolutely SHIT at maths! I'm competent at physics, English, spelling, blagging, lockpicking, Archery, driving, even love making(apparently! Lol!) but MATHS! I think I'm numerically dyslexic!
I'm very pleased to hear that you're not simply copying the code from the book. It might get the job done that way, but not 'exactly' how you'd like it, plus you wouldn't learn much.

And if you're competent at Physics, you're not as bad at Maths as you think. :D
Physics and the associated Applied Maths are two of my favourite subjects.

Either way, there's no more maths in C than any other language. Sometimes it's necessary, most of the time it's not.
Just like Arduino, it's more a matter of learning the functions/statements that are available, and how to use them. But C has a lot more functions available.
Start with 'for' loops, 'while' loops, and 'if' statements. They're the three things you'll use the most.
Then gradually move on from there.

You can write in Arduino and gradually add more and more C as you progress. It's far more powerful, much faster, and best of all it uses a hell of a lot less code space to get a given job done. Arduino is really C, C libraries are called every time you use an Arduino command.

Like Arduino, the worst part is learning what library functions are available and how to use them. You need access to a good library reference.
Look up 'Standard C libraries' on the web. The two main ones needed for Arduino are stdlib and stdio.
Probably the best C resource for Arduino is the Arduino libraries themselves, already installed with Arduino. Search the Arduino install folders and you'll find them.

For now, start writing your code in Arduino, and if you get stuck with finding a way to do something, ask me and I'll explain how to do it with C. You'll pick up some basics very easily that way. Sometimes I'll be able to show you both - how to do something in Arduino and how to do the same thing in C.
Finally, C is very, very similar to Arduino, so when you're learning Arduino you're already learning C.

I'm doing another all-nighter, trying to get my head around 'Processing' language. Now that's confusing. Doesn't seem to have normal programming rules.
 
Last edited:
Eureka - I got the brainwave graph working, on serial input from a PIC via RF. It's working great.
It takes serial input at 19200 baud.
(Simulated values, of course - no headset yet.) When I get it completed, and you get to that stage, I can give you a copy if you like. I can compile it to an *.exe file, so you won't need to have 'Processing' installed.
(You'll need to have Java V8 installed though.)

Brain Graph Screenshot.JPG
This will be great for diagnostics, and for helping me train my brain.
Next I'll see if I can work out how to label each bar. I'll probably lift the bottom of the colour bars up about 10mm, then label on the white underneath, so the labels don't obscure the view.

Edit: I almost forgot. Credit where it's due. Left to my own devices, it was only in shades of grey. One of the boys on the 'Processing' forum, "GoToLoop", re-wrote it for the colours it has now. I couldn't figure out that bit.
Edit2: I've also got the text labelling for the bottom of each bar figured out now, too. Getting there.
 
Last edited:
Mate, I can't thank you enough for clearing that up about Arduino & C for me! I took a look at C & shit myself (basically)! I know that you're well versed in it but was scared about the math! But I understand where you're coming from with the Arduino being C, I've read that elsewhere. I'm actually still collecting parts (waiting for delivery from T'Interweb), so I'm gonna download what you've said & start trying to write sketch for real. I'll post it on here to you & you can give me your opinion. PLEASE be harsh cos I WANT to learn PROPERLY. I'm glad you like the fact I'm not just using generic codes. I got a HC-05 XBEE slave/master Bluetooth module & a RS232 TTL bluetooth module. What's your general opinion on them? Being honest, I'm not overly keen on opening up & trying to hack the headset with me being not THAT competent yet, I don't want to touch summat I shouldn't & **** it up cos I can't afford a new one! I'm glad you cracked the brainwave graph! Just proves hard work pays off!
 
Mate, I can't thank you enough for clearing that up about Arduino & C for me! I took a look at C & shit myself (basically)! I know that you're well versed in it but was scared about the math! But I understand where you're coming from with the Arduino being C, I've read that elsewhere. I'm actually still collecting parts (waiting for delivery from T'Interweb), so I'm gonna download what you've said & start trying to write sketch for real. I'll post it on here to you & you can give me your opinion. PLEASE be harsh cos I WANT to learn PROPERLY. I'm glad you like the fact I'm not just using generic codes. I got a HC-05 XBEE slave/master Bluetooth module & a RS232 TTL bluetooth module. What's your general opinion on them? Being honest, I'm not overly keen on opening up & trying to hack the headset with me being not THAT competent yet, I don't want to touch summat I shouldn't & **** it up cos I can't afford a new one! I'm glad you cracked the brainwave graph! Just proves hard work pays off!

Yeah, don't be concerned about C. It'll all fall into place.

I can't give an opinion on the HC-05 XBEE slave/master Bluetooth module & the RS232 TTL bluetooth module. I've never played with Bluetooth stuff at all.
Going by the Tero & Kimmo Karvinen book, the dongle hack looks fairly straightforward. Just a matter of cutting a track and soldering in your wires, basically. I'll have a better read of it tomorrow. I've only skipped through so far.

Hacking the Mindflex headset is easy too, but I can't speak for your Neurosky because I haven't seen one. I think it sends all of the data, and fairly often, too. (The Mindflex only sends once per second.)
The chip is the same, and if the board is also the same, only two connections are needed, to it's TxD pin and to ground. I'm doing it the way I am because the Mindflex game doesn't have a dongle, and only sends part of the data. The Mattel micro in the headset doesn't bother about the Alpha, Beta etc. Only Meditation, Attention and Signal strength, as far as I know.
I want the lot, so direct connection to the board is necessary.

I've improved the brain wave graph program even more, now. It's completely finished. I gave it dividing lines between the bars, and made the background black and the lines and text white.
Working really well. I wrote another PIC program to animate it tonight, with all of the bars moving up and down. There's a slight flicker each time it's updated, but I can live with that. It's only once per second.

It's been 7 days now since my headset was sent from the US, so it won't be far away. The Arduino will be longer, at least another two weeks. The waiting is killing me now that I have everything else ready to go. :mad:
 
I know exactly what you mean about the frustration!!! I had ONE HC-SR04 ultra sonic distance sensor (looks like eyes!) & i waited for absolutely AGES for a proper mount to come for it, so i can connect it to a servo & keep it looking sound. And NOW..............I've LOST BOTH!!!!!!!!! I'm FUMING!!!!! Now I've had to order another set of each!!!!!!! I'm waiting for some free spacers & nuts& bolts to arrive from Essentra (they should be here tomorrow, they're good like that.). THEN i can fit the final level with the Arduino Mega 2560 board & sensor shield. I am gonna put a infra red obstacle avoidance sensor at the rear, just so that when it's reversing, it's not knocking into stuff. Also a Photosensitive diode to make it either light or dark sensitive, A trickle charger for re-chargeable batteries, and i was HOPING to fit an LCD screen (the jury is still out on that one, but i WOULD like to.). What i wanted to ask is on the Arduino uno board, i've got screw shield terminals on either side & a L293D motor shield on top with the 4 motors connected up accordingly. Would you know WHICH actual pin terminals are being used for the motors? The reason being, so that i know which pin terminals i can connect other things to like Photodiode etc.
 
The NeuroSky headset works on meditation, concentration & BLINKING (of all things). When i first got it, i only had my iPhone because my laptop was bust. But now I've installed the proper software, it does SO much more than i originally realised! As well as the graphs & charts, There are game apps to make it more fun. One works on concentration to set a barrel on fire the more you concentrate, another is making a ball float thru meditation, ANOTHER is staring at Zombies & making their heads swell thru concentration then blinking to make them explode. I'm still getting used to this laptop but later i'll try take a screenshot of the graphs (i'm pretty sure you're not really THAT interested in zombies with exploding heads! (LOL) & post them on here for you to take a look at.
 
Mate, LISTEN! I have a BIG apology for you & also a MASSIVE thank you! Because i've been swerving the sketching side of things because A) I know its gonna take concentration & effort, so while i'm building it, I'm getting it thru to the mrs just how complicated this is to a beginner. So she HAS to give me some time to learn & NOT do the frigging washing up!!! LOL! And B) Just how much effort you've been putting into helping me on this! I've actually just sat down & read thru the sketches & stuff you've sent me so far properly & YEH MAN!!!!! NOW YA TALKIN!!!!! I'm gonna knuckle down & learn this shit!
 
Spike124 updated Mind & Infra Red Controlled Robot Car

14/9/15 This is where i'm up to now. I've added the Arduino Uno board with screw shields on either side & a Arduino Motor Shield, to which i've wired the 4 motors. I'm going to put a 2.1mm Jack plug close to the battery compartment as a form of Trickle charging NiMH Re-Chargeable batteries. I've bought the 2wheel drive version of this chassis to add a 3rd level. The reason for this is to give me room for a Arduino Mega 2560 board with a sensor shield. I have bought the NeuroSky Brainwave Headset, which i must say, i'm pretty impressed with & would encourage ANYONE to give one a shot! Rather than programming it with generic sketches i've copied from books, I'm in the process of learning the C language mixed with Arduino, thanks to a friend of mine on here Old Steve (Thanks Steve). So, things are coming along nicely & i'll update ASAP.

View updates to this showcase item...
 
So, Steve, I know this is going back to basics, but am I right in assuming that on the basic Arduino code
{
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000);
}
If I wrote it in C, it would be
{
digitalWrite(ledPin,1);
delay(1000);
digitalWrite(ledPin,0);
delay(1000);
}
I know that's simple, but I'm trying to go back to basics Arduino & trying to swap it over to C
 
First up, get yourself a cuppa before starting on this, it might take a while to get through. Officially the longest post I've ever typed on a forum.

spike124 said:
What i wanted to ask is on the Arduino uno board, i've got screw shield terminals on either side & a L293D motor shield on top with the 4 motors connected up accordingly. Would you know WHICH actual pin terminals are being used for the motors? The reason being, so that i know which pin terminals i can connect other things to like Photodiode etc.
Not having either an Arduino or that particular brand of motor shield, (or any other for that matter), that's pretty hard to answer. The datasheet for the motor shield should cover it, though, I imagine.


spike124 said:
The NeuroSky headset works on meditation, concentration & BLINKING (of all things). When i first got it, i only had my iPhone because my laptop was bust. But now I've installed the proper software, it does SO much more than i originally realised! As well as the graphs & charts, There are game apps to make it more fun. One works on concentration to set a barrel on fire the more you concentrate, another is making a ball float thru meditation, ANOTHER is staring at Zombies & making their heads swell thru concentration then blinking to make them explode. I'm still getting used to this laptop but later i'll try take a screenshot of the graphs (i'm pretty sure you're not really THAT interested in zombies with exploding heads! (LOL) & post them on here for you to take a look at.
Right, I'd forgotten about the blinking in the Neurosky sets. I read it somewhere the other day.
Pretty useful, I reckon.
I'd like to see those graphs.

Glad to see you updated the 'Project Log', too.


spike124 said:
So, Steve, I know this is going back to basics, but am I right in assuming that on the basic Arduino code
{
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000);
}
If I wrote it in C, it would be
{
digitalWrite(ledPin,1);
delay(1000);
digitalWrite(ledPin,0);
delay(1000);
}
I know that's simple, but I'm trying to go back to basics Arduino & trying to swap it over to C
It's not really C, but one step closer. It's still arduino language, but in a simpler form, and quicker to type.

Assuming that pin0, port B is the output the LED is on, and you wanted to write to the LED in C without affecting other pins, you might write:-

PORTB = PORTB & 0x00000001
PORTB = PORTB & 0x01 // Hex, not binary as I first wrote. Sorry

or, put more concisely still:-

PORTB &= 0x00000001
PORTB &= 0x01 // And again

or, using decimal instead of binary hexadecimal:-

PORTB &= 1

The '&' is the symbol for a logical 'AND'

(That's why, in programming, it's important to learn the logical AND, OR, NOT, XOR etc. Very useful.)

(I like binary for 'AND', because you can easily see exactly which pin you're working with.)

Personally, for simplicity, I think you should stick with one of these methods for now:-
digitalWrite(ledPin,HIGH);
or
digitalWrite(ledPin,1);

For simplicity when writing, that's how I'm doing it.

Where C / C++ comes into it's own is when you need to do things that are too complex in Arduino, like string (text) handling.
A few C examples in text handling:-

To copy a string, (actually a char array in C), from one variable, we'll call it cNameSource, to another, we'll call this one cNameDest:-
First we'll create and initialise cNameSource:-
char cNameSource[10]={"Spike"};

Now, to copy it:-
strcpy(cNameSource,cNameDest); // The 'cpy' is short for copy, of course.

Then, we'll add a surname:-
strcat(cNameDest," Milligan"); // The 'cat' is short for concatenate.

Now, for whatever reason, you might want to get the exact length of the name, and store
it in an 'int' variable, so:-
int iLength = strlen(cNameDest); // 'len' is length, needless to say.

There are literally thousands of C and C++ functions, for strings and almost anything else you can imagine, to do a very wide range of things.

Then, there are C 'classes'. Very, very powerful.
A class is another type like int, char etc, but it is a collection of both data types (int, char ......) and functions to address and manipulate the data, and you can create your own classes in C.
Windows is built from classes.

You could create a class called "Animal".
"Animal" might have data such as:-
int iLeg; // which would hold the number of legs
int iHead // holds the number of heads
int iArms // Number of arms, 0 = none
int iTail // Number of tails, 0 = none
etc, etc

Note that I use 'i' as a prefix to int variables, 'c' to prefix char variables, 'l' for long, 'ui' for unsigned int, etc etc. Not necessary, but it makes it easier to keep track of the type of variable you're working with.

Then a function to get the number of legs:-

int GetNumLegs() // A function will normally do more than simply return a value.
{
return iLeg;
}

Now, to create an instance of "Animal":-

Animal Horse;
Horse.iLeg=4;
Horse.iHead=1; // Hopefully just one, or the horse is a dud. :)
Horse.iArms=0;
Horse.iTail=1;

You could create all sorts of "Animals", with different attributes.

And to find out how many legs the horse has, create an 'int' variable, (integer), then call the function:-

int iHorseLegNum=Horse.GetNumLegs();

It all seems complicated at first, but as mentioned, very powerful.
There's a lot more to creating a class, so ignore that for now, but read up on C functions, contained in the 'Standard C libraries'. ( In particular, "stdlib.h" for now. It contains many very useful functions)
And look in the libraries included with arduino for good code examples - they're mostly written in C and/or C++.
C++ is my favourite, of all the computer languages I've learned.

Here's a link to the ATMega328 datasheet, which is useful for C programming with an Arduino, when you get right into it. :-
http://www.atmel.com/Images/doc8161.pdf

And a link to the ATMega2560 datasheet:-
http://www.atmel.com/Images/Atmel-2...r-ATmega640-1280-1281-2560-2561_datasheet.pdf

Finally, don't be put off by the apparent complexity of programming in C - just build up slowly, working from the Arduino libraries as examples. And ask me or the forum in general, in the Microcontroller section, if you have a problem. (I won't be able to answer a lot of questions that relate to ATMel/AVR chips. I'm best with PICs.)
 
Last edited:
Just looking through the Arduino reference, the "String" class is supported, but it will be very heavy on program memory, as are a lot of the other Arduino commands and data types/classes.
Anywhere that you can use char and the associated C character-handling functions I mentioned, you'll be saving a lot of program space.
ie String.length() will use more space than strlen() and String.concat() will use more space than strcat(). Inside the "String" class, for example, strlen() is most probably used anyway. You just won't see it.
 
MAN! On one side, that is COMPLEX as F**K! On the other, I SORT of understand it! :confused:I'm bobbing around at the minute so have only REALLY scanned it. I'm printing it off now & I'm gonna read it properly tonight. At the same time, I'm gonna do those graphs for you without fail. What I was thinking, if you're up for it, while I'm trying to learn Arduino crossing with C, if I write my sketches in as far as I understand, then post them to you, would you be kind enough to swap them to C? The reason I'm asking is NOT to ask you to do my work for me! What I'm thinking is if I can see what I'm writing that I understand, then see it the way it's SUPPOSED to be in C; it MIGHT make it easier! If that's wrong, please say! And ALSO, if you've got ENOUGH goins on with doing your own projects & stuff, I TOTALLY understand mate. PLEASE don't think I'll get upset if you say look mate, sorry but I haven't got the time to do all that as well. Because I'd be more p****d off if you didn't say it. I can't thank you enough for the help so far.:);):p:D
 
I'm NOT asking you to re~write my whole thing. I'm saying go thru it & say "this would be written like this & that like that". BUT, like I say, I know you've got you're own projects. So will NOT take offence.
 
P.S. Do you know anything about Raspberry Pi & TFT LCD Screens? Ive got a raspberry Pi B & I bought a 3.5" TFT LCD screen for it. Ive got the SD card for NOOBS etc. Why is it, when I plug the screen straight onto the board, I only get a blank white screen?:mad::mad::mad::mad::mad:
 
MAN! On one side, that is COMPLEX as F**K! On the other, I SORT of understand it! :confused:I'm bobbing around at the minute so have only REALLY scanned it. I'm printing it off now & I'm gonna read it properly tonight. At the same time, I'm gonna do those graphs for you without fail. What I was thinking, if you're up for it, while I'm trying to learn Arduino crossing with C, if I write my sketches in as far as I understand, then post them to you, would you be kind enough to swap them to C? The reason I'm asking is NOT to ask you to do my work for me! What I'm thinking is if I can see what I'm writing that I understand, then see it the way it's SUPPOSED to be in C; it MIGHT make it easier! If that's wrong, please say! And ALSO, if you've got ENOUGH goins on with doing your own projects & stuff, I TOTALLY understand mate. PLEASE don't think I'll get upset if you say look mate, sorry but I haven't got the time to do all that as well. Because I'd be more p****d off if you didn't say it. I can't thank you enough for the help so far.:);):p:D
I could do that with short sketches, or snippets of code. But I need to get used to the Arduino language first. I haven't done much yet. No point until I actually have the UNO.
(And don't get too advanced too fast with Arduino, or you'll beat me and I won't be able to translate it to C.)

Already, I'm better at 'Processing' than I am at 'Arduino'. 'Processing' is based on Java, in the same way that Arduino is based on C and C++. More C++ than C, too. The lib files have a *.cpp extension. C uses *.c

Speaking of Processing, I fixed the flicker in the Brain Wave Grapher when updating the bars. Smooth as silk. It's completely finished now.
 
Status
Not open for further replies.
Top