Hello Folks!
My current Arduino sketch contains code that reads in bytes from Serial. The neat thing is that I made an instruction buffer. It’s defined to hold 8 instructions at the moment, but I can easily increase it to 64. This allows the Arduino to buffer 7 instructions, whether they be movement instructions or getstatus instructions, and execute them sequentially. The Arduino Serial buffer itself is only 63 characters, that means it will only hold 63 bytes before we start overflowing. Each instruction is 6 bytes long, including terminating character ‘;’. The SerialEvent() is capable of consuming the instructions on the serial buffer as quickly as they come. Eventually, we’ll have to keep track of ‘states’ of the telescope, so we can simultaneously read in instructions from serial, buffer them internally, execute movement instructions, do getstatus instructions, pause instructions, and break instructions if necessary, And what’s really neat is that I didn’t have to rely on a QueueList class, or too heavily on String class, meaning the sketch is currently only 4.2kb. With all the free space, we can add some math and functionality, where the Arduino can ‘learn’ about it’s own sensors and motors.
*Basically, the code is at a point where it can read in ‘;’ terminated byte-string-commands. It can store in memory up to 8 byte-string-commands, but it can be expanded to much more. The ASCOM telescope driver is capable of sending byte-string-commands. There may be problems later on regarding sending and interpreting only bytes.
**I apologize in advance for the wordwrapping and bad formatting. I’m not used to wordpress with code.
//David Harbottle
//May 03, 2014
//Motorized Telescope Project
//PCC Engineering Club
#define bufflen 8
#define codelen 16
//the queue works, but Arduino Serial buffer is still limited at 63 bytes
byte byteArray[bufflen][codelen] = { };
int stringCount = 0; //counts the number of instructions
int i = 0; //byeArray's 2nd index counter, used by SerialEvent
int current = 0; //byteArray's 1st index counter, current instruction to run
int nextIn = 0; //byteArray's 1st index counter, index of array to write the next instruction
void setup() {
// put your setup code here, to run once:
Serial.begin(19200);
pinMode(13, OUTPUT);
digitalWrite(13,LOW);
delay(1000);
Serial.println("start");
}
//round robin indexer
void currentPlus() {
current = current + 1;
if (current >= bufflen)
current = 0;
}
//round robin indexer
void nextInPlus() {
nextIn = nextIn + 1;
if (nextIn >= bufflen)
nextIn = 0;
}
void loop() {
if (current!=nextIn)
{
Serial.write("instructions left : ");
Serial.print(stringCount);
Serial.write('\t');
switch (byteArray[current][0])
{
case 48:
Serial.write("You sent a char '0'.\t");
break;
case 49:
Serial.write("You sent a char '1'.\t");
break;
case 50:
Serial.write("You sent a char '2'.\t");
break;
case 51:
Serial.write("You sent a char '3'.\t");
break;
case 52:
Serial.write("You sent a char '4'.\t");
break;
case 53:
Serial.write("You sent a char '5'.\t");
break;
case 54:
Serial.write("You sent a char '6'.\t");
break;
case 55:
Serial.write("You sent a char '7'.\t");
break;
case 56:
Serial.print("You sent a char '8'.\t");
break;
case 57:
Serial.write("You sent a char '9'.\t");
break;
default:
Serial.write("You sent a non-cmd : ");
Serial.write(byteArray[current][0]);
Serial.write('\t');
break;
}
//clear out the instruction code
for (int j = 0; j < codelen; j++) byteArray[current][j] = 0;
currentPlus();
stringCount--;
Serial.write(";\n");
}
delay(500);
}
void serialEvent()
{
byte inByte;
//keep adding instructions as long as there are bytes in serial available
//and while there is at least one empty spot for an instruction in the buffer
//this stringCount+1, because nextIn points to an empty spot in the buffer
while (Serial.available() && ((stringCount+1) < bufflen)){
inByte = Serial.read();
if (inByte == ';') {
nextInPlus();
stringCount++;
i = 0;
}
else {
byteArray[nextIn][i++] = inByte;
}
}
}
Additionally, I found a way to customize the color scheme of the Arduino IDE, Dark Arduino IDE by Jeff Thompson. It’s easier on the eyes.
We are getting closer to a working telescope driver!!!! So here’s an exciting gif!
And remember guys :“Say No To Fluff”
-david