Issues with Parallax Color pal

With the attached code i can get all but the Color sensor to behave properly. Ideally stepperS would step, then the Parallax RGB would read once, then stepperR would step(step-read-step-repeat).This seems to work. However, the RGB sensor stops printing data around 6-8 cycles. I have tried changing the "if" to a "while", but this seems to prevent the steppers from rotating after the first cycle.

Any trouble shooting ideas would be greatly appreciated.

I've also attached an image of the circuit. although i don't believe the problem is in the wiring

_4.0.ino (2.67 KB)

I really don't understand why it is so hard to simply post code.

#include <Stepper.h>
#include <SoftwareSerial.h>

 

#define STEPS 1000       //CHanges the speed
Stepper stepperS(STEPS,9,8);
Stepper stepperR(STEPS,5,4);
int         pin1Dir  = 9;          // Pin for stepperS DIR input
int         pin1Step = 8;          // Pin for stepperS STEP input
int         pin2Dir  = 5;          // Pin for stepperR DIR input
int         pin2Step = 4;          // Pin for stepperR  input


const int sio = 2;      // ColorPAL connected to pin 2
const int unused = 255;     // Non-existant pin # for SoftwareSerial
const int sioBaud = 4800;
const int waitDelay = 200;

// Received RGB values from ColorPAL
int red;
int grn;
int blu;

// Set up two software serials on the same pin.
SoftwareSerial serin(sio, unused);
SoftwareSerial serout(unused, sio);

// 2. Function setup() runs once when the board powers on.
void setup() {
  Serial.begin(9600);
  reset();          // Send reset to ColorPal
  serout.begin(sioBaud);
  pinMode(sio, OUTPUT);
  serout.print("= (00 $ m) !"); // Loop print values, see ColorPAL documentation
  serout.end();       // Discontinue serial port for transmitting

  serin.begin(sioBaud);         // Set up serial port for receiving
  pinMode(sio, INPUT);
  
  pinMode(pin1Dir,OUTPUT);         // Sets the pin as an output
  pinMode(pin1Step,OUTPUT);        // Sets the pin as an output
  //digitalWrite(pinDir,LOW);       // Sets the direction
   stepperS.setSpeed(60);
  pinMode(pin2Dir,OUTPUT);         // Sets the pin as an output
  pinMode(pin2Step,OUTPUT);        // Sets the pin as an output
   stepperR.setSpeed(60);
}

// 3. Function loop() runs repeatedly forever after setup()
void loop() {
 
     stepperS.step(1600);
       readData();
     stepperR.step(1600);
     delay(500);
 
}

void reset() {
  delay(200);
  pinMode(sio, OUTPUT);
  digitalWrite(sio, LOW);
  pinMode(sio, INPUT);
  while (digitalRead(sio) != HIGH);
  pinMode(sio, OUTPUT);
  digitalWrite(sio, LOW);
  delay(80);
  pinMode(sio, INPUT);
  delay(waitDelay);
}

void readData() {
  char buffer[32];
  
  if (serin.available() > 0) {
    // Wait for a $ character, then read three 3 digit hex numbers
    buffer[0] = serin.read();
    if (buffer[0] == '

) {
      for(int i = 0; i < 9; i++) {
        while (serin.available() == 0);    // Wait for next input character
        buffer[i] = serin.read();
        if (buffer[i] == '


)               // Return early if $ character encountered
          return;
      }
      parseAndPrint(buffer);
      delay(10);
    }
  }
}

// Parse the hex data into integers
void parseAndPrint(char * data) {
  sscanf (data, "%3x%3x%3x", &red, &grn, &blu);
  char buffer[32];
  sprintf(buffer, "R%4.4d G%4.4d B%4.4d", red, grn, blu);
  Serial.println(buffer);
}

This seems to work. However, the RGB sensor stops printing data around 6-8 cycles.

Why are you not printing to Serial what you read from the sensor?

I do not understand why you have two instances of SoftwareSerial to read from, and write to, one device.

PaulS:
I do not understand why you have two instances of SoftwareSerial to read from, and write to, one device.

The intent appears to be doing serial output and serial input on the same pin.

johnwasser:
The intent appears to be doing serial output and serial input on the same pin.

So, wouldn't

SoftwareSerial colorSensor(sio, sio);

accomplish the same thing?

PaulS:
So, wouldn't

SoftwareSerial colorSensor(sio, sio);

accomplish the same thing?

Depends on the implementation of SoftwareSerial. I'd be surprised if it was capable of switching pin direction on the fly. Using two separate instances and manually switching direction (with pinMode()) is more likely to work.

johnwasser:
Depends on the implementation of SoftwareSerial. I'd be surprised if it was capable of switching pin direction on the fly. Using two separate instances and manually switching direction (with pinMode()) is more likely to work.

That makes sense.