I have three debounced push buttons, two of them increment/decrement a counter that gets displayed on the serial monitor and also toggles an LED every time one of them is pressed. The other push button resets my counter to 127. Additionally, every time one of the increment/decrement push buttons is pressed it uses SPI to increase the resistance of a potentiometer in a chip by 39 ohms. All of this is working fine.
Then I added I2C into my ISR functions. I want to increase/decrease the voltage on the Vout pin of the DAC chip by 19.5mV according to which push button is pressed. My program starts up fine, and is printing the counter on the serial monitor, but when I press any button my program seems to stop. I can tell because the serial monitor stops printing the counter and the LED's won't toggle anymore. What could be the issue here?
#include <SPI.h>
#include <Wire.h>
byte pot_address = 0x00; // Address for wiper 0
int CS = 4; // Chip select
int pot_value = 0x00; // Variable to send data to MCP4162-010 potentiometer
int dac_value = 0x00; // Variable to send data to MCP4725 I2C DAC
volatile int sw_count = 127; // Variable to keep track of the number of times the up and down push buttons have been pressed
#define green_led_toggle 31 // LED will change state any time the increment or decrement push button is pressed
#define yellow_led_indicator 33 // LED will turn on whenever the counter equals 127
#define red_led_indicator 35 // LED will indicate when the counter is either less than 0 or greater than 255
#define PB_UP 2 // Increment push button
#define PB_DOWN 3 // Decrement push button
#define PB_CLEAR 18 // Push button to reset sw_count to 127
void setup() {
pinMode(CS, OUTPUT);
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
SPI.begin();
Wire.begin();
Serial.begin(9600); // Establish serial communication at 9600 bps
pinMode(yellow_led_indicator, OUTPUT); pinMode(red_led_indicator, OUTPUT); pinMode(green_led_toggle, OUTPUT); // Declare output pins to turn on LED's
pinMode(PB_UP, INPUT); pinMode(PB_DOWN, INPUT); pinMode(PB_CLEAR, INPUT); // Declare input pins to read push buttons
// Declare interrupts for detecting button presses
attachInterrupt(digitalPinToInterrupt(18),sw_count_clr,FALLING); // Interrupt for clear button
attachInterrupt(digitalPinToInterrupt(2),sw_up,RISING); // Interrupt for increment button
attachInterrupt(digitalPinToInterrupt(3),sw_down,RISING); // Interrupt for decrement button
}
void loop() {
Serial.println(sw_count); // Print count to serial monitor
if(sw_count == 127){ // Turn on yellow LED if counter equals 127
digitalWrite(yellow_led_indicator, HIGH);
}
else{
digitalWrite(yellow_led_indicator, LOW);
}
if((sw_count < 0) || (sw_count > 255)){ // Turn on red LED if counter is less than zero or greater than 255
digitalWrite(red_led_indicator, HIGH);
}
else{
digitalWrite(red_led_indicator, LOW);
}
delay(1000);
}
// -------------------- Functions ------------------------- //
void sw_count_clr(){ // Interrupt service routine triggerd by PB_CLEAR push button
sw_count = 127;
digitalWrite(green_led_toggle, LOW);
}
void sw_up(){ // Interrupt service routine triggerd by PB_UP (incrementor) push button
sw_count = sw_count + 1;
digitalWrite(green_led_toggle, !digitalRead(green_led_toggle));
pot_value = pot_value + 0x01; // Increase resistance by 39 ohms ( (39 / 10,000) * 255 = 1 = 01 in HEX )
digitalWrite(CS, LOW);
SPI.transfer(pot_address); // Send to wiper 0
SPI.transfer(pot_value); // Send value to chip
digitalWrite(CS, HIGH);
/* Program only works when commented out
dac_value = dac_value + 0x10; // Increase voltage by 19.5 mV ( (19.5 mV / 5V) * 4095 = 16 = 10 in HEX )
Wire.beginTransmission(0x60); // Address for MCP4725
Wire.write(0x40); // Command to update the DAC
Wire.write(dac_value); // Send value to DAC
Wire.write(0x00);
Wire.endTransmission();
*/
}
void sw_down(){ // Interrupt service routine triggerd by PB_DOWN (decrementor) push button
sw_count = sw_count - 1;
digitalWrite(green_led_toggle, !digitalRead(green_led_toggle));
pot_value = pot_value - 0x01; // Increase resistance by 39 ohms ( (39 / 10,000) * 255 = 1 = 01 in HEX )
digitalWrite(CS, LOW);
SPI.transfer(pot_address); // Send to wiper 0
SPI.transfer(pot_value); // Send value to chip
digitalWrite(CS, HIGH);
/* Program only works when commented out
dac_value = dac_value - 0x10; // Increase voltage by 19.5 mV ( (19.5 mV / 5V) * 4095 = 16 = 10 in HEX )
Wire.beginTransmission(0x60); // Address for MCP4725
Wire.write(0x40); // Command to update the DAC
Wire.write(dac_value); // Send value to DAC
Wire.write(0x00);
Wire.endTransmission();
*/
}