Arduino code optimisation

Thread Starter

Kalon

Joined Aug 30, 2022
1
Hi guys. I have some code here that runs a 4 channel servo tester. The display currently updates when the two encoders are adjusted, or when the channel is changed via one of the push buttons. I also want the display to update when the potentiometers are adjusted. Is anyone able to help?

Mod: placed your code in Code Tags, easier to read.

C-like:
#include <Servo.h>

#include <Bounce2.h>

#include <Encoder.h>

#include <SPI.h>

#include "Ucglib.h"

//attach all inputs and outputs

#define PIN_BUTTON_A 17

#define PIN_BUTTON_B 16

#define PIN_BUTTON_C 15

#define PIN_BUTTON_D 14

#define PIN_ENCODER_L_A 18

#define PIN_ENCODER_L_B 19

#define PIN_ENCODER_R_A 20

#define PIN_ENCODER_R_B 21

#define PIN_POT_A A3

#define PIN_POT_B A2

#define PIN_POT_C A1

#define PIN_POT_D A0

#define PIN_LED_A 4

#define PIN_LED_B 5

#define PIN_LED_C 6

#define PIN_LED_D 7

#define PIN_SERVO_A 11

#define PIN_SERVO_B 10

#define PIN_SERVO_C 9

#define PIN_SERVO_D 8


// Total pins used without LCD is 20


//#define channelPulseMin 500

//#define channelPulseMax 2500


#define channelPulseMin 0

#define channelPulseMax 180


Bounce buttonA = Bounce(PIN_BUTTON_A, 10);

Bounce buttonB = Bounce(PIN_BUTTON_B, 10);

Bounce buttonC = Bounce(PIN_BUTTON_C, 10);

Bounce buttonD = Bounce(PIN_BUTTON_D, 10);

Encoder encoderLeft(PIN_ENCODER_L_A, PIN_ENCODER_L_B);

Encoder encoderRight(PIN_ENCODER_R_A, PIN_ENCODER_R_B);

Servo servoA;

Servo servoB;

Servo servoC;

Servo servoD;



// Arduino Mega Hardware SPI Pins:    sclk=52, data=51

Ucglib_ILI9341_18x240x320_HWSPI ucg(/*cd=*/ 45, /*cs=*/ 49, /*reset=*/ 47);


//int curEncLeftPos = 0;

int lastEncLeftPos = -999;

//int curEncRightPos = 0;

int lastEncRightPos = -999;

int curChannelSelection = 0; //

int lastChannelSelection = -1;


int channelSetMin[] = {channelPulseMin, channelPulseMin, channelPulseMin, channelPulseMin};

int channelSetMax[] = {channelPulseMax, channelPulseMax, channelPulseMax, channelPulseMax};

int channelSetPos[] = {(channelPulseMin + channelPulseMax) / 2, (channelPulseMin + channelPulseMax) / 2, (channelPulseMin + channelPulseMax) / 2, (channelPulseMin + channelPulseMax) / 2};

int channelSetLastPos[] = {-1, -1, -1, -1};


boolean updateRequired = false;

unsigned long curTime = 0;

unsigned long lastTime = -1;

unsigned long updateInterval = 5000;


void setup(void){


  pinMode(PIN_BUTTON_A, INPUT_PULLUP);

  pinMode(PIN_BUTTON_B, INPUT_PULLUP);

  pinMode(PIN_BUTTON_C, INPUT_PULLUP);

  pinMode(PIN_BUTTON_D, INPUT_PULLUP);

  pinMode(PIN_LED_A, OUTPUT);

  pinMode(PIN_LED_B, OUTPUT);

  pinMode(PIN_LED_C, OUTPUT);

  pinMode(PIN_LED_D, OUTPUT);

  digitalWrite(PIN_LED_A, HIGH);



  //Serial.begin(9600);

  ucg.begin(UCG_FONT_MODE_TRANSPARENT);

  ucg.setFont(ucg_font_profont12_mf);

  ucg.setRotate270();

  ucg.clearScreen();

  ucg.setColor(255, 255, 100);

  ucg.setPrintPos(10, 16);

  ucg.print("Channel A");

  ucg.setPrintPos(90, 16);

  ucg.print("Channel B");

  ucg.setPrintPos(170, 16);

  ucg.print("Channel C");

  ucg.setPrintPos(250, 16);

  ucg.print("Channel D");

  ucg.drawBox(0, 20, 320, 2);

  delay(500);

  servoA.attach(PIN_SERVO_A);

  servoB.attach(PIN_SERVO_B);

  servoC.attach(PIN_SERVO_C);

  servoD.attach(PIN_SERVO_D);



}


void loop(void){


  getInput();



  if (buttonA.fell()) curChannelSelection = 0;//If button A is pressed then channel 0 is selected

  if (buttonB.fell()) curChannelSelection = 1;//If button B is pressed then channel 1 is selected

  if (buttonC.fell()) curChannelSelection = 2;//If button C is pressed then channel 2 is selected

  if (buttonD.fell()) curChannelSelection = 3;//If button D is pressed then channel 3 is selected



  if (curChannelSelection != lastChannelSelection){

 

    clearLEDs();

    if (curChannelSelection == 0) digitalWrite(PIN_LED_A, HIGH);

    if (curChannelSelection == 1) digitalWrite(PIN_LED_B, HIGH);

    if (curChannelSelection == 2) digitalWrite(PIN_LED_C, HIGH);

    if (curChannelSelection == 3) digitalWrite(PIN_LED_D, HIGH);

    updateRequired = true; // on changes

    lastTime = millis();

    lastChannelSelection = curChannelSelection;

 

  }


  curTime = millis();


  if (curTime - lastTime > updateInterval && updateRequired){ // limit screen updates to 20Hz so user input can be handled more responsively


    updateDisplay();

    updateRequired = false;

    lastTime = curTime;

    //setServoPositions();

     

  }



}


void setServoPositions(){



}


void clearLEDs(){



  digitalWrite(PIN_LED_A, LOW);

  digitalWrite(PIN_LED_B, LOW);

  digitalWrite(PIN_LED_C, LOW);

  digitalWrite(PIN_LED_D, LOW);



}


void updateDisplay(){


  ucg.setColor(0, 0, 0);

  ucg.drawBox(0, 26, 320, 100);



  ucg.setColor(255, 255, 255); // draw text values

  for (int ii = 0; ii < 4; ii++){

    ucg.setPrintPos(10 + (ii * 80), 50);

    ucg.print("Min: "); ucg.print(channelSetMin[ii]);

    ucg.setPrintPos(10 + (ii * 80), 80);

    ucg.print("Max: "); ucg.print(channelSetMax[ii]);

    ucg.setPrintPos(10 + (ii * 80), 110);

    ucg.print("Pos: "); ucg.print(channelSetPos[ii]);

  }




  for (int ii = 0; ii < 4; ii++){

    ucg.setColor(0, 25, 200);

    ucg.drawBox(10 + (ii * 80), 140, 48, 90); // default range

    ucg.setColor(200, 200, 25);

    ucg.drawBox(10 + (ii * 80), 140 + (channelSetMin[ii]>>1), 48, (channelSetMax[ii]>>1) - (channelSetMin[ii]>>1)); // user set range

    ucg.setColor(200, 0, 25);

    ucg.drawHLine(10 + (ii * 80), 140 + (channelSetPos[ii]>>1), 48); // servo position

  }



  // show pulse min, max, and set position

}


void getInput(){


  //Read encoder left


  long curEncLeftPos = encoderLeft.read();



  if (abs(curEncLeftPos - lastEncLeftPos) >= 4) { // there are roughly four presses detected from each rotation to the next detent

    if (curEncLeftPos - lastEncLeftPos > -1){ // positive increase

      channelSetMin[curChannelSelection] += 1;

      if (channelSetMin[curChannelSelection] >= channelSetPos[curChannelSelection]) channelSetMin[curChannelSelection] = channelSetPos[curChannelSelection];

    } else {

      channelSetMin[curChannelSelection] -= 1; // only allow setting the lower end of the max range to the current position

      if (channelSetMin[curChannelSelection] < channelPulseMin) channelSetMin[curChannelSelection] = channelPulseMin;

    }

    lastEncLeftPos = curEncLeftPos;

    updateRequired = true;

    lastTime = millis();

  }


  long curEncRightPos = encoderRight.read();



  if (abs(curEncRightPos - lastEncRightPos) >= 4) { // there are roughly four presses detected from each rotation to the next detent

    if (curEncRightPos - lastEncRightPos > -1){ // positive increase

      channelSetMax[curChannelSelection] += 1;

      if (channelSetMax[curChannelSelection] > channelPulseMax) channelSetMax[curChannelSelection] = channelPulseMax;

    } else {

      channelSetMax[curChannelSelection] -= 1;

      if (channelSetMax[curChannelSelection] <= channelSetPos[curChannelSelection]) channelSetMax[curChannelSelection] = channelSetPos[curChannelSelection];

    }

    lastEncRightPos = curEncRightPos;

    updateRequired = true;

    lastTime = millis();

  }



  //Read buttons

  buttonA.update();

  buttonB.update();

  buttonC.update();

  buttonD.update();



  //Read potentiometers


   int rawPotAVal = analogRead(PIN_POT_A);

   int rawPotBVal = analogRead(PIN_POT_B);

  int rawPotCVal = analogRead(PIN_POT_C);

  int rawPotDVal = analogRead(PIN_POT_D);




  channelSetPos[0] = map(rawPotAVal, 1023, 0, 0, 180); // always allow moving a non highlighted channel

  channelSetPos[1] = map(rawPotBVal, 1023, 0, 0, 180);

  channelSetPos[2] = map(rawPotCVal, 1023, 0, 0, 180);

  channelSetPos[3] = map(rawPotDVal, 1023, 0, 0, 180);


if (PIN_POT_A >-1)

updateRequired = true;



  if (PIN_POT_B >-1)

updateRequired = true;


if (PIN_POT_C >-1)

updateRequired = true;



if (PIN_POT_D >-1)

updateRequired = true;


  for (int ii = 0; ii < 4; ii++) channelSetPos[ii] = constrain(channelSetPos[ii], channelSetMin[ii], channelSetMax[ii]);

  servoA.write(channelSetPos[0]);

  servoB.write(channelSetPos[1]);

  servoC.write(channelSetPos[2]);

  servoD.write(channelSetPos[3]);





}
 
Last edited by a moderator:
Top