Struggling with programming side

Status
Not open for further replies.

Rev_Smarm

New member
I'm trying to do a project involving a Teensy, and I'm really struggling with the programming side.

I've done the tutorials, but there seems to be a big gap between the tutorials and the projects. I've been trying to mash something together using the tutorial stuff and borrowing stuff from published projects, but i'm struggling to integrate it into a working whole. Also, i don't like it because i don't fully understand the code.

I don't want to go off and learn 'C' and 'C in embedded systems' just to fix what is a fairly minor part of the project.
Is there anything that is more advanced than the Arduino and PJRC tutorials? something that fills the gap between basic stuff and advanced stuff?

Ta
 
Sorry - here's what i' trying to do:

I have a rotary switch with 10 positions connected to a Raspberry Pi, and for each position, 1 to 9, I want to play a seperate web audio stream, and position 10 starts a random shuffle play list from a folder stored locally. The stream or playlist will play continually whilst the switch is at the place, and change once the switch moves to the new position with a delay of a few seconds.
Audio plays through the Pi and I'm doing other stuff with the Pi so don't have enough I/O ports on it to connect it directly to the roraty switch, so i'm using a Teensy 3.2 for the rotary switch part.
It's frustrating because it seems so simple, and it's something i could do in pyhton -

I have bits of the code eg.
I can define the positions on the rotary switch and print out the pin number when it moves to a different pin, but the pin number keeps printing out every 'n' seconds ( i can increase the delay, but not eliminate it, i've thought about setting the delay to -say -over an hour, but it seems........inelegant).
I can print out when the switch moves to a new position, but can't define which pin the new position is connected to.
.....and i haven't even attempted to try to call the external web-streams or the the playlist because i'm stuck on what i thought would be a simple task.

Don't get me wrong, at this stage, i'd love someone to say "here's the code you need", but honestly i'd prefer to be pointed towards some resources that would give me enough understanding to just do this bit.

Thanks
 
To stop the repeating print, you can use logic to print only when the position changes. Let's say the pins are 1-10. You can define "previous_pin" as 0, set "current_pin" to the pin number of the current position, and then print only when current_pin is different from previous_pin, something like this:

Code:
setup()
{  
  int previous_pin = 0;
  int current_pin;
}
loop()
{
  current_pin = (however you determine rotary position)
  if (current_pin != previous_pin) {
    Serial.print( "position = " );
    Serial.println( current_pin );
    // update previous_pin so print only occurs once after a change in position
    previous_pin = current_pin
  }
}

Since you're using Teensy, there is probably no way around having to learn a little bit of C/C++ and embedded programming to complete your task. To get help with your code, you can include it in a message (please use "CODE" tags as I did above), and you can get feedback and help to accomplish your objectives.
 
Hi Rev_Smarm,
just to clarify, is this a 'real' 10 position rotary switch? I.e does it have ten pins, one for each position and an eleventh pin for the contact wiper?
Or is is a standard rotary encoder switch with five pins that rotates endlessly? And thus can encode an arbitrary number of positions.

This matters because the two devices require entirely different approaches.
 
Correct, 10 contacts, plus the ground.
However, this is going on hold for the minute while i learn a bit of C++ and come back a 'wiser' man.
 
OK, now I understand why the pin count is important. Have you considered using a Raspberry Pico? It is really cheap, has the required pin count and is more than powerful enough. I suggest it because you can then use your Python skills since Micro Python and Circuit Python work well on the Pico. But then have you worked out how to communicate with the Raspberry Pi from the client MCU? I think this will the real challenge whether you use a Teensy or Pico.

On the other hand I think it is a really good idea to acquire new skills, such as C/C++ and the best way to do that is with a real project. If that is one of your goals(which would be admirable) then I suggest starting with the Teensy since it has a good upgrade path to really powerful MCUs.

Another approach would be to use an MCP23017, which is a GPIO 16 port expander, on your Raspberry Pi. It has an I2C interface and there are good libraries for it in the Raspberry Pi ecosystem. This would be by far the simplest solution to your problem. If your main goal is just to get a working system in the simplest way possible(and not to learn new skills in a new ecosystem) then this is the path I would suggest.
 
Another approach would be to use an MCP23017, which is a GPIO 16 port expander, on your Raspberry Pi. It has an I2C interface and there are good libraries for it in the Raspberry Pi ecosystem. This would be by far the simplest solution to your problem. If your main goal is just to get a working system in the simplest way possible(and not to learn new skills in a new ecosystem) then this is the path I would suggest.

This is good advice. No need to add another MCU to get more digital I/O.
 
Thanks for the info guys - i'm liking the look of the Pico - i hadn't seen that before.
The only reason i have the Teensy is that i bought a couple to pad out an order a few years back, thinking that they might be useful later, but didn't know too much about them then (or now).
They're not getting thrown out - i like a challenge.

Thanks,
 
Status
Not open for further replies.
Back
Top