Login | Register 
FEATURES
APPLICATIONS
DOWNLOADS
EDUCATION
BLOG
WIKI
FORUM
STORE

Python: Adding more than one byte out with Serial

Still a little green? Ask your beginner's questions here

Python: Adding more than one byte out with Serial

Postby delgadoaustin » Fri Jun 29, 2018 3:48 am

Hey Everyone,

I have a project that I'm working on to control 4 12v fans through an Arduino's PWM pins and 12v speed controllers. The hardware side of things are working fine and am now just testing with 4 leds. I've gotten ch1 pin 3 to work with a button and slider just fine with my current sketch and project file.

I believe I have the Arduino sketch right to receive multiple bytes and have the right python strings to extract the individual variables v1-v4 I just can't figure out how to tell the chopexec to output the other bytes 1-3. I was thinking it might have to be sent out in a string of serial. If so, I would have to figure out the decode on the Arduino side and to get the right "val" for all 4chs for python.

Any help would be greatly appreciated!

Arduino
Code: Select all
int thirdSensor = 0;    // digital sensor
int inByte0 = 0;
int inByte1 = 1;
int inByte2 = 2;
int inByte3 = 3;// incoming serial byte
  int switchPin = 12;
  int ledPin1 = 3;
  int ledPin2 = 5;
  int ledPin3 = 6;
  int ledPin4 = 9;


void setup() {
  // start serial port at 9600 bps:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(switchPin, INPUT_PULLUP);   // digital sensor is on digital pin 12
  //establishContact();  // send a byte to establish contact until receiver responds
}

void loop() {
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte0 = Serial.read();
      analogWrite(ledPin1, inByte0);
    inByte1 = Serial.read();
      analogWrite(ledPin2, inByte1);
    inByte2 = Serial.read();
      analogWrite(ledPin3, inByte2);
    inByte3 = Serial.read();
      analogWrite(ledPin4, inByte3);
   
  }

    thirdSensor = digitalRead(switchPin);

    Serial.println(thirdSensor);
    //Serial.println(inByte);

}





Python
Code: Select all
# me - this DAT
#
# channel - the Channel object which has changed
# sampleIndex - the index of the changed sample
# val - the numeric value of the changed sample
# prev - the previous sample value
#
# Make sure the corresponding toggle is enabled in the CHOP Execute DAT.

def offToOn(channel, sampleIndex, val, prev):
   return

def whileOn(channel, sampleIndex, val, prev):
   return

def onToOff(channel, sampleIndex, val, prev):
   return

def whileOff(channel, sampleIndex, val, prev):
   return

def valueChange(channel, sampleIndex, val, prev):
   #sends the value of the slider to the serial dats send method
   #op('serial1').send(val,  terminator='\r\n')
   if channel.index == 0:
      op('serial1').sendBytes(val)

   if channel.index == 1:
      op('serial1').sendBytes(val)

   if channel.index == 2:
      op('serial1').sendBytes(val)

   if channel.index == 3:
      op('serial1').sendBytes(val)

   return
Attachments
Serial.9 Fan Test.toe
(5.77 KiB) Downloaded 24 times
delgadoaustin
 
Posts: 1
Joined: Sat Jun 23, 2018 3:37 am

Re: Python: Adding more than one byte out with Serial

Postby rob » Mon Jul 09, 2018 1:17 pm

If you want to distinguish between the source of the values, you'll need to send more information from TouchDesigner.
Also consider a delimiter, so the Arduino knows when the stream begins and ends.

Check out this example which sends/parses entire complete lines of multiple values:

https://docs.derivative.ca/index.php?title=Arduino
User avatar
rob
Staff
 
Posts: 1526
Joined: Tue Nov 13, 2007 1:14 am


Return to Beginners

Who is online

Users browsing this forum: No registered users and 11 guests