I'm new to this, that is my code about sinusoidal lighting system. it doesn't work when i applied on the lamp, but works in serial plotter. it can make the sinusoidal wave.
#include <Arduino.h>
// Define the PWM pin
const int pwmPin = 4; // pin number
// Parameters for the sinusoidal function
const float amplitude = 255.0; // Max amplitude for 8-bit PWM
const float period = 1 * 3600.0; // Period of the sinusoidal wave in seconds (1 hour)
const float frequency = 1.0 / period; // Frequency in Hz
const float phase = -PI/2; // Phase shift in radians
// Time calculation
unsigned long startTime;
void setup() {
Serial.begin(9600);
// Initialize the PWM pin
ledcAttach(pwmPin, 5000, 8); // pin, frequency, resolution
pinMode(pwmPin, OUTPUT);
// Record the start time
startTime = millis();
}
void loop() {
// Calculate the elapsed time in seconds
unsigned long currentTime = millis();
float t = (currentTime - startTime) / 1000.0;
// Calculate the sinusoidal value
float y = amplitude/2 * (1 + sin(2 * PI * frequency * t + phase));
// Set the PWM value
ledcWrite(0, (int)y);
Serial.println((int)y);
// Small delay to allow smooth PWM changes
delay(100);
}
void setup(){
// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin, ledChannel);
}
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
What model Arduino are you using?
What is the lamp you are trying to control?
Have you written some simple ON/OFF code to prove you have control of the lamp?