Shop OBEX P1 Docs P2 Docs Learn Events
Arduino uno, adafruit motor shield and colorPAL — Parallax Forums

Arduino uno, adafruit motor shield and colorPAL

Hello, I am building a robot using arduino uno, a motor shield and the sensor colorPAL. I've got colorPAL demo code from  http://learn.parallax.com/colorpal-arduino-demo, and it worked properly. Then I made a code for the motors, and robot went forward. All ok till now.  Then I wrote an if statement, using data obtained from colorPAL. Unfortunately, motorE, connected in port M1 of motor shield, stopped working. Could someone explain for me why if statement is causing that trouble for M1 port? Motor shield uses digital pins 3,4,5,6,7,8,9,10,11,12 of arduino board.Here's the code:
#include <AFMotor.h>AF_DCMotor motorE(1);AF_DCMotor motorD(2);#include <SoftwareSerial.h>
const int sio = 2;            // ColorPAL connected to pin 2const int unused = 255;         // Non-existant pin # for SoftwareSerialconst int sioBaud = 4800;const int waitDelay = 200;
// Received RGB values from ColorPALint red;int grn;int blu;
// Set up two software serials on the same pin.SoftwareSerial serin(sio, unused);SoftwareSerial serout(unused, sio);
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);}
void loop() {  readData();  motorD.run(FORWARD);  motorE.run(FORWARD);  motorD.setSpeed(180);  motorE.setSpeed(180);  if((red>130)&&(red<150)&&(grn>110)&&(grn<130)&&(blu>150)&&(blu<170)){  motorD.run(FORWARD);  motorE.run(FORWARD);  motorD.setSpeed(180);  motorE.setSpeed(180);  delay(2000);}  
}


// Reset ColorPAL; see ColorPAL documentation for sequencevoid 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 = serin.read();        if (buffer == '$')               // Return early if $ character encountered          return;      }      parseAndPrint(buffer);      delay(10);    }  }}
// Parse the hex data into integersvoid 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);}

Comments

  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2015-07-30 22:08
    While the shield may not use pin2, the PWM functions for the motor control may use a timer that conflicts with the timer that the SoftwareSerial library uses. This is the fundamental limitation of a single-core microcontroller that uses just a couple of hardware interrupts and timers. Everything needs to play nice.
    If it is an internal timer conflict, you could try a different software serial library in the hopes of locating one that plays along. Or you could try using the hardware serial in pins 0 and 1. You'll probably need to temporarily disconnect the ColorPAL while uploading your sketches, and you will not be able to use the Serial Monitor for debugging feedback. Another option is to use the Arduino Mega, which has more timers and hardware serial ports.

Sign In or Register to comment.