Predefined relay state change with encoder

Hi, I'm a beginner.
Can you direct me to a similar example that I need.
I have 8 relays that have 256 states predefined, LCD2x16 display and Rotary encoder 5pin.
Moving the encoder left or right would change the presets at State 1 to State 256.....

Sample:

State 1 - rel1 (ON )
State 2 - Rel2 (ON)
State 3 - Rel 1(ON) and Rel 2 (ON)
State 4 - Rel 3 (ON)
State 5 - Rel 1(ON) and Rel 3 (ON)
etc....

Is there any similar example on the forum? I'm still a beginner and I'm having a hard time getting started with code. What version of arduino do you recommend for the 8 relays?

Hello antimatery

Welcome to the worldbest Arduino forum ever.

This is a nice project to get started with an Arduino UNO or similar.

Keep it simple and stupid firstly.
Run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.

Have a nice day and enjoy coding in C++.

1 Like

seems that you're starting from scratch

  • figure out (google) how to write an arduino program that compiles, loads and prints "hello world"
  • figure out how to connect a relay to an Arduino and turn on/off
  • figure out how to connect the LCD and display something
  • figure out how to connect the Encoder, read it and display value on serial monitor

then start combining things (e.g. display encoder value on lcd)

1 Like

Hello antimatery

consider

#define ProjectName "Predefined relay state change with encoder"
/* BLOCK COMMENT
  - https://forum.arduino.cc/t/predefined-relay-state-change-with-encoder/1114273
  https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
*/
// include libaries
#include <Encoder.h>
Encoder rotaryKnop(2, 3);
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
// make variables
constexpr uint16_t Condensators[] {10, 20, 40, 80, 160, 320, 651, 1280};
constexpr uint16_t Relays[] {5, 6, 7, 8, 9, 10, 11, 12};
constexpr uint8_t SelectButton {4};
uint16_t condensator{0};
uint8_t newRotary{0};
uint8_t positionNew {255};
// make structures
struct TIMER
{
  uint32_t intervalMillis;
  uint32_t previousMillis;
};
struct BUTTON
{
  const uint8_t Knop;
  uint8_t stateOld;
  TIMER debounce;
} knop {SelectButton, LOW, 20, 0};
// tools
void heartBeat(int LedPin, uint32_t currentMillis)
{
  static bool setUp = false;
  if (!setUp) pinMode (LedPin, OUTPUT), setUp = !setUp;
  digitalWrite(LedPin, (currentMillis / 500) % 2);
}
void setup()
{
  Serial.begin(115200);
  Serial.println(ProjectName);
  Serial.println(__FILE__);
  Serial.println(__DATE__);
  Serial.println(__TIME__);
  lcd.init(); // initialize the lcd
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Capacity      pF");
  lcd.setCursor(0, 1);
  lcd.print("Capacity      pF");
  pinMode(knop.Knop, INPUT_PULLUP);
  for (auto relay : Relays) pinMode(relay, OUTPUT);
}

void loop()
{
  uint32_t currentMillis = millis();
  heartBeat(LED_BUILTIN, currentMillis);

  if (currentMillis - knop.debounce.previousMillis >= knop.debounce.intervalMillis)
  {
    knop.debounce.previousMillis = currentMillis;
    uint8_t stateNew = digitalRead(knop.Knop) ? LOW : HIGH;
    if (knop.stateOld != stateNew)
    {
      knop.stateOld = stateNew;
      if (stateNew == HIGH)
      {
        lcd.setCursor(9, 0);
        makePrint2LCD (condensator);
        uint8_t selector = 1;
        for (auto relay : Relays)
        {
          digitalWrite(relay, newRotary & selector);
          selector = selector << 1;
        }
      }
    }
  }
  newRotary = rotaryKnop.read();
  if (newRotary != positionNew) {
    positionNew = newRotary;
    uint8_t selector = 1;
    condensator = 0;
    for (auto Condensator : Condensators)
    {
      condensator = condensator + (newRotary & selector ? Condensator : 0);
      selector = selector << 1;
    }
    lcd.setCursor(9, 1);
    makePrint2LCD (condensator);
  }
}
void makePrint2LCD (uint16_t value)
{
  if (value < 1000) lcd.print(" ");
  if (value < 100) lcd.print(" ");
  if (value < 10) lcd.print(" ");
  lcd.print(value);
}
//------------------------------------------------------------

Have a nice day and enjoy coding in C++.

1 Like

Woow,

@paulpaulson

Thank you very much. I have a great desire to learn as much as possible.
That's for the antenna tuner for ham radio. By combining relays I will get capacities 10,20,30,40,50,60,70,80 and so on up to 2560pF when all relays are on.

I have to start from the beginning, microcontroller programming is always necessary in building devices.Arduino seems like a great thing to me.

Thank you all.

Many thank for your reply.

73 es 55 :+1:

1 Like

I tested the simulator with @paulpaulson code. It happens that it does not choose from 0, 10, 20, 30, 40, 50... etc
It already selects in increments of 0, 40, 80, 120, 160....

I've been trying to correct the code to dial in steps of 10pf but failed and it's still 40pf. Everything else is fine.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.