I have two Arduinos. The first Arduino provides a PWM signal to a MOSFET, incrementing the OCR1A register value step by step. I achieve this by continuously pressing a button connected to digital pin 2 within an if loop. The MOSFET adjusts the current level in a circuit accordingly. The second Arduino reads the current changes, voltage, and power using a wattmeter. I want to receive a reading on the second Arduino every time the first Arduino increments the OCR1A value. Only a button is connected to digital pin 2 on the first Arduino, and the digital pins 4 on both Arduinos are physically connected. What might be wrong with the code?
I don't understand how the interrupt function works completely. I received programming assistance from ChatGPT, but it keeps insisting that I can use pin 2, which is connected to a button, as an interrupt pin. This doesn't make sense to me. Pin 2 is internally built with a pull-up and constantly receives a low signal. How can I activate an interrupt triggered by a rising edge? I don't understand all of this. Please help.
#define Up 2 // Pin 2 teki artırma butonu
int stateUp; // Yukarı butonunun bilgisini tutacak değişken
const int stepPin = 4; //değeri bir daha değiştirilemez türde integer bir değişken
volatile bool stepTriggered = false; //Kesme işlevlerinde kullanılan değişkenler "volatile" olarak işaretlenir, çünkü bu değişkenlerin değerleri donanım tarafından değiştirilebilir ve bu değişiklikler hemen işlenmelidir./
void setup()
{
pinMode(Up, INPUT_PULLUP); // Up butonuna dahili pull-up uygulaması
pinMode(stepPin, OUTPUT);
digitalWrite(stepPin, LOW);
attachInterrupt(digitalPinToInterrupt(2), triggerStep, RISING);
DDRB |= (1 << DDB1) | (1 << DDB2); // Set ports
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11); // Fast PWM mode
TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS10); // Fast PWM mode, no clock prescaling possible
OCR1A = 0; // Start PWM just below MOSFET turn on
ICR1 = 8191; // Set the number of PWM steps
Serial.begin(115200); // Start communication with serial monitor
}
void loop()
{ stateUp=digitalRead(Up);
if(stateUp==LOW){
OCR1A++;
delay(20);
Serial.println(OCR1A);}
if (stepTriggered)
{
stepTriggered = false;
digitalWrite(stepPin, HIGH);
delay(1);
digitalWrite(stepPin, LOW);
}
}
void triggerStep() {
// Adım sinyali kesme işlevi
stepTriggered = true;
}
#include <Wire.h>
#include "DFRobot_INA219.h"
DFRobot_INA219_IIC ina219(&Wire, INA219_I2C_ADDRESS4);
// Revise the following two paramters according to actual reading of the INA219 and the multimeter
// for linearly calibration
float ina219Reading_mA = 1000;
float extMeterReading_mA = 1000;
void setup(void)
{
Serial.begin(115200);
while(!Serial);
Serial.println();
while(ina219.begin() != true) {
Serial.println("INA219 begin faild");
delay(2000);}
//Linear calibration
ina219.linearCalibrate(/*The measured current before calibration*/ina219Reading_mA, /*The current measured by other current testers*/extMeterReading_mA);
Serial.println();
Serial.println("CLEARSHEET");
Serial.println("LABEL,Date,Time,Bus Voltage(V), Shunt Voltage(mV), Current(mA), Power(mW)");
}
void loop(void)
{if (digitalRead(4)==HIGH)
{Serial.print("DATA,DATE,TIME,");
Serial.print(ina219.getBusVoltage_V(), 2);
Serial.print(",");
Serial.print(ina219.getShuntVoltage_mV(), 3);
Serial.print(",");
Serial.print(ina219.getCurrent_mA(), 1);
Serial.print(",") ;
Serial.print(ina219.getPower_mW(), 1);
Serial.print(",");
Serial.println("");
delay(100);
}}