Reading softpots with Arduino works but not with Teensy

Status
Not open for further replies.

alexandros

Well-known member
I'm trying to read four soft pots like this one (https://grobotronics.com/softpot-membrane-potentiometer-200mm.html) with a Teensy 3.6 but I can't get it to work properly.
Here's the code:
Code:
#define RIBDELTA 3

const int ribons = 4;
int ribonPins[ribons] = { 2, 3, 6, 7 };
int ribVals[ribons] = { 0 };
int curVals[ribons] = { 0 };
bool extremeValSent[ribons] = { false };

const int floorVal = 5;
const int ceilVal = 1005;

void setup() {
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i < ribons; i++) {
    curVals[i] = analogRead(ribonPins[i]);
    curVals[i] = constrain(curVals[i], floorVal, ceilVal);
    curVals[i] = map(curVals[i], floorVal, ceilVal, 0, 1000);
    if (abs(curVals[i] - ribVals[i]) > RIBDELTA) {
      ribVals[i] = curVals[i];
      Serial.print("Ribon "); Serial.print(i);
      Serial.print(": "); Serial.println(ribVals[i]);
      if ((ribVals[i] == 0) || (ribVals[i] == 1000)) {
        extremeValSent[i] = true;
      }
      else {
        extremeValSent[i] = false;
      }
    }
    if ((ribVals[i] == 0) || (ribVals[i] == 1000)) {
      if (!extremeValSent[i]) {
        ribVals[i] = curVals[i];
        Serial.print("Ribon "); Serial.print(i);
        Serial.print(": "); Serial.println(ribVals[i]);
        extremeValSent[i] = true;
      }
    }
  }
}
When I open the serial monitor I get some readings and then nothing at all. When I use the same code and circuit with an Arduino UNO, it works properly, printing values to the serial monitor when I change my finger position on the soft pot (ribon). But with a Teensy 3.6 or LC it doesn't seem to work. Additionally, with I try to upload the code to any Teensy board, initially the upload process takes some time and when Teensyduino is launched and uploads the code, I get an upload error message in the Arduino IDE, but I can still open the serial monitor. Once I close the serial monitor (which hangs for a couple of minutes before it closes) and upload the code again, it uploads fine without any error messages in the Arduino IDE.

Here's a Fritzing image of the circuit with normal pots instead of soft pots, cause my Fritzing installation doesn't have a soft pot part.
soft_pot_circuit.jpg
Any ideas why this works with Arduino but not with Teensy?
 
What exactly do you mean by "soft pot". How is that different from a normal pot? Maybe a link to the product you're actually using.

On this part:

Additionally, with I try to upload the code to any Teensy board, initially the upload process takes some time and when Teensyduino is launched and uploads the code, I get an upload error message in the Arduino IDE, but I can still open the serial monitor.

This sounds like something is really messed up on your PC. Can you tell us if you're using Mac, Linux or Windows, and which version? And which version of Arduino and Teensyduino are you using? (to check, click Help > About or Arduino > About if using a Mac)
 
Paul - there is an item link at start of post.

From the code the only reason I see it wouldn't print is if the value gets STUCK not 0 or 1000 and not changing by RIBDELTA.

However the Teensy 3.6 loop() rate may be overwhelming the SerMon with updates - something an UNO may not be able to do. Put a delay(10); in the loop to limit the cycle rate.
 
From the code the only reason I see it wouldn't print is if the value gets STUCK not 0 or 1000 and not changing by RIBDELTA.

However the Teensy 3.6 loop() rate may be overwhelming the SerMon with updates - something an UNO may not be able to do. Put a delay(10); in the loop to limit the cycle rate.
Tried it but it didn't help. I get the exact same behaviour. Tried a 115200 baud rate as well and still get the same behaviour.

This sounds like something is really messed up on your PC. Can you tell us if you're using Mac, Linux or Windows, and which version? And which version of Arduino and Teensyduino are you using? (to check, click Help > About or Arduino > About if using a Mac)
I'm on Linux Ubuntu studio 18.04 with Arduino 1.8.5 and Teensyduino 1.42.
 
Did you install the udev rule file?

If you so something like add yourself to the "dialout" group or run Arduino as root (common advice found on Arduino sites), to get around the permission issues, you'll probably have problems with ModemManager interfering. The udev rule file really is needed to make things work properly on all Linux systems.

About the speed, Teensy ignores the baud rate and always communicates are 12 Mbit/sec (minus USB overhead). To run slower you need to add a delay.
 
Did you install the udev rule file?

If you so something like add yourself to the "dialout" group or run Arduino as root (common advice found on Arduino sites), to get around the permission issues, you'll probably have problems with ModemManager interfering. The udev rule file really is needed to make things work properly on all Linux systems.

About the speed, Teensy ignores the baud rate and always communicates are 12 Mbit/sec (minus USB overhead). To run slower you need to add a delay.
I have both installed the udev rules, as well as added myself to the dialout group, so none of these should be the problem.
Since I couldn't remember which Teensyduino I had installed, I installed the latest before my previous reply. The behaviour didn't change. Then I tried to upload the blink sketch. The first time the IDE and Teensyduino took some time and eventually uploaded the code with the Arduino IDE giving an error message (can't remember the message, I'm not getting it any more). Uploaded the blink sketch a second time everything worked fine without any error messages. After that, the Teensyduino seems to be uploading code without any problems, but the code of the OP still doesn't work. Code uploads fine, but closing the serial monitor usually causes it to stall for a couple of minutes.

Is it possible there's some problem in the circuit that causes this issue with the Teensy? In the OP I have uploaded a .jpg with a Fritzing image of the circuit, replacing the ribbons with normal pots. The resistors are 10k, as well as the pots. The pull-down resistors are there so that I get a 0 when I don't press a ribbon. Again, this circuit and code works fine with an Arduino UNO.
 
The baud rate shown in the SerMon doesn't actually change anything - the USB to the Teensy runs at full speed.

Maybe delay(10) was conservative? Delay(200) should be overkill enough to assure messages don't overwhelm the computer.

Something odd is missing - if these lines are put inside loop() what happens?:
Code:
static elapsedMillis ShowMe=0;
if ( ShowMe > 1000 ) {
  ShowMe = 0;
  Serial.print( "Ribbon 0:);
  Serial.println(curVals[ 0 ]);
}
Delay(200);

I like putting a wait in setup to make sure Serial is on line and showing it working before continuing:
Code:
void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 5000 );
  Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
// …
 
I like putting a wait in setup to make sure Serial is on line and showing it working before continuing:
Code:
void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 5000 );
  Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
// …
I did get the same tip quite some years ago for something else, and I had completely forgotten about it. I just added the following line after Serial.begin(115200);
Code:
while(!Serial) {};
And now the serial monitor seems to work, though I get values jumping. So now there might be something in the circuit which I'll check tomorrow.
Thanks for reminding me!

BTW, is this really necessary when printing to the serial line? If so, why is this, and is it somewhere documented?
 
If the very first character sent is to be seen - waiting for the 'indeterminant' connect time of the host is required. The 'Serial' output is setup to continue without blocking if there is no host connect.

I've not been able to see the behavior indicated on Windows - if the code starts before the PC connects data is just 'lost' - but then comes online and works as if nothing happened from that point.

Something unique with Linux or the code in use is exhibiting not normal behavior . . . perhaps the speed of .print from loop() is messing with PC's USB.

The while(!serial) is not ideal in many cases in that it will never leave that line if it runs on non-usb power or it can run without the USB connect. The code in post 7 gives 5 seconds max to wait, when it is normally connected in half a second.
 
Status
Not open for further replies.
Back
Top