Hello there!
This is my first post… So if i’m doing something wrong, let it me know.
I digitalize my old Kenwood Receiver with an digital potenziometer.
Everthing works fine, till i try to add Buttons for Volume up/down.
My Problem ist, i cant figure out, how to increment or decrement 1.
This is my code. at the bottom you can see my buttons.
.YAML
esphome:
name: verstaerker
includes:
- CustomDigipotOutput.h
esp8266:
board: d1_mini
# Enable logging
logger:
# Enable Home Assistant API
api:
services:
- service: control_pot
variables:
level: float
then:
- output.set_level:
id: lautstaerke_digipoti
level: !lambda 'return level / 100.0;'
ota:
wifi:
ssid: ""
password: ""
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: ""
password: ""
captive_portal:
spi:
clk_pin: D5
mosi_pin: D7
miso_pin: D6
output:
- platform: custom
type: float
lambda: |-
auto custom_digipot_output = new CustomDigipotOutput();
App.register_component(custom_digipot_output);
return {custom_digipot_output};
outputs:
id: lautstaerke_digipoti
number:
- platform: template
name: "Lautstärke Anlage"
id: lautsarke
optimistic: true
min_value: 0
max_value: 100
initial_value: 5
step: 1
mode: slider
on_value:
then:
- output.set_level:
id: lautstaerke_digipoti
level: !lambda 'return x / 100.0;'
switch:
- platform: gpio
name: "Unmute Anlage"
pin: D1
- platform: gpio
name: "Power Anlage"
pin: D2
button:
- platform: template
name: Anlage lauter
id: lautstaerke_plus
on_press:
then:
- logger.log: Anlage lauter
- output.set_level:
id: lautstaerke_digipoti
level: !lambda 'return id(lautstaerke_digipoti) + 1;'
- platform: template
name: Anlage leiser
id: lautstaerke_minus
on_press:
then:
- logger.log: Anlage leiser
- output.set_level:
id: lautstaerke_digipoti
level: !lambda 'return id(lautstaerke_digipoti) - 1;'
And here ist my CustomDigipotOutput.h:
#include "esphome.h"
using namespace esphome;
#include <SPI.h>
const int shutdownPin = D1;
int CS= D8;
class CustomDigipotOutput : public Component, public FloatOutput {
public:
void setup() override {
// This will be called by App.setup()
pinMode(CS, OUTPUT);
pinMode (shutdownPin, OUTPUT);
digitalWrite(shutdownPin, HIGH);
SPI.begin();
}
//write to Pot
void digitalPotWrite(int value)
{
delay(50);
digitalWrite(CS, LOW);
delay(50);
SPI.transfer(B00000000);
SPI.transfer(value);
delay(50);
SPI.transfer(B00010000);
SPI.transfer(value);
delay(50);
digitalWrite(CS, HIGH);
}
void write_state(float state) override {
// state is the amount this output should be on, from 0.0 to 1.0
// we need to convert it to an integer first
int value = state * 255;
ESP_LOGD("CustomDigipotOutput", "write_state %d", value);
digitalPotWrite(value);
}
};
Some parts are in German, because I’m German
What am I doing wrong?
Can someone tell me?
EDIT:
this is my acutall error:
Compiling .pioenvs\verstaerker\src\main.cpp.o
verstaerker.yaml: In lambda function:
verstaerker.yaml:89:35: error: cannot convert 'esphome::output::FloatOutput*' to 'float' in return
verstaerker.yaml: In lambda function:
verstaerker.yaml:99:35: error: cannot convert 'esphome::output::FloatOutput*' to 'float' in return
*** [.pioenvs\verstaerker\src\main.cpp.o] Error 1
i tried it with .state at the end of my variable but then i get the message “no member named state”