
Originally Posted by
MichaelMeissner
Note, Teensy's SoftwareSerial package will use Serial2 if rx == 9 and tx == 10. What speed are your running your Teensy at?
I see 9600 baud called out in the code. Here is my latest code btw:
You see any issues?
Code:
// Use the softwareserial library to create a new "soft" serial port
// for the display. This prevents display corruption when uploading code.
#include <SoftwareSerial.h>
// Initialize constants that refer to specific pins on the Teensy
const int ledPin = 13;
const int CVin = 14;
const int PWMout = 22;
// Initialize variables, names are arbitrary
int CVvalue = 0;
int dutyCycle = 0;
char tempstring[10];
// Attach the serial display's RX line to digital pin 10
SoftwareSerial mySerial(9,10); // pin 10 = TX, pin 9 = RX (unused)
// This setup() function runs once when power is applied to the Teensy or its reprogrammed
void setup(){
mySerial.begin(9600); // set up serial port for 9600 baud
delay(1000); // wait for display to boot up
// Initialize the serial COM port for debug
Serial.begin(38400); // Baud rate is not critical
// Initialize pins as either OUTPUT or INPUT
pinMode(ledPin, OUTPUT);
pinMode(PWMout, OUTPUT);
pinMode(CVin, INPUT);
// Initialize PWM module
analogWriteFrequency(PWMout, 2000); // PWM frequency is 2kHz
analogWriteResolution(10); // 10-bits of resolution = 0-1024
analogWrite(PWMout, dutyCycle); // program starts with PWM output at zero
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write(" "); // clear display
mySerial.write(" ");
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write("VC REV-VAR LOOP ");
mySerial.write(" RPMS +0000 ");
}
// The loop() runs forever.
void loop(){
CVvalue = analogRead(CVin); // sample the analog value
dutyCycle = CVvalue; // do math here to scale/limit/adjust the sample value
analogWrite(PWMout, dutyCycle); // update the PWM duty-cycle with the new value
Serial.print("CV is:"); // spit out the variables on the serial bus
Serial.print(CVin);
Serial.print(" DutyCycle is:");
Serial.println(dutyCycle);
digitalWrite(ledPin,!digitalRead(ledPin)); // toggle LED on the Teensy (heartbeat)
//...might look always on because it's toggling so fast
sprintf(tempstring,"%4d",dutyCycle); // create strings from the numbers
// right-justify to 4 spaces
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write("VC REV-VAR LOOP ");
mySerial.write(" RPMS ");
mySerial.write(254); // move cursor to second line 10th space
mySerial.write(201);
mySerial.write(tempstring);
}