Hello, I would like the voltage output by the DAC to change when I switch pin 1. I am currently simulating this change by switching between GND and 3 V on the plug-in board. This is my code (I use the Nano Every):
#include <Wire.h>
#include <MCP4726.h>
float vref = 4840;
MCP4726 dac;
volatile int x = 0;
int pin1 = A0;
int pin2 = A1;
int pin3 = A2;
void fall() {
dac.setVoltage(x);
x += 500;
if (x >= 4096) {
x = 0;
}
}
void setup() {
// For MCP4726 the address is 0x60 (A0 part address. Check MCP4726 part number suffix as address could be different)
dac.begin(MCP4726_DEFAULT_ADDR);
pinMode(12, OUTPUT),
digitalWrite(12, HIGH);
Serial.begin(9600);
pinMode(pin1,INPUT);
attachInterrupt(digitalPinToInterrupt(pin1), fall, CHANGE);
}
First of all: what is not working? Your code is not complete (no loop())!
To read a switch, an interrupt handler is usually the wrong way. For example, sending an I2C message inside an interrupt handler is a bad idea. Generally you shouldn't use any method depending on interrupts to work inside an interrupt handler.
So, if you need more help, post a wiring diagram of your setup, post your complete code and post a link to the library used (as it doesn't seem to be available in the library manager of the IDE)!
You can not use i2c communications inside an ISR. i2c requires interrupts, and they are disabled inside the isr. You will need to set a flag and respond to it in loop() or else don't use an interrupt to read the change of the input pin.
This Topic has been locked as it is the identical or too similar your other topic.
Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.
Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.
Repeated duplicate posting could result in a temporary or permanent ban from the forum.
Could you take a few moments to Learn How To Use The Forum
It will help you get the best out of the forum in the future.