I'm guessing using the Serial.write function then makes me use the hardward serial pins which is what I want to avoid.
Code: Select all
#include <NewSoftSerial.h>
NewSoftSerial softSerial(4,5); // 4 is the rx and 5 the tx
int target = 0;
byte serialBytes[2]; // declare an array for the speed command
void setup()
{
softSerial.begin(9600);
pinMode(4, INPUT);
pinMode(5, OUTPUT);
}
void loop()
{
while(target < 4000){
serialBytes[0] = 0xC0 + ( target & 0x1F); // Command byte holds the lower 5 bits of target.
serialBytes[1] = (target >> 5) & 0x7F; // Data byte holds the upper 7 bits of target.
//Send a Pololu JRK21v3 Protocol command
softSerial.print(0xAA,BYTE); //start byte
softSerial.print(0x0B,BYTE); //device id - default is 11
softSerial.print(0x40,BYTE); //command 40 is Set Target High Resolution
softSerial.print(serialBytes[0], BYTE); //speed
softSerial.print(serialBytes[1], BYTE); //speed
target = target + 1000;
delay(5000);
}
target = 0;
}