Hello Everyone,
I am so sorry to bother you but I need a little help with my code. For my senior design project I am using the neurosky mindwave mobile headset to gather attention values. From those attention values i want to do three things with my stepper motor: turn the motor clockwise, turn the motor counter clockwise, or have the motor be at rest.
I want to use an array of type byte in my code (code is below) to gather up to 5 values between the ranges shown in my switch cases. Once those five values are collected, I want to then send a signal to my motors. I am new to coding for arduino and I greatly appreciate your help at figuring out if this code looks decent and where to put the array. Thank you so much.
More info about the code: I got part of it from the Neurosky website's sample code and the other half from the seeed stepper motor driver wiki.
Thank You Again.
Here is the code:
#define LED 13
#define BAUDRATE 115200 // Sets the data rate in bits per second (baud) for serial data transmission.
#define DEBUGOUTPUT 0
#define LED13 0x0D //(Pin(of(Visual(indication( for(runing("heart(beat"(using(onboard(LED(
#define CW_LIMIT_ANGLE 0x3E8 //(({Hexadecimal}(lowest(clockwise(angle(is(1,(
as(when(set(to(0(it(set(servo(to(wheel(mode(
#define CCW_LIMIT_ANGLE 0xBB8 //( {Hexadecimal}( Highest( anitDclockwise(
angle(is(0XFFF,(as(when(set(to(0(it(set(servo(to(wheel(mode(
#include <Stepper.h>
const int stepsPerRevolution = 400;
int speedpinA=9;//enable motor A
int speedpinB=10;//enable motor B
int speed =127;//define the speed of motor
......
analogWrite(speedpinA,speed);//AnalogWrite to Generate PWM to control the motor speed
analogWrite(speedpinB,speed);
int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
......
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
Stepper myStepper(stepsPerRevolution, 8,11,12,13);
// checksum variables
byte generatedChecksum = 0;
byte checksum = 0;
int payloadLength = 0;
byte payloadData[64] = {
0};
byte poorQuality = 0;
byte attention = 0;
byte meditation = 0;
// system variables
long lastReceivedPacket = 0;
boolean bigPacket = false;
//////////////////////////
// Microprocessor Setup //
//////////////////////////
void setup() {
delay(1000); //for Stepper Motor
pinMode(LED,(OUTPUT);
pinMode(BLUESMIRFON,(OUTPUT);//initalize(BlueSMiRF
digitalWrite(BLUESMIRFON,(HIGH);//powertoBlueSMiRF
Serial.begin(BAUDRATE);//USB
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
delay(3000) ;
Serial.print(194,BYTE) ;
}
////////////////////////////////
// Read data from Serial UART //
////////////////////////////////
byte ReadOneByte() {
int ByteRead;
while(!Serial.available());
ByteRead = Serial.read();
#if DEBUGOUTPUT
Serial.print((char)ByteRead); // echo the same byte out the USB serial (for debug purposes)
#endif
return ByteRead;
}
/////////////
//MAIN LOOP//
/////////////
void loop() {
// Look for sync bytes
if(ReadOneByte() == 170) {
if(ReadOneByte() == 170) {
payloadLength = ReadOneByte();
if(payloadLength > 169) //Payload length can not be greater than 169
return;
generatedChecksum = 0;
for(int i = 0; i < payloadLength; i++) {
payloadData[i] = ReadOneByte(); //Read payload into memory
generatedChecksum += payloadData[i];
}
checksum = ReadOneByte(); //Read checksum byte from stream
generatedChecksum = 255 - generatedChecksum; //Take one's compliment of generated checksum
if(checksum == generatedChecksum) {
poorQuality = 200;
attention = 0;
meditation = 0;
for(int i = 0; i < payloadLength; i++) { // Parse the payload
switch (payloadData[i]) {
case 2:
i++;
poorQuality = payloadData[i];
bigPacket = true;
break;
case 4:
i++;
attention = payloadData[i];
break;
case 5:
i++;
meditation = payloadData[i];
break;
case 0x80:
i = i + 3;
break;
case 0x83:
i = i + 25;
break;
default:
break;
} // switch
} // for loop
#if !DEBUGOUTPUT
// *** Add your code here ***
if(bigPacket) {
if(poorQuality == 0)
digitalWrite(LED, HIGH);
else
digitalWrite(LED, LOW);
Serial.print("PoorQuality: ");
Serial.print(poorQuality, DEC);
Serial.print(" Attention: ");
Serial.print(attention, DEC);
Serial.print(" Time since last packet: ");
Serial.print(millis() - lastReceivedPacket, DEC);
lastReceivedPacket = millis();
Serial.print("\n");
switch(attention /3 ) {
case 0:
// where motor does not move
if(attention < 30, meditation < 30)
{
myStepper.setSpeed(0);
}
break;
case 1:
// where motor moves clockwise
If(attention >= 80, attention <=100) {
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
}
}
break;
case 2:
// where motor moves counter clockwise
If( attention > = 50, attention <=70 )
{
void loop() {
//step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
}
break;
}
}
#endif
bigPacket = false;
}
else {
// Checksum Error
} // end if else for checksum
} // end if read 0xAA byte
} // end if read 0xAA byte
}