I'm working on a sip and puff controller for an RC boat using 2 Unos. I had troubles with getting the NRF24L01 communications working a month or so ago but that is now working.
When I sip or puff on a tube the serial monitor shows me that I am going forward, idle or reverse, right now I'm just trying to get this part working. I want to send that variable's text to the receiving Uno. These are the two variables used:
enum DRIVE_STATE {FORWARD, REVERSE, IDLE}; // Enumeration, FORWARD - 1, REVERSE = 2, IDLE = 3
char *DRIVE_STATE_STRS[] = {"forward", "reverse", "idle"}; // ? Defines words so that when Drive state = 1 Serial monitor will display Forward, etc
I added this commands to send the text"
char text[20];
text[]= DRIVE_STATE_STRS[driveState];
radio.write(&text, sizeof(text));
I get the following error
"expected primary-expression before ']' token"
How do I format that variable so I can send the drive status to the receiving Uno?
Here is the complete code
/* Started with sipPuffToyCar_Mod example
* added NRF24L01 communications
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
#define ThrottleTrimInput A2
#define SteeringTrimInput A3
#define SENSOR_PIN A5
#define ThrottleOutput 5
#define SteeringOutput 6
#define FORWARD_PIN 7
#define REVERSE_PIN 8
#define LEFT_PIN 9
#define RIGHT_PIN 10
#define SIP_THRESHOLD 450 //analog val
#define PUFF_THRESHOLD 550 //analog val
#define POLL_INTERVAL 10 //ms
#define SHORT_ACTION_MIN 100 //ms
#define SHORT_ACTION_MAX 750 //ms
#define ACTION_SPACE_MIN 50 //ms
#define ACTION_SPACE_MAX 750 //ms
uint32_t sipStartTime;
uint8_t sipStarted = 0;
uint32_t lastSipTime;
uint32_t lastSipDuration;
uint32_t puffStartTime;
uint8_t puffStarted = 0;
uint32_t lastPuffTime;
uint32_t lastPuffDuration;
uint8_t ThrottleOutValue = 0;
uint8_t ThrottleTrimValue = 0;
uint8_t SteeringOutValue = 135;
uint8_t SteeringTrimValue = 0;
uint32_t SteeringDelay = 0;
uint32_t previousMillis = 0;
uint32_t StartSteeringDelay = 50;
uint8_t SteeringPause = 0;
uint32_t SteeringPauseDelay = 1000;
uint32_t SteeringPauseStart = 0;
enum DRIVE_STATE {FORWARD, REVERSE, IDLE}; // Enumeration, FORWARD - 1, REVERSE = 2, IDLE = 3
char *DRIVE_STATE_STRS[] = {"forward", "reverse", "idle"}; // ? Defines words so that when Drive state = 1 Serial monitor will display Forward, etc
uint8_t driveState = IDLE;
//
void setDrive(uint8_t state)
{
pinMode(FORWARD_PIN, OUTPUT);
pinMode(REVERSE_PIN, OUTPUT);
if (driveState == state)
return;
if (state == FORWARD)
{
digitalWrite(FORWARD_PIN, HIGH);
Serial.print (" Forward Pause = ");
Serial.print (SteeringPause);
Serial.print (" Time = ");
Serial.println (SteeringPauseStart);
}
else if (state == REVERSE)
{
digitalWrite (REVERSE_PIN, HIGH);
Serial.print (" Reverse Pause = ");
Serial.print (SteeringPause);
Serial.print (" Time = ");
Serial.println (SteeringPauseStart);
}
else
{
digitalWrite (FORWARD_PIN, LOW);
digitalWrite (REVERSE_PIN, LOW);
}
driveState = state;
}
//
void toggleReverse()
{
if (driveState == REVERSE)
return;
else if (driveState == IDLE)
setDrive(REVERSE);
else
setDrive(IDLE);
}
//
void toggleForward()
{
if (driveState == FORWARD)
return;
else if (driveState == IDLE)
setDrive(FORWARD);
else
setDrive(IDLE);
}
enum STEERING_STATE {LEFT, RIGHT, CENTER};
char *STEERING_STATE_STRS[] = {"left", "right", "center"};
uint8_t steeringState = CENTER;
//
void setSteering(uint8_t state)
{
pinMode(LEFT_PIN, OUTPUT);
pinMode(RIGHT_PIN, OUTPUT);
if (steeringState == state)
return;
if (state == LEFT)
// && (millis() - SteeringPauseStart) > SteeringPauseDelay // This line was added to the If condition statement above
{
Serial.print (" Left Pause = ");
Serial.print (SteeringPause);
Serial.print ("Pause Time = ");
Serial.println (millis() - SteeringPauseStart);
digitalWrite(LEFT_PIN, HIGH);
}
else if (state == RIGHT)
// && (millis() - SteeringPauseStart) > SteeringPauseDelay // This line was added to the If condition statement above
{
Serial.print (" Right Pause = ");
Serial.print (SteeringPause);
Serial.print (" Pause Time = ");
Serial.println (millis() - SteeringPauseStart);
digitalWrite(RIGHT_PIN, HIGH);
}
else
{
digitalWrite(LEFT_PIN, LOW);
digitalWrite(RIGHT_PIN, LOW);
SteeringPauseStart = millis();
SteeringPause = 0;
Serial.print (" Idle Pause = ");
Serial.print (SteeringPause);
Serial.print (" Pause Time = ");
Serial.println (millis() - SteeringPauseStart);
}
steeringState = state;
} // end void setSteering(uint8_t state)
//
void setup()
{
Serial.begin(9600);
radio.begin();
radio.setDataRate(RF24_1MBPS); //added to original example
radio.setChannel(96); //added to original example
radio.setPALevel(RF24_PA_MIN);
radio.openWritingPipe(address);
radio.stopListening();
while (1) // Constantly run While Loop
{
int16_t val = analogRead(SENSOR_PIN); // Read Pressure Switch < 450 = SIP, > 550 = Puff
if (!sipStarted && val < SIP_THRESHOLD)
{
SteeringPause = 1;
sipStarted = 1;
sipStartTime = millis(); //Store Sip Start Time
}
else if (sipStarted && val > SIP_THRESHOLD) // SIP has Stopped
{
sipStarted = 0;
uint32_t duration = millis() - sipStartTime;
if (duration > SHORT_ACTION_MIN)
{
uint32_t prevLastSipTime = lastSipTime;
uint32_t prevLastSipDuration = lastSipDuration;
lastSipTime = millis();
lastSipDuration = duration;
uint32_t space = sipStartTime - prevLastSipTime;
//two shorts in a row
if (prevLastSipDuration < SHORT_ACTION_MAX &&
lastSipDuration < SHORT_ACTION_MAX &&
space > ACTION_SPACE_MIN && space < ACTION_SPACE_MAX)
{
toggleReverse();
}
}
}
if (!puffStarted && val > PUFF_THRESHOLD)
{
SteeringPause = 1;
puffStarted = 1;
puffStartTime = millis();
}
else if (puffStarted && val < PUFF_THRESHOLD)
{
puffStarted = 0;
uint32_t duration = millis() - puffStartTime;
if (duration > SHORT_ACTION_MIN)
{
uint32_t prevLastPuffTime = lastPuffTime;
uint32_t prevLastPuffDuration = lastPuffDuration;
lastPuffTime = millis();
lastPuffDuration = duration;
uint32_t space = puffStartTime - prevLastPuffTime;
//two shorts in a row
if (prevLastPuffDuration < SHORT_ACTION_MAX &&
lastPuffDuration < SHORT_ACTION_MAX &&
space > ACTION_SPACE_MIN && space < ACTION_SPACE_MAX)
{
toggleForward();
}
}
}
//update steering
if (sipStarted)
setSteering(LEFT);
else if (puffStarted)
setSteering(RIGHT);
else
setSteering(CENTER);
// Set Throttle Output with Trim
ThrottleTrimValue = map(analogRead(ThrottleTrimInput), 0, 1024, 85, 135) - 110;
if(DRIVE_STATE_STRS[driveState]== "forward") {
ThrottleOutValue = (240 - ThrottleTrimValue) ;
analogWrite(ThrottleOutput, ThrottleOutValue);
// Serial.print(" Throttle Out = ");
// Serial.print(ThrottleOutValue);
}
else if (DRIVE_STATE_STRS[driveState]== "reverse") {
ThrottleOutValue = (30 - ThrottleTrimValue);
analogWrite(ThrottleOutput, ThrottleOutValue);
// Serial.print(" Throttle Out = ");
// Serial.print(ThrottleOutValue);
}
else{
ThrottleOutValue = (135 - ThrottleTrimValue);
analogWrite(ThrottleOutput, ThrottleOutValue);
// Serial.print(" Throttle Out = ");
// Serial.print(ThrottleOutValue);
}
// Set Steeering Output with Trim
SteeringTrimValue = map(analogRead(SteeringTrimInput), 0, 1024, 85, 135) - 110;
// Delay for Steering change
unsigned long currentmillis = millis();
int CenterStop ;
if (currentmillis - previousMillis > StartSteeringDelay){
previousMillis = currentmillis;
CenterStop = (135 + SteeringTrimValue);
if((STEERING_STATE_STRS[steeringState]== "right")&& (SteeringOutValue < 230)) {
SteeringOutValue = ((SteeringOutValue + 10) );
analogWrite(SteeringOutput, SteeringOutValue);
}
if ((STEERING_STATE_STRS[steeringState]== "left") && (SteeringOutValue > 40)) {
SteeringOutValue = ((SteeringOutValue - 10) );
analogWrite(SteeringOutput, SteeringOutValue);
}
if (STEERING_STATE_STRS[steeringState]== "center"){
if (SteeringOutValue - CenterStop > 2) {
SteeringOutValue = ((SteeringOutValue) - 5);
analogWrite(SteeringOutput, SteeringOutValue);
}
if (SteeringOutValue - CenterStop < -2){
SteeringOutValue = ((SteeringOutValue) + 5);
analogWrite(SteeringOutput, SteeringOutValue);
}
} // End of CENTER if
} // End of Main Steering If loop
Serial.print(" drive: ");
Serial.println(DRIVE_STATE_STRS[driveState]);
char text[20];
text[]= DRIVE_STATE_STRS[driveState];
radio.write(&text, sizeof(text));
// Serial.print(" Throttle Out =" );
// Serial.print(ThrottleOutValue);
// Serial.print(" Throttle Trim =" );
// Serial.print(ThrottleTrimValue);
// Serial.print(" Steering Out ");
// Serial.print (SteeringOutValue);
// Serial.print(" Steering Trim =" );
// Serial.print(SteeringTrimValue);
// Serial.print(" CenterStop = " );
// Serial.println(CenterStop);
// Serial.print (" Steering - Center = ");
// Serial.print(SteeringOutValue - CenterStop);
Serial.print(" Steering State = ");
Serial.println(STEERING_STATE_STRS[steeringState]);
// Serial.print(" Drive State = ");
// Serial.println(DRIVE_STATE_STRS[driveState]);
delay(POLL_INTERVAL);
// if (sipStarted == 1){
// Serial.println ("Sip Started");
// StartSteeringDelay = 0;
// }
// else if (puffStarted ==1){
// Serial.println ("Puff Started");
// }
// else {}
// }
} // End of While;
} // End ot Setup Loop
//
void loop() {}
Thanks
John