Go Down

Topic: Coding Issues - Serial Communication (Read 1 time) previous topic - next topic

Yogurt

Currently I'm having some troubles with serial communication between two arduinos. I can send and receive the data however the receiving end seems to be getting a lot of interference since the motors are turning when they shouldn't be.

Transmitter.
Code: [Select]

#include <PS2X_lib.h>  //for v1.6


PS2X ps2x; // create PS2 Controller Class

// Arrays, 0 = Left, 1 = Right, 2 = Vertical

int ledPin = 3;
int error = 0;
int data[] = {
  0, 0};
int pwm[] = {
  0, 0, 0};
int dir[] = {
  0, 0, 0};
byte type = 0;
int sync = 0xAA;
int led1 = 0xBB;
int led2 = 0xCC;

void setup(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);

  error = ps2x.config_gamepad(13,10,11,12, true, true);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
}

void loop(){

  ps2x.read_gamepad();



  data[0] = (ps2x.Analog(PSS_LY));
  data[1] = (ps2x.Analog(PSS_RY));

  for(int index = 0; index < 2; index++){
    if(data[index] <= 104){
      pwm[index] = map(data[index], 104, 0, 0, 255);
      dir[index] = 1;
    }
    else if(data[index] >= 150){
      pwm[index] = map(data[index], 150, 255, 0, 255);
      dir[index] = 0;
    }

    else{
      pwm[index] = 0;
      dir[index] = 0;
    }
  }

  if(ps2x.Button(PSB_L2)){
    pwm[2] = 255;
    dir[2] = 0;
  }
  else if(ps2x.Button(PSB_R2)){

    pwm[2] = 255;
    dir[2] = 1;
  }
  else{
    pwm[2] = 0;
    dir[2] = 0;
  }



  for(int index = 0; index < 3; index++){

    Serial.write(sync);
    Serial.write(index);
    Serial.write(pwm[index]);
    Serial.write(dir[index]);

  }
      delay(50);
  if(Serial.available() > 0 && Serial.read() == led1)
    digitalWrite(ledPin, HIGH);                       

  else
    digitalWrite(ledPin, LOW);
   
}


Receive:
Code: [Select]

int inData[4];  // Allocate some space for the Bytes
int index;
int pwm;
int dir;
int motorPin[] = {
  6, 9, 10, 11};
int dirPin[] = {
  2, 3, 4, 5};
int sync = 0xAA;
int led1 = 0xBB;
int led2 = 0xCC;

// 0 = left, 1 = right, 2 = vertical

void setup(){
  Serial.begin(9600);
  for (int x = 0; x < 4; x++){
    pinMode(motorPin[x], OUTPUT);
    pinMode(dirPin[x], OUTPUT);
  }
}

boolean recieve(){
  if(Serial.available() > 0 && Serial.read() == sync){ //If there is a Serial message waiting and the Serial is sync, then proceed
    delay(2);
    index = Serial.read(); // index
    delay(2);
    pwm = Serial.read(); // pwm value
    delay(2);
    dir = Serial.read(); // dir value
    delay(2); // YOU NEED DELAY, delay prevents garbled messages. (saves you from headaches)

    return true;
  }
  else
    return false;
}





void loop()
{
  if (recieve()){
    Serial.print(led1, BYTE);

    if(dir == 1)
      dir = HIGH;
    else if(dir == 0)
      dir = LOW;

    switch(index){

    case 0:
      analogWrite(motorPin[0], pwm);
      digitalWrite(dirPin[0], dir);


    case 1:
      analogWrite(motorPin[1], pwm);
      digitalWrite(dirPin[1], dir);


    case 2:
      analogWrite(motorPin[2], pwm);
      analogWrite(motorPin[3], pwm);
      digitalWrite(dirPin[2], dir);
      digitalWrite(dirPin[3], dir);


    }

  }
  else
    Serial.print(led2, BYTE);
}




Please help!

PaulS

Code: [Select]
  if(Serial.available() > 0 && Serial.read() == sync){ //If there is a Serial message waiting and the Serial is sync, then proceed
    delay(2);
    index = Serial.read(); // index
    delay(2);
    pwm = Serial.read(); // pwm value
    delay(2);
    dir = Serial.read(); // dir value
    delay(2); // YOU NEED DELAY, delay prevents garbled messages. (saves you from headaches)

The last comment is wrong. No, make that WRONG!!!!

The delay is not saving you headaches. It is causing you headaches.

You are testing that there is at least one character available to read, and then reading all 4 of them. The receive() function should test that Serial.available() returned a value greater than or equal to 4 before doing any reading. Then you get get rid of the delays that are masking the problem sometimes.

Some capacitors on your motors, to cut down on noise, might be needed, too.
The art of getting good answers lies in asking good questions.

AgeingHippy

I have not looked at your code, but personally I have found using a separate power source for your motors (relative to the power-source for your micro controller) seems to improve spurious movement on the motors. Does for me anyway. NB - I am a noob and don't know what I am doing...

Go Up