Go Down

Topic: SoftwareSerial only rx Pin cause no more pins left (Read 1 time) previous topic - next topic

Robohac

Dec 20, 2012, 11:56 pm Last Edit: Dec 20, 2012, 11:57 pm by Robohac Reason: 1
Hi there,

im in the middle of a small project. For that project i have a stepper unit consisting out of two steppers, one uln2803, and two Attiny45. (ofcourse an arduino :D)

My problem is: I wanna drive one stepper with one attiny. This uses 4 pins. So scratch 4 of the five pins. Theres only one pin left for commuication from the arduino to the attiny. I wanned to use the softwareserial library for that on the attiny. But theres a problem, i have to declare a RX and a TX pin, but i only have room for my RX pin.

I found a version of the SoftwareSerial library, which only uses a TX pin, but not one that only recieves. (librarie can be fund in THIS post.)

Sadly i cant moddify the library myself, because i dont fully understand how those work.

Has anyone some tipps / a solution for me?

- Robohac

robtillaart


IIRC, you can use the standard SoftSerial library and declare the pin you do not want to use as pin -1.

give it a try.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Robohac

Ok so i wrote the first bit of code. One problem though: It dosnt work :D but im tired and just wanna show what ive done so far (ill correct the mistakes tomorrow) I think the mistake is in the function stringtonumber().

Code: [Select]
// Modes:
// R (Rotate)   format: R<Ticks>;<Speed>;
// S (Spinn)    format: S<Speed>;<Time>;
// C (Constant) format: C<r/l>;<Speed>;

#include <SoftwareSerial.h>

//declare variables for the motor pins
int motorPin1 = 3; // Blue   - 28BYJ48 pin 1
int motorPin2 = 4; // Pink   - 28BYJ48 pin 2
int motorPin3 = 2; // Yellow - 28BYJ48 pin 3
int motorPin4 = 1; // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)
                       
int rxPin = 0;         //Serial recieve pin 
                       
int motorSpeed = 0;     //variable to set stepper speed

SoftwareSerial mySerial(rxPin, -1); // RX, TX

String cue = "";

void setup() {
  //declare the motor pins as outputs
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);

}

void loop() {
  while (mySerial.available()>0) {
    cue = cue + getData();
  }
  switch (cue.charAt(0)) {
    case 'R':
      rotate();
      break;
    case 'S':
      spinn();
      break;
    case 'C':
      constant();
      break;     
  }
 
}

void counterclockwise (){
  // 1
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(motorSpeed);
  // 2
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay (motorSpeed);
  // 3
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(motorSpeed);
  // 4
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(motorSpeed);
  // 5
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(motorSpeed);
  // 6
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, HIGH);
  delay (motorSpeed);
  // 7
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(motorSpeed);
  // 8
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(motorSpeed);
}

void clockwise(){
  // 1
  digitalWrite(motorPin4, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin1, LOW);
  delay(motorSpeed);
  // 2
  digitalWrite(motorPin4, HIGH);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin1, LOW);
  delay (motorSpeed);
  // 3
  digitalWrite(motorPin4, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin1, LOW);
  delay(motorSpeed);
  // 4
  digitalWrite(motorPin4, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin1, LOW);
  delay(motorSpeed);
  // 5
  digitalWrite(motorPin4, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin1, LOW);
  delay(motorSpeed);
  // 6
  digitalWrite(motorPin4, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin1, HIGH);
  delay (motorSpeed);
  // 7
  digitalWrite(motorPin4, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin1, HIGH);
  delay(motorSpeed);
  // 8
  digitalWrite(motorPin4, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin1, HIGH);
  delay(motorSpeed);
}

String getData() {
  String input = "";
  while(mySerial.available()>0) {
    input = input + mySerial.read();
  }
  return input;
}

void deleteOld() {
  if(cue.length() != 0) {
    int n = 1;
    for (n = 1; n < cue.length(); n++) {
      if (cue.charAt(n) == ';') {
        break;
      }
    }
    cue = cue.substring(n + 1);
  }
}

void rotate() {
}

void spinn() {
}

void constant() {
  char direct = cue.charAt(1);
  int paramlength = 0;
  for (paramlength = 3; paramlength < cue.length(); paramlength++) {
    if (cue.charAt(paramlength) == ';') {
      break;
    }
  }
  paramlength = paramlength - 3;
  motorSpeed = stringtonumber(cue.substring(3, paramlength + 3));
  if (direct == 'r') {
    while (mySerial.available()==0) {
      clockwise();
    }
  } else {
    while (mySerial.available()==0) {
      counterclockwise();
    }
  }
  deleteOld();
}

int stringtonumber(String snumber) {
  int number = 0;
  for (int z = 0; z < snumber.length(); z++) {
    number = (number*10) + (snumber.charAt(z) - 48);
  }
  return number;
}

PeterH

You don't say what's wrong, but the way you're receiving the command string looks unsafe to me.

Firstly, you using the String class, which isn't giving you any benefit here and introduces the possibility of memory leaks and resulting stability problems.

Secondly, you aren't waiting until the whole command string is available before you try to parse it.

It would make far more sense to accumulate the incoming chars in a char array until you recognise the terminating ';' and then process the whole command.

As far as SoftwareSerial goes, you need to arrange for the SoftwareSerial to have a zero value for _transmitBitMask. This value is looked up in a board-specific way from the pin number you supply and I don't know whether there are any pin numbers that translate to zero. You could always call digitalPinToBitMask() with a few pin numbers and see whether any came back with zero. If you find any that do, you could use that as your TX pin. Otherwise, I suppose you could modify SoftwareSerial::setTX() and SoftwareSerial::setRX() to test the supplied pin number and set the port and bitmask properties to zero if an out-of-range value was supplied.
I only provide help via the forum - please do not contact me for private consultancy.

HazardsMind

#4
Dec 21, 2012, 06:19 pm Last Edit: Dec 21, 2012, 06:27 pm by HazardsMind Reason: 1
Do you need to use pins 0 and 1 or can you possibly move somethings around. The easiest way of solving your problem is to see what goes in and what comes out. However, you would need the serial monitor for that, meaning the Tx pin.

Code: [Select]
int stringtonumber(String snumber) {
  int number = 0;
  for (int z = 0; z < snumber.length(); z++) {
    number = (number*10) + (snumber.charAt(z) - 48);
    Serial.print(snumber);
    Serial.print(", ");
    Serial.println(number);
  }
  return number;
}
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Go Up