I am writing a code for a laboratory equipment in which I am using a 'Robotdyne AC dimmer' module with Arduino UNO board. In this I intend to operate an electromagnet ON/OFF at defined power-level (say 50%) for predefined time duration (say 1000 seconds). Once the operation of electromagnet is over, I want to acknowledge the operation complete by playing a buzzer sound.
However, when I use the tone() function, I get the compilation error because both PWM and buzzer use the same timer.
- How can I implement the buzzer sound after the electromagnet operation is complete?
- When the PWM of AC dimmer module and tone functions are intended to operate sequentially and not simultaneously why there is a compiler issue?
- How can I detach the AC dimmer module from timer when operation is complete, so that buzzer can use it?
#include <RBDdimmer.h>
# define buzzer A0 // +ve of buzzer-pin to A0 and -ve to GND
#define dimmerPWM 4 // Robotdyn AC module PWM to D4. Zero-Crossing default connected to D2,No declaration for this needed.
dimmerLamp Electromagnet(dimmerPWM);
unsigned int TotalTime = 1000; // this time is in seconds
unsigned int OnInterval = 1000; // this time is in milliseconds
unsigned int OffInterval = 1000; // this time is in milliseconds
byte Power = 50 ; // Dimmer Power value for Electromagnet to Operate, can be set as 0-100%.
void setup() {
Electromagnet.begin(NORMAL_MODE, OFF); // Initialize the Electromagnet Dimmer Circuit
}
void loop() {
Electromagnet.setPower(Power);
unsigned int StartTime = millis() / 1000; // Time in seconds
unsigned int CurrentTime = StartTime; // Time in Seconds
unsigned int TimeElapsed = StartTime - CurrentTime;
while (TimeElapsed < TotalTime) {
Electromagnet.setState(ON);
delay (OnInterval);
Electromagnet.setState(OFF);
delay (OffInterval);
CurrentTime = millis() / 1000;
TimeElapsed = StartTime-CurrentTime;
}
tone(buzzer, 1000, 2000); //Code compiles when this function is commented.
}
Thanks
Robotdyn AC dimmer module link: (https://robotdyn.com/ac-light-dimmer-module-1-channel-3-3v-5v-logic-ac-50-60hz-220v-110v.html.