PikesPeakPJRC
Member
I have a Teensy 2.0 mounted on a Perma Proto board with 10 voltage dividers. Those are fed by 12 volt signals into 10 terminals mounted on top of the box containing the Teensy and other components (see images). 12 volts comes into the box, and is converted to 5 volts for the Teensy and a dual relay board.
The Teensy just waits for instructions from the PC, and if it receives an ASCII "1", it activates relay #1, an ASCII "0" deactivates (opens) relay #1, and the only other command is an ASCII "9", which starts the reading process. Relay 2 is activated, it waits 1 second for the sensors connected to settle, and the 10 GPIO inputs are read and accumulated, and the result fed out the serial port to the PC. Very simple, yet it hangs fairly often. If the Teensy loader program is running, I can press the reset button on the Teensy, and it reloads and reboots. Otherwise, I have to kill the power from both the USB port and the 5V regulator.
This is for a production line, and simply cannot hang like this. I've used Teensy's for a long time and cannot figure out why it hangs. If anyone has any ideas what I'm doing to cause this, or if there is a better way to get data to and from the Teensy 2.0 and the Windows 10 PC, please let me know.
Thanks in advance for your help!
Here's the code:
The Teensy just waits for instructions from the PC, and if it receives an ASCII "1", it activates relay #1, an ASCII "0" deactivates (opens) relay #1, and the only other command is an ASCII "9", which starts the reading process. Relay 2 is activated, it waits 1 second for the sensors connected to settle, and the 10 GPIO inputs are read and accumulated, and the result fed out the serial port to the PC. Very simple, yet it hangs fairly often. If the Teensy loader program is running, I can press the reset button on the Teensy, and it reloads and reboots. Otherwise, I have to kill the power from both the USB port and the 5V regulator.
This is for a production line, and simply cannot hang like this. I've used Teensy's for a long time and cannot figure out why it hangs. If anyone has any ideas what I'm doing to cause this, or if there is a better way to get data to and from the Teensy 2.0 and the Windows 10 PC, please let me know.
Thanks in advance for your help!
Here's the code:
Code:
const int ledPin = 11; // 11->Teensy 2.0; 13->Teensy 3.0 +
const int relayPin1 = 15;
const int relayPin2 = 14;
int counter = 0;
int incomingByte = 0;
char foo[128];
int availCount=0;
// the setup() method runs once, when the sketch starts
void setup()
{
Serial.begin(9600); // USB is always 12 Mbit/sec
pinMode(ledPin, OUTPUT);
// The dual relay we're using is opto-isolated, which means the logic is reversed.
// Therefore, to ensure the power output is off, set the control pins HIGH immediately
// to ensure the relays are off.
pinMode(relayPin1, OUTPUT);
digitalWrite(relayPin1, HIGH);
pinMode(relayPin2, OUTPUT);
digitalWrite(relayPin2, HIGH);
/*
* Set pins as input
*/
pinMode(0, INPUT);
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
}
void loop()
{
// Check the input queue. This is how we know when to turn the relays on and off.
availCount = Serial.available();
if (availCount > 0) {
incomingByte = Serial.read();
availCount = 0;
// Relay board is opto-isolated, so the inputs are negative logic
if (incomingByte == 49) { // ASCII 1 - Relay 1 on
digitalWrite(relayPin1, LOW);
} else if (incomingByte == 48) { // ASCII 0 - Relay 1 off
digitalWrite(relayPin1, HIGH);
} else if (incomingByte == 57) { // ASCII 9 - Read all inputs and report
// Turn on the sensor relay
digitalWrite(relayPin2, LOW);
delay(1000); // Give the relay a chance to settle
// Read all the sensor inputs and return the data
int dState = 0;
int ii;
for (ii=0; ii<10; ii++) {
if (digitalRead(ii) == HIGH) {
dState += (1 << ii);
}
}
digitalWrite(relayPin2, HIGH);
sprintf(foo, "%d", dState);
Serial.println(foo);
}
}
digitalWrite(ledPin, HIGH); // set the LED on
delay(30); // Leave the LED on for 0.03 second
digitalWrite(ledPin, LOW); // set the LED off
delay(270);
}