One Rotary encoder to set volt and amp

I am buzzy to make a Arduino variable power supply with an rotary encoder.
I have now that it controle via an MCP4725 the output from 0 to 5volt in 4096 steps.
Now I will when I push the rotary button that I can controle a second MCP4725 output from 0 to 5volt in 4096 steps.
The code work fine and the second MCP4725 is working but How to controle this??
the output of the second MCP4725 is < MCP_I.setVoltage(0.17); > see code
The output volt of the second MCP is 0.17volt
But how....

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <MCP4725.h>
#include <Adafruit_ST7735.h>
#include <Adafruit_BusIO_Register.h>
#include <RotaryEncoder.h>

#define MCPIn 	   A0
#define TFT_CS     10
#define TFT_RST    8 
#define TFT_DC     9
#define PIN_IN1    A2
#define PIN_IN2    A3
#define ROTARYMIN  0
#define ROTARYMAX  4096

MCP4725 MCP_V(0x60); 										
MCP4725 MCP_I(0x62); 										
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

int ROTARYSTEPS = 0;
															 
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
															
int lastPos = -1;
int intention = 0;
const int BT0 = 7;
int selector = 0;
boolean isPressed = false;

void setup(void)
{
	Serial.begin(9600);
	Wire.begin();
	MCP_V.begin();
	MCP_I.begin();
    MCP_V.setMaxVoltage(5.0);
	MCP_I.setMaxVoltage(5.0);

	tft.initR(INITR_BLACKTAB);
	tft.fillScreen(ST7735_BLACK);
	tft.setRotation(1);
	tft.setCursor(10, 10);
	tft.print("ARDUINO POWER SUPPLY");
	Serial.print("Ready...");

    encoder.setPosition(0 / ROTARYSTEPS);
    pinMode(BT0, INPUT_PULLUP);
}

void loop(void)
{
	int adcInput = 0;
	float voltageIn = 0;
	float voltIn = 0.0;
	{
		adcInput = analogRead(MCPIn); 
		voltageIn = (adcInput * 5.0) / 1024.0;
		tft.setTextColor(ST7735_RED, ST7735_BLACK);
		tft.setTextSize(2);
		tft.setCursor(0, 80);	
		tft.print(voltageIn, 2);
	}
    encoder.tick();
														
    int newPos = encoder.getPosition() * ROTARYSTEPS;

    if (newPos < ROTARYMIN) {
        encoder.setPosition(ROTARYMIN / ROTARYSTEPS);
        newPos = ROTARYMIN;
    }
    else if (newPos > ROTARYMAX) {
        encoder.setPosition(ROTARYMAX / ROTARYSTEPS);
        newPos = ROTARYMAX;
    }
    if (lastPos != newPos) {
        Serial.println(newPos);
        lastPos = newPos;
		voltIn = (newPos * 5.0) / 4096.0;
		tft.setTextColor(ST7735_GREEN, ST7735_BLACK);
		MCP_V.setVoltage(voltIn);
		MCP_I.setVoltage(0.17);
		tft.setTextSize(2);
		tft.setCursor(0, 60);
		tft.print(voltIn, 2);
    }

    if (digitalRead(BT0) == LOW && isPressed == false)	
    {
        isPressed = true; 					 			
        doSwitchStatement(); 				 			

        selector++; 									
        if (selector > 2) {
            selector = 0;
        }														
    }
    else if (digitalRead(BT0) == HIGH)
    {
        isPressed = false; 								
    }
}

void doSwitchStatement() {
    switch (selector) {
    case 0:
		Serial.println("0.01");
		
        ROTARYSTEPS = 8;
        break;
    case 1:
		Serial.println("0.10");
        ROTARYSTEPS = 82;
        break;
    case 2:
		Serial.println("1.00");
        ROTARYSTEPS = 819;
    }
}

Instead of thinking of it as one encoder that you set the voltage from, think of it as two global voltage variables, and that you are adjusting either one or the other.

When you switch modes, do an encoder.setPosition(…) to match that mode’s voltage

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