Hello,
I'm using the Teensy 3.6 with PC (windows) as communication over Serial at baud 9600
I'm having trouble with Serial.read, I'm trying to provide an input through Serial but at the same time using serial.print, I guess you can't do both things at the same time and this is why it doesn't go as expected (it does not get the input from serial). I have entered the code I use below.
I'm using the Teensy 3.6 with PC (windows) as communication over Serial at baud 9600
I'm having trouble with Serial.read, I'm trying to provide an input through Serial but at the same time using serial.print, I guess you can't do both things at the same time and this is why it doesn't go as expected (it does not get the input from serial). I have entered the code I use below.
Code:
char string[4] = {0, 0, 0, 0};
float Serial_input[4] = {0, 0, 0, 0};
int val = 0;
volatile int cycles;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.setTimeout(1);
ARM_DEMCR |= ARM_DEMCR_TRCENA;
ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
}
void loop() {
uint32_t startCycleCPU;
startCycleCPU = ARM_DWT_CYCCNT;
if (Serial.available() > 0) {
for (int i = 0; i < 4; i++) {
string[i] = Serial.read();
}
int temp100 = ((int)string[1] - 48) * 100;
int temp10 = ((int)string[2] - 48) * 10;
int temp1 = (int)string[3] - 48;
val = temp100 + temp10 + temp1;
switch (string[0]) {
case 'L':
Serial_input[0] = val;
Serial.println("im here - Left");
break;
case 'R':
Serial_input[1] = val;
Serial.println("im here - Right");
break;
case 'F':
Serial_input[2] = val;
Serial.println("im here - Front");
break;
case 'B':
Serial_input[3] = val;
Serial.println("im here - Back");
break;
cycles = (ARM_DWT_CYCCNT - startCycleCPU);
while (cycles < 360000) { // 360000 = 2 milli sec @ 180Mhz
cycles = (ARM_DWT_CYCCNT - startCycleCPU);
Serial.print(millis());
Serial.print(",");
Serial.print("somePrint");
Serial.print(",");
Serial.print("somePrint");
Serial.print(",");
Serial.print("somePrint");
}
}
}
}
Last edited: