This is further to the thread User editable look up table - Programming Questions - Arduino Forum
The project is almost on the verge of completion … the only hiccup is that I am unable to send via Serial large amounts of waveform data. In fact not more than about 200 bytes. Upto this count the data is received , parsed and I also get a confirmation on the number of bytes got. Beyond this the program does not confirm back. I know I must ask this on Teensy forum but the response is far better here
Possibly I should increase the Rx buffer ??
The full Teensy 3.2 code is here :
// Hardware used in Teensy3.2
#include <EEPROM.h>
# define StartPin 5
# define SerComPin 6
# define CycleOnLed 9
# define AliveLed 13
# define DACpin A14
int voltLUT[100] ; /* = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6, 26, 128, 256, 384, 512, 614, 665, 704, 742, 781,
806, 819, 845, 857, 870, 883, 883, 889, 902, 909,
915, 921, 921, 921, 921, 921, 921, 928, 941, 953,
998, 1024, 1088, 1152, 1408, 1856, 2175, 2559,
2847, 3084, 3321, 3558, 3794, 3839, 3903, 3967,
4031, 4095, 4095, 4095, 4095, 4095, 4095, 4095,
4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095,
4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095,
4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095,
4095, 4095
}; */ // Uncomment for first time load of profile..
int eeAddress = 0;
int lutIndex = 0;
unsigned long updateMillisec = millis();
unsigned long updateInterval = 10;
char startChar ;
bool sendLutViaSerial = false;
bool getLutViaSerial = false;
char incomingByte ;
int charIndex ;
int const charCount = 1000;
bool started, ended;
char LUTFromPC[charCount];
int userArray[100];
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void setup() {
Serial.begin(9600);
pinMode(StartPin, INPUT);
digitalWrite( StartPin, HIGH);
pinMode(SerComPin, INPUT);
pinMode(CycleOnLed, OUTPUT);
analogWriteResolution(12);
digitalWrite(CycleOnLed, HIGH);
delay(1000);
digitalWrite(CycleOnLed, LOW);
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void loop() {
bool startState = digitalRead(StartPin);
if (!startState) { // Check Cycle Start input.
digitalWrite(CycleOnLed, HIGH);
if (millis() - updateMillisec > updateInterval) {
updateMillisec = millis();
analogWrite(DACpin, voltLUT[lutIndex]);
Serial.println(voltLUT[lutIndex]);
lutIndex++ ;
if (lutIndex > 99 ) lutIndex = 99;
}
}
else { // Reset and wait for next cycle..
lutIndex = 0;
digitalWrite(CycleOnLed, LOW);
analogWrite(DACpin, 0);
}
//+++++++++++++++++++++++++++++++++
checkSerialCommand(); // Check if Serial session is required
//+++++++++++++++++++++++++++++++++
if (sendLutViaSerial) { // SEND the LUT via Serial to the LabVIEW application
lutIndex = 0;
do {
Serial.print(voltLUT[lutIndex]);
Serial.print(',');
lutIndex++;
} while (lutIndex < 100);
Serial.println('E'); // Mark the EOF
}
//+++++++++++++++++++++++++++++++++
if (getLutViaSerial) { // GET the LUT via Serialfrom the LabVIEW application
while (Serial.available() > 0) // Is there any data in Serial buffer ??
{
incomingByte = Serial.read(); // Read the incoming byte .. this removes it from Serial buffer
if (incomingByte == '<') // Wait for the start marker..
{
started = true; // Got the start marker. Set receive boolean to true..
charIndex = 0;
LUTFromPC[charIndex] = '\0'; // Throw away any incomplete characters
}
else if (incomingByte == '>') // Wait for the end marker
{
ended = true; // Got the end marker ...
break; // Stop reading - exit from while loop!
}
else // Read the message from the Serial buffer !
{
if (charIndex < charCount) // Make sure there is room
{
LUTFromPC[charIndex] = incomingByte; // Add char to array
charIndex++;
LUTFromPC[charIndex] = '\0'; // Add NULL to end.. keep on adding
}
}
}
if (started && ended) // All data read from the Serial buffer... process it.
{
parseData(); // Read and store the Voltage info in variables.
started = false;
ended = false;
}
}
} //loop
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void checkSerialCommand() {
if (Serial.available() > 0) {
startChar = Serial.read();
if (startChar == 's') sendLutViaSerial = true;
else if ( startChar == 'g' ) getLutViaSerial = true;
else {
sendLutViaSerial = false;
getLutViaSerial = false;
}
}
}
//==========================
void parseData()
{
char * strtokIndx; // This is used by strtok() as an index
strtokIndx = strtok(LUTFromPC, ","); // Get the first entry. Comma replaced by NULL after this..
userArray[0] = atoi(strtokIndx);
for (int count = 1; count < 100; count++ ) {
strtokIndx = strtok(NULL, ",");
userArray[count] = atoi(strtokIndx);
}
writeEEPROM(); // Write the look up table to the EEPROM
readEEPROM(); // Update the LUT with latest pattern.
Serial.print( "Got ");
Serial.print(charIndex);
Serial.println(" char fom PC"); // Just to know if all went well !!
charIndex = 0;
}
//==========================
void writeEEPROM() {
eeAddress = 0;
EEPROM.put(eeAddress, userArray);
}
//==========================
void readEEPROM() {
eeAddress = 0;
EEPROM.get(eeAddress, voltLUT);
}
The code for sending the waveform array has been done in LabVIEW and that has been tested on other serial devices for sending somewhat large amounts of data ( > 600 bytes )