Hey Paul,
I solved the problem mentioned, but I stumbled across another one. I now can power up my sensor with the MOSFET, that's nice. But when doing so, I'm not able to establish a serial connection with it, my program stops at "userial.begin". If I connect ground of the sensor directly to ground of my circuit, everything works fine, the program gets into Loop and I'm able to read data from it.
Do you have any clue, what could cause this?
Code:
#include <TimeLib.h>
#include <TimeAlarms.h>
// USB host settings
#include "USBHost_t36.h"
uint32_t format = USBHOST_SERIAL_8N1;
USBHost myusb;
USBSerial userial(myusb);
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
char buffer[150];
void setup() {
Serial.begin(115200);
Serial.println("I'm ready");
pinMode(13,OUTPUT);
digitalWrite(13,HIGH);
pinMode(5,OUTPUT);
digitalWrite(5,HIGH);
myusb.begin();
Serial.println("myusb.begin finished");
myusb.Task();
Serial.println("myusb.Task finished");
userial.begin(115200);
Serial.println("userial.begin finished");
}
void loop() {
if (userial.available() > 0){
while (userial.available()) {
recvWithEndMarker();
readSensor();
}
}
digitalClockDisplay();
Alarm.delay(1000);
}
// RTC clock functions
void setTimeFromRtc(){
setSyncProvider(getTeensy3Time);
}
time_t getTeensy3Time()
{
return Teensy3Clock.get();
}
void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
void readSensor() {
digitalWrite(13, HIGH);
delay(50);
digitalWrite(13, LOW);
delay(50);
digitalWrite(13, HIGH);
delay(50);
digitalWrite(13, LOW);
delay(50);
digitalWrite(13, HIGH);
delay(50);
digitalWrite(13, LOW);
if (newData == true) {
Serial.println(receivedChars);
newData = false;
}
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (userial.available() > 0 && newData == false) {
rc = userial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
Thank you,
Martin