4 wheel drive car driven using attention levels

I have set up the Neurosky Mindwave headset to an BlueSMiRF silver bluetooth modom. I am trying to use the attention levels of the EEG headset to control the direction of the 4WD Arduino car ( forward, backwards, left, right, and stop). I would like to know what type of function I can use in-order to get a range of number to do a specific action for example the EEG headset has a range of 0 to 100 and I would like for it to go forward when the attention level is between 10 and 30 and when the levels are between 60 to 80 to go left for example.

thank you

arduino_code_1.4.2.ino (1.61 KB)

You want your code to do something if something else is true, like a value is between 60 and 80?

Have a look at the 'if' statement. if - Arduino Reference
You already have loads of them in your code.

You'll probably also need '&&' (and) so you can say if (x >=60 && x <= 80).

Steve

I'd not heard of mindwave before and noted it wasn't shown in the IDE library manager. I searched and downloaded it from github and had a look. I'm not sure your original code would have compiled with that lib(maybe I'm missing something...) I tried to write something based on the lib. Sorry if I misunderstood or it's not what you're after. This compiles but whether it works or not, I don't know.

You'll have to tweak the xxx_LOW and xxx_HIGH definitions near the top to match attention levels to actions.

Nifty gizmo from the sounds of it.

//code trial 1
#include <Mindwave.h> 

#define NUM_ATTNS       5
//
//
#define FWD_LOW         85
#define FWD_HIGH        100
//
#define RIGHT_LOW       60
#define RIGHT_HIGH      75
//
#define LEFT_LOW        40
#define LEFT_HIGH       55
//
#define BACK_LOW        20
#define BACK_HIGH       35
//
#define STOP_LOW        0
#define STOP_HIGH       15

//int constrainedInput = constrain(input, minimumValue, maximumValue);
const int left_motor_f = 13;
const int left_motor_r = 12;
const int right_motor_f = 11;
const int right_motor_r = 10;

bool
    bmwNewData;
int 
    attention = 0;

Mindwave mindwave;

typedef struct 
{
    byte    Command;
    byte    Low;
    byte    High;
    
}struct_MwAttentions;

struct_MwAttentions MwAttentions[NUM_ATTNS] = 
{
    {
        .Command = 'F',
        .Low = FWD_LOW,
        .High = FWD_HIGH        
    },
    {
        .Command = 'R',
        .Low = RIGHT_LOW,
        .High = RIGHT_HIGH        
    },
    {
        .Command = 'L',
        .Low = LEFT_LOW,
        .High = LEFT_HIGH        
    },
    {
        .Command = 'B',
        .Low = BACK_LOW,
        .High = BACK_HIGH        
    },
    {
        .Command = 'S',
        .Low = STOP_LOW,
        .High = STOP_HIGH        
    }
};

void setup()
{
    Serial.begin( MINDWAVE_BAUDRATE );
    //mindwave.setDebug(true);
  
    pinMode(13, OUTPUT);
    pinMode(12, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(10, OUTPUT);
    //Serial.begin(57600);
    bmwNewData = false;

}//setup

void MindwaveCallback( void )
{
    attention = mindwave.attention();
    bmwNewData = true;
    
}//MindWaveCallback;

void loop()
{    
    mindwave.update( Serial, MindwaveCallback );
    if( bmwNewData )
    {
        bmwNewData = false;
        
        switch( AssignAttentionLevel() )
        {
            case    'F':
                // ALL MOTORS GO FORWARTS
                //Serial.print(attention);
                digitalWrite(13, HIGH);
                digitalWrite(12, LOW);
                digitalWrite(11, HIGH);
                digitalWrite(10, LOW); 
            break;

            case    'B':
                // ALL MOTORS GO BACKWORDS
                //Serial.print(attention);
                digitalWrite(13, LOW);
                digitalWrite(12, HIGH);
                digitalWrite(11, LOW);
                digitalWrite(10, HIGH);  
            break;

            case    'L':
                // TURNS RIGHT 
                //Serial.print(attention);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                digitalWrite(11, HIGH);
                digitalWrite(10, LOW);  
            break;

            case    'R':
                // TURNS RIGHT 
                //Serial.print(attention);
                digitalWrite(13, HIGH);
                digitalWrite(12, LOW);
                digitalWrite(11, LOW);
                digitalWrite(10, LOW);
            break;

            case    'S':
                // STOPS CAR 
                //Serial.print(attention);
                digitalWrite(13, LOW);
                digitalWrite(12, LOW);
                digitalWrite(11, LOW);
                digitalWrite(10, LOW);
            break;
                
        }//switch
        
    }//if
        
}//loop

byte AssignAttentionLevel( void )
{
    for( int i=0; i<NUM_ATTNS; i++ )
    {
        if( (attention >= MwAttentions[i].Low) && (attention >= MwAttentions[i].High) )
             return( MwAttentions[i].Command );
             
    }//for

    //if no match found, command a stop
    return ('S');

}//AssignAttentionLevel