Teensy 4.1, Serial1, Pin 0, Setting PinMode Issues

AaronNY

New member
Ran into a weird problem today. For some reason, using this code, breaks pin 0 for RX using Arduino IDE (2.3.2) :

#define SENSOR_TIMEOUT 2000 // 2 seconds timeout
#define COOLDOWN_TIME 5000 // 5 seconds cooldown period
#define NUM_SENSORS 30
int sensorPins[NUM_SENSORS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}; // Pin numbers for sensors
int sensorState[NUM_SENSORS]; // Store sensor states
unsigned long lastTrigger[NUM_SENSORS]; // Track when sensors were last triggered

void setup() {
Serial.begin(9600); // Set baud rate for communication
while(!Serial);
Serial1.begin(9600);
while(!HWSERIAL);
for (int i = 0; i < NUM_SENSORS; i++) {
pinMode(sensorPins, INPUT_PULLUP); // Set sensor pins as input with pull-up resistors
lastTrigger = 0; // Initialize last trigger times }
Serial.println("Serial start");
}

void loop() {
// Check if any data is available on Serial1
if (Serial1.available()) {
// Read the incoming message
String receivedMessage = Serial1.readString();
// Print the received message on Serial (USB)
Serial.println("Message received: " + receivedMessage);
}
}

Once I comment this line: pinMode(sensorPins, INPUT_PULLUP); // Set sensor pins as input with pull-up resistors
everything seems to work. Otherwise, no data is received when that line is left in.

Anyone have an idea what I am missing? Or am I doing something wrong?

Reading this from the main Teensy 4.1 page:
Digital Input Pins
Digital pins may be used to receive signals. Teensy 4.1 pins default to INPUT most with a "keeper" resistor. Teensy 4.1 pins accept 0 to 3.3V signals. The pins are not 5V tolerant. Do not drive any digital pin higher than 3.3V.
Input Pullup / Pulldown / Keeper Resistors
All digital pins have optional pullup, pulldown, or keeper resistors. These are used to keep the pin at logic HIGH or logic LOW or the same logic level when it is not being actively driven by external circuity. Normally these resistors are used with pushbuttons & switches. The pinMode function with INPUT_PULLUP or INPUT_PULLDOWN must be used to configure these pins to input mode with the built-in resistor.

I am assuming they are all INPUTs anyway, and I don't really need the built-in resistors. I have sensors hooked up to all of these pins that are listed in the array and they seem to function without this line in there anyway. But I figured I would post and get some feedback.

Thanks!
Aaron
 
Also this:
Code:
for (int i = 0; i < NUM_SENSORS; i++) {
    pinMode(sensorPins, INPUT_PULLUP); // Set sensor pins as input with pull-up resistors

Should be perhaps more like: sensorPins [ i ]
Code:
for (int i = 0; i < NUM_SENSORS; i++) {
pinMode(sensorPins[i], INPUT_PULLUP); // Set sensor pins as input with pull-up resistors
 
Yeah, that would do it! Sometimes when you are so engulfed in the moment, you don't see the obvious. As I get older, seems like I look for the un-obvious and start to assume things.
I have to say, the Teensy is one of my favorite boards. I am using the code to adjust a servo on a train set. The servo library works like a charm on the Teensy! I do have an issue with a MySQL library that is driving me nuts on another project. I will post it at some point when I get back to it.

Thanks for all your guys help! I feel embarrassed.

Aaron
 
Last edited:
Should be perhaps more like: sensorPins[ i ]
It probably was, which is why the rest of the code and the message turned into italicized text.
@AaronNY: when you post code, please enclose it in code tags (which can be generated using the </> icon) to avoid this problem.

Pete
 
probably was,
Quite possibly - I had to edit the post three times to get the line before the CODE to show as it does.
1727416036300.png

Though it is odd the first < CODE > post has no italics and the second does ...

And intended to make note of posted code needs to use the " </> " code marker.
 
Gotcha, will do in the future. The "i" was in the original code. This actually perplexed me and I really felt like I was getting old.

Thanks for letting me know about this. It makes me feel a little better.

Aaron
 
Back
Top