1. Got a question or need help troubleshooting? Post to the troubleshooting forum or Search the forums!

Arduino related code questions (sketches).

Discussion in 'Off Topic' started by BrooklynBay, Jul 1, 2021.

  1. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    414
    Likes Received:
    49
    In post number 6, the code is set up to work with a potentiometer, and an encoder. I want to use 2 encoders so do I set up the code for the second encoder like this:
    pinMode(Encoder_C, INPUT); // quadrature encoder input C
    pinMode(Encoder_D, INPUT); // quadrature encoder input D
    then substitute all of the analog references with this code? The pins will have to be defined, so all of the pins which are defined for analog will need to be defined for digital.
     
  2. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,478
    Likes Received:
    7,303
    Yes, you setup the second encoder exactly the same way as the first.
    One word of caution... sometime deciphering (reading/reacting to) encoder data can use a fair amount of resources on the Arduino boards. So your response times may take a hit if you read more than one.
     
  3. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    414
    Likes Received:
    49
    I bought a cheap $5 16 X 2 line display from the Micro Center store yesterday. Is it possible to display step increments & clockwise - counterclockwise data on the same display from two encoders simultaneously (no scrolling)? An example would be:

    20 + CW
    35 + CW

    Both encoders are going through a different number of steps by the steering wheel, and the steering linkage. I know that the codes for data for each line are 0 or 1 with a clear data code between them from what I've read, but that is for data from a single encoder.

    By the way, I've also looked at various websites mentioning pull up resistor values for the limit switch. They seem to vary between 1K & 10K. Some didn't have any (maybe because of the internal 20K resistors, so it doesn't seem like the value is critical. The codes in those websites didn't mention pull up resistors or values either. I would assume that the higher Ohm value would be better since it applies less of a shunt across the voltage terminals, but too high resistance will not pull up correctly.
     
  4. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,478
    Likes Received:
    7,303
    If it is a multiline display I see no reason you can't display them that way.
    My caution was only that actually reading some encoders can take a fair amount of clock cycles so it may slow down the execution of your main loop. You will know if that is happening :) It will be obvious if it bogs down.

    Pull up/down resistors are usually a larger value just to minimize the parasitic current that would be drawn through them.
     
  5. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    414
    Likes Received:
    49
    The displays usually have a time delay of around 2 seconds written into the code so that the data stream to the display, and the command to the motor/servo could catch up, and be synchronized. So is it better to remove the time delay when using two encoders or increase the time delay so that the system could catch up to synchronize?
     
  6. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,478
    Likes Received:
    7,303
    I would expect that you want to keep thing synchronized. However the exact lag you will have between changing things and reacting to the changes will need to be measured.
     
  7. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    414
    Likes Received:
    49
    Is there any problem connecting two encoders, and one display in parallel with the wiring on the I2C data terminals? Do I just define everything with the same pins? If so, does each item get addressed as a separate unit? I understand that many sensors could be read off of these two wires without any conflicts.
     
  8. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,478
    Likes Received:
    7,303
    No problem with multiple I2C devices. Here is an article that discusses this: https://create.arduino.cc/projecthub/ronfrtek/arduino-connect-multiple-i2c-devices-7db8b8

    There is example code showing them connecting 4 different LCDs.

    Another example over here with 2 devices on the I2C.

    http://www.learningaboutelectronics...I2C-devices-to-an-arduino-microcontroller.php

    a bit more over here too:

    https://learn.adafruit.com/i2c-addresses

    Essentially yes, you can connect multiple devices (even multiples of the same type of device and address them directly and individually.
     
  9. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    414
    Likes Received:
    49
  10. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,478
    Likes Received:
    7,303
  11. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    414
    Likes Received:
    49
    Would I need a multiplexer such as a TCA9548A just for encoders, and a 16 X 2 LCD or could they be hard wired together?
     
  12. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,478
    Likes Received:
    7,303
    You need the multiplexer when two of the devices have a fixed address. If they are all individually addressable then you can use them on the same buss with no multiplexer
     
  13. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    414
    Likes Received:
    49
    I read that the 16 X 2 LCD could have a few different addresses. They could be hardwired with solder for a single address or set in code to automatically set an address.

    I saw something in the link that you sent about a temperature sensor having a single address but it was wired in parallel. The code showed sensor 1 & sensor 2 which is confusing since they shouldn't work together if they have the same address. I wasn't able to find anything about encoder addresses.
     
  14. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,478
    Likes Received:
    7,303
  15. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    414
    Likes Received:
    49
  16. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,478
    Likes Received:
    7,303
  17. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,478
    Likes Received:
    7,303
    Don't get caught down a rat hole though, if you don't need those as I2C -- don't get distracted by it :)
     
  18. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    414
    Likes Received:
    49
    I want to use the encoders which I already have. Should I assign 4 digital non I2C inputs or use an I2C multiplexer? I think that the 16 X 2 display should be on the I2C, and the two encoders should be on 4 digital inputs. The hard part is setting up each encoder since they require a clock pulse. This makes me think that 2 encoders need 2 digital inputs, and 2 digital outputs since a clock pulse enters then exits.
     
  19. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,478
    Likes Received:
    7,303
    Sounds like a plan.
     

Share This Page