Sender trigger signal from USB (in pySerial) to start program

Status
Not open for further replies.

mqlo

Member
Hi,

I want to send a trigger signal from my computer to the Teensy. I'm using pyserial on the computer. Somehow the Teensy code doesn't respect waiting for the trigger signal.

Code on computer:

Code:
import serial
ser = serial.Serial("/dev/tty.usbmodem3071981",9600)

ser.write(1);


Code on Teensy:
Code:
   if (Serial.available()) {
    incomingByte = Serial.read();  // will not be -1
  }
    // do something

The teensy just launches into what's at
Code:
// do something.

This should be an easy problem but something I can't sort it out... any suggestions?
 
in order to synchronize correctly
you have to start teensy first

in teensy setup I would try something like this
Code:
void setup()
{ 
   while(!Serial); // wait for serial connection
   while(!Seral.available() || (incomingByte=Serial.read())!=1); wait for trigger byte to arrive 

    // do something
}

this assumes PC sends single bytes in ser.write(1)
 
Those code fragments look correct.

This should be an easy problem but something I can't sort it out... any suggestions?

The problem is almost certainly in some part of the code you didn't show. Details matter. That's why we have the "Forum Rule" to post complete code. We can help you much more if you show us everything you're doing, rather than only tell us the parts you believe matter.
 
Those code fragments look correct.



The problem is almost certainly in some part of the code you didn't show. Details matter. That's why we have the "Forum Rule" to post complete code. We can help you much more if you show us everything you're doing, rather than only tell us the parts you believe matter.



Full code:

Code:
#include <Messenger.h>

// According to https://www.pjrc.com/teensy/td_pulse.html, the following pins share the same timer:
// FTM0 5, 6, 9, 10, 20, 21, 22, 23
// FTM1 3, 4
// FTM2 29, 30
// FTM3 2, 7, 8, 14, 35, 36, 37, 38
// TPM1 16, 17

// Here, we'll use FTM0 for centre and FTM3 for surround.
// Define the centre
int C01 = 5;
int C02 = 6;
int C03 = 9;
int C04 = 10;
int C05 = 20;
int C06 = 21;
int C07 = 22;

// Define the surround
int S01 = 2;
int S02 = 7;
int S03 = 8;
int S04 = 14;
int S05 = 35;
int S06 = 36;
int S07 = 37;

// Status LED
int X00 = 13;

int incomingByte;



void setup() {
  // Set up the serial port
  Serial.begin(9600); // USB is always 12 Mbit/sec

  
  // put your setup code here, to run once:
  analogWriteResolution(12);// analogWrite value 0 to 4095, or 4096 for high
  
  // Set all pins to output
  pinMode(C01, OUTPUT);
  pinMode(C02, OUTPUT);
  pinMode(C03, OUTPUT);
  pinMode(C04, OUTPUT);
  pinMode(C05, OUTPUT);
  pinMode(C06, OUTPUT);
  pinMode(C07, OUTPUT);
  pinMode(S01, OUTPUT);
  pinMode(S02, OUTPUT);
  pinMode(S03, OUTPUT);
  pinMode(S04, OUTPUT);
  pinMode(S05, OUTPUT);
  pinMode(S06, OUTPUT);
  pinMode(S07, OUTPUT);
}

void loop() {

   if (Serial.available()) {
    incomingByte = Serial.read();  // will not be -1
    // actually do something with incomingByte
  }
    Serial.println(incomingByte);

    
    Serial.println("Hello World...");
    while(1) {
    setIntensity(4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095);
    delay(200);
    setIntensity(0, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095);
    delay(200);
    }

  //}
}

void setIntensity(int X00_s, int C01_s, int C02_s, int C03_s, int C04_s, int C05_s, int C06_s, int C07_s, int S01_s, int S02_s, int S03_s, int S04_s, int S05_s, int S06_s, int S07_s) {
    analogWrite(X00, X00_s);
    analogWrite(C01, C01_s);
    analogWrite(C02, C02_s);
    analogWrite(C03, C03_s);
    analogWrite(C04, C04_s);
    analogWrite(C05, C05_s);
    analogWrite(C06, C06_s);
    analogWrite(C07, C07_s);
    analogWrite(S01, S01_s);
    analogWrite(S02, S02_s);
    analogWrite(S03, S03_s);
    analogWrite(S04, S04_s);
    analogWrite(S05, S05_s);
    analogWrite(S06, S06_s);
    analogWrite(S07, S07_s);
}

Pyserial code:

Code:
import serial
ser = serial.Serial("/dev/tty.usbmodem3071981",9600)

ser.write(1);

while True:
    print("Waiting for messages from arduino..");
    read_ser=ser.readline()
    print(read_ser)
 
Status
Not open for further replies.
Back
Top