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:
    410
    Likes Received:
    49
    After getting experience modifying code to install firmware for my Robo 3D R1 printer, I was looking at various applications for the Arduino platform. It's very versatile, and could replace a lot of discrete components with a tiny Uno or Leonardo board. I want to start with a simple servo project using a Leonardo board. A library sketch has a potentiometer controlling the movement of a servo which is used for steering. The servo mimics the direction of the potentiometer.

    I have a question about loading sketches. One sketch has the basic code to move the servo while another sketch has code to use an H-bridge with a dc brush motor. A separate encoder or potentiometer cold be used to track the movement of the dc motor. I wanted to have both working simultaneously (H-bridge with a dc motor, and a servo). Could I upload both sketches separately into the Arduino to achieve two functions or do I have to combine the code from both sketches into a new sketch, then upload it into the Arduino?

    My goal is to have a force (haptic) feedback system so that I could sense the position of the dc motor while turning a steering wheel. A servo's shaft is connected to a steering wheel, and the dc motor is connected to the steering system of a small go kart. The driver turns the steering wheel connected to a servo, and the dc motor turns the steering system. If the front wheels touch a curb or go into a pot hole, then the servo connected to the steering wheel will react accordingly, and move with resistance or move back slightly on its own to react to road conditions. It's basically a steer by wire system with a feel of the road to simulate the feel of a physical steering linkage.

    Similar servo systems tend to get out of alignment. The idea that I have is similar to the stop switches on the Robo printers. Instead of relying upon the preset steps to go from stop to stop (left to right & vise versa), stop switches, encoder feedback or potentiometer feedback tells the Arduino how many degrees the rotation is to either side so that it remains aligned. Basic non feedback systems just have set screws on a potentiometer preset so that it could only allow a limited amount of degrees of movement without over or under extending the servo.
     
  2. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    The latter. The Arduino only executes one sketch at a time so you need to combine/merge them into one.

    That is largely how the printers work -- they use end-stops (some have HOME and END stops) and all the positions are calculated. You could switch to using a rotary encoder that is attached to the moving shafts and then you know exactly how far each shaft has turned. You will essentially "know" the position of the object being controlled on each axis that moves (assuming you use encoders on all of them).
     
  3. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    RAMPS shields have inputs for end stop switches, but is there a way to add these switches to the inputs directly onto the Arduino (either in the digital or analog pins)? If so, I would assume that those pins would need to be defined in the sketch. I ordered a Leonardo so a lot of this is new for me since I've only worked with the Mega 2560 & RAMPS boards until now.
     
  4. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    The inputs on the RAMPS map (are routed on the RAMPS board traces) to digital (or analog) inputs on the Arduino and are then handled in code (firmware) however you want.

    So you could go directly to the I/O pins on the Arduino -- yes, defining them in the sketch is the best way.
    Most of the code is the same on all of the Arduino platforms -- you just have to sort out which pins you use for sensors.

    The Arduino mega uses 3.3v (CMOS) logic for the digital pins and IIRC the Leonardo uses 5v (TTL)
     
  5. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    Think of the RAMPS as a shield that just breaks out the pins from the Arduino for easier access (although it provides extra features like motor drivers as well).
     
  6. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    The Leonardo board came in the mail along with some other parts. I found a servo project online with an H-bridge controlling a dc motor. It uses a potentiometer, rotary encoder, servo, dc motor, and a limit switch. I saw a series of messages on the bottom of the page in red writing which look like error messages but I wasn't able to copy & paste them. I was able to copy the code, so I'll post it. It hangs saying that it's compiling but it never go past that step to upload.

    //Include the libraries we need
    #include <PinChangeInterrupt.h> //http://electronoobs.com/eng_arduino_PinChangeInterrupt.php
    #include <PinChangeInterruptBoards.h>
    #include <PinChangeInterruptPins.h>
    #include <PinChangeInterruptSettings.h>
    #include <TimerOne.h> // Timer one interrupts

    //////////////////////////////////////////////////////////////
    /////////////////////DEFINE PID constants/////////////////////
    //////////////////////////////////////////////////////////////
    int kp = 2, ki = 0.01, kd = 0.02;
    //////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////

    int input, output, setpoint;
    int iTerm = 0, lastInput = 0, dInput = 0, error = 0;
    int outMin = -255, outMax = 255;
    int sampleTime = 10; //This values is is milliseconds
    volatile long encoderPos = 0;

    //////////////////////////////////////////////////////////////
    ////////////////////Define the pins we use////////////////////
    //////////////////////////////////////////////////////////////
    #define Encoder_A 2 // Quadrature encoder A pin
    #define Encoder_B 8 // Quadrature encoder B pin
    #define Motor_CW 11 // PWM outputs to L298N H bridge motor driver module
    #define Motor_CCW 3
    #define led 13
    #define END_stop 4

    void setup(void)
    {
    pinMode(Encoder_A, INPUT); // quadrature encoder input A
    pinMode(Encoder_B, INPUT); // quadrature encoder input B
    pinMode(END_stop, INPUT); // Input from the end stop switch
    pinMode(led, OUTPUT);
    attachPCINT(digitalPinToPCINT(Encoder_A), encoder, FALLING); // We update encoder position each falling edge detected in the interruption
    TCCR2B = TCCR2B & 0b11111000 | 1; // set 31Kh PWM to prevent motor whine (timer 2)
    Timer1.initialize(sampleTime * 1000); // setup timer 1
    Timer1.attachInterrupt(Compute);
    Serial.begin(115200); //Just for debugging if you want to print values
    while(digitalRead(END_stop))
    {
    analogWrite(Motor_CW, 0); // Rotate the motor CCW
    analogWrite(Motor_CCW, 255);
    }
    analogWrite(Motor_CW, 0); // Stop the motor
    analogWrite(Motor_CCW, 0);
    }

    void Compute()
    {
    setpoint = map(analogRead(0),0,1024,1024,0) * 110; // setpoint position is made with a potentiometer but could be given by serial monitor or other...
    input = encoderPos; // we get the data from the encoder interruption
    error = setpoint - input;
    iTerm += ki * error * sampleTime;
    if (iTerm > outMax) iTerm = outMax; // prevent that the I term from PID gets too big
    else if (iTerm < outMin) iTerm = outMin;
    dInput = (input - lastInput) / sampleTime;
    output = kp * error + iTerm - kd * dInput; // The PID output is the sum of P I and D values
    if (output > outMax) output = outMax; // limit output to 0 and 255 for the analog write
    else if (output < outMin) output = outMin;
    lastInput = input; //Remember to save the last input value for the next loop
    pwmOut(output); //Change the analog write for the motor control
    }

    void pwmOut(int out) { // to H-Bridge board
    if (out > 0) {
    analogWrite(Motor_CW, out); // Rotate the motor CW
    analogWrite(Motor_CCW, 0);
    }
    else {
    analogWrite(Motor_CW, 0);
    analogWrite(Motor_CCW, abs(out)); // Rotate the motor CCW
    }
    }

    void encoder() { // pulse and direction, direct port reading to save cycles
    if (PINB & 0b00000001) encoderPos++; // if(digitalRead(encodPinB1)==HIGH) count ++; //We increase steps by 1
    else encoderPos--; // if (digitalRead(encodPinB1)==LOW) count --; //We decrease steps by 1
    }

    void loop(void)
    {
    digitalWrite(led, !digitalRead(led)); // blink led or do something else...
    delay(200);
    }
     
    #6 BrooklynBay, Jul 7, 2021
    Last edited: Jul 26, 2021
  7. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    Zip up the entire project (and INO file) and stage it somewhere and I can try to compile it.

    If I can replicate your environment I should be able to sort it.
     
  8. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    I figured it out but it's complicated. I had to use two different computers to correct this issue. The Arduino IDE works differently on both of them for some odd reason. On my Win XP machine, it was showing Java errors. My Win XP machine didn't give me the option to copy the errors but my Win 7 machine had the option. I ran the IDE on my Win 7 machine, and it didn't have Java errors. It said that the TimerOne.H library wasn't installed. The original page where I found this project never mentioned it or showed a link to download it. A Google search found it.

    The next issue was in the line which says TCCR2B = TCCR2B. It had to be edited to say TCCR0B = TCCR0B. The IDE in my Win 7 machine wouldn't allow me to edit it, but my Win XP machine allows it even though both machine are using the 1.8.13 IDE. After editing this, and adding the PinChangeInterrupt.H & TimerOne.H libraries, I was able to compile & upload it to the Arduino. Do you have any idea why the same IDE 1.8.13 would behave differently on two different operating systems? When I was repairing my Robo, I had issues editing with the IDE on my Win 7 machine. I was able to open the notepad program to edit it since I couldn't edit directly in the IDE like I could on the Win XP machine.
     
    mark tomlinson likes this.
  9. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    First -- good work chasing it down :)
    The Arduino IDE stopped supporting Windows XP a while back (closer to 1.0.x IIRC) there is some discussion around that here:

    https://forum.arduino.cc/t/version-of-arduino-ide-that-will-run-on-xp/363683/3

    The libraries and such that the Arduino IDE is built with (and don't forget the Arduino IDE is actually a compiled Java program) should work anywhere that java will work, but Oracle de-supported Windows XP a good while back as well. So having said all of that -- it is likely something that broke the java VM on XP. You could try upgrading it (java on XP) and see how far you can upgrade it and that MIGHT help, but likely not since the Arduino IDE is a compiled one, not java source.

    For the other issues -- open source has it's limitations. Many groups don't do a good job of packaging their code and many don't use some sort of installer either (which makes it easier to create and enforce the structure you want for everything). They also do a rather poor job of documenting the process needed to get it working. Forgetting to mention a 3rd party library is almost a standard mistake :)

    Not all are trash, heavens no! There are a fair number that do a great job of developing and deploying the code. Which is rather a good thing. No matter how cool your code/widget/whatever is if you can't get it running easily then ... nobody knows but you.
     
  10. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    Why does the IDE on Win XP allow editing while the IDE on Win 7 doesn't allow editing? Is there a patch that could be downloaded?

    I saw that the Leonardo board has a steady power light, and a flashing light above it. The flashing light is a steady light since that sketch was uploaded. I guess this shows that there's a sketch installed.
     
  11. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    If I had to guess I'd suspect the XP version is having permissions related issues with it being XP (the filesystem support is different and the new Windows 10 file system supports ACLs where the XP/MSDOS one did not). Wild guess mind you because while I do java development, I have not tried it on XP in many years :) As for patches? No. Arduino (the foundation) does not support XP with any recent releases so that is not something they would patch. Microsoft does not support XP any longer so they would not be interested in fixing a problem with java on XP and lastly Oracle doesn't support XP and would not fix the Java VM to work there...

    I use XP on our arcade cabinets (MAME) and that never gets any OS changes or updates which is fine by me (since it is working as-is). That is the one benefit to using an unsupported OS -- no changes to deal with.
     
  12. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    I did a quick Google search, and found the answer. I wish that I would have known this a half a year ago when I was fixing the Robo. You go to preferences in the IDE then uncheck the use external editor option. That's it. That was on my Win 7 machine.
     
  13. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    By the way, why is the IDE still set up like an old MS-DOS interface with a cursor, and code words if it uses Java? It could be more like a KDE interface like Windows with graphical icons to click for functions instead of using code words with slashes, commas, and similar characters?
     
  14. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    You would need to pose that question to the Arduino.cc folks :)
    You can always use visual studio* to edit and compile the Arduino code as well as vscode (their trimmed down one for more open stuff) :

    https://code.visualstudio.com/.


    *takes a bit more setup with Visual Studio since there are extensions that have to be installed and configured.
     
  15. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    Is there a different code when defining hall effect & optical encoders since both of them are digital? I know that potentiometers are analog so there is a different code. Is this 5 pin encoder optical or hall effect? https://www.microcenter.com/product/618904/inland-ks0013-keystudio-rotary-encoder-module. How many encoders & H-bridges could a Leonardo board handle?

    On the Reprap full graphics display, there are cheap knock offs, and the original. Is the 16x2 also like this (original & cheap copy)?
     
  16. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    The arduino code for an encoder is going to be pretty much the same regardless of how it is constructed. It send codes back that have to be read and interpreted. There are a number of different types, but they all work much the same way from the code side.

    https://lastminuteengineers.com/rotary-encoder-arduino-tutorial/

    The encoders and h-bridges are going to take a fair amount of CPU to handle and since many of the encoders use interrupts to service the encoder that will likely be your biggest limitation.

    Frankly, all we used any more are Mega or Due boards since the costs are so cheap now and those boards have so much larger capacity (both in terms of I/O and memory). The Due has a beefier and faster CPU, but only uses CMOS logic (3.3v not TTL 5v) which means that you need to design and handle the boards with more respect for static :)

    For the LCDs almost all of the 16x2 are the same these days and all sourced from Asia. Buy from a reputable source and you will be fine (but I have bought a number from the cheaper ones Like AliExpress and BangGood that all worked). Someone like Adafruit or Jameco or DigiKey will have better components at a bit more of a price.
     
  17. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    I saw several variations of 16x2 displays with either 5 or 7 pins. I found one with only 4 pins (+), (-), plus two pins for data. Where do the data pins attach, and how is the display set up in the sketch? This is the display: https://www.microcenter.com/product/623816/seeed-studio-grove-lcd-rgb-backlight. I saw some articles online showing code for LCD.h or display.h libraries to be referenced, but I'm not sure if the pins are defined or just connected to digital outputs.
     
  18. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    I saw this link: "How to use the 16x2 LCD with Arduino? - Latest open tech from seeed studio" https://www.seeedstudio.com/blog/20...6x2-lcd-with-arduino-grove-lcd-rgb-backlight/. It looks like it uses its own library for code. They also use a custom board, but looking at the description of the pins, it looks like it uses the same pins as an encoder. Those pins are encoder inputs but according to this it would be an output for display data. How could an input pin also be a data output?
     
  19. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    It is meant to be used with the Groove base system connectors.

    https://wiki.seeedstudio.com/Grove-LCD_RGB_Backlight/

    Can you use it with others? Yes, but they should have Arduino examples at the above link ... not sure if they have examples of it stand-alone. See if that helps and if not we can dig deeper.

    It should work since it is I2C (serial communications) and the Arduino Leonardo has an I2C connection (pins 2 and 3)
     
  20. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    I wanted to have the display show clockwise or counter clockwise steps or degrees of movement.
     

Share This Page