How to code Pulse Duration for PWM

This is a stimulation code but it stimulates the muscle constantly i need a duration here but i just couldnt think a way to do it here im literally stuck i need help any help will be appreciated it should stimulate like maybe 10 seconds

#include <PWM.h>

int led_pin = 9;
int 32_t frequency = 150;

void setup() {

InitTimersSafe();
SetPinFrequencySafe(led_pin, frequency);

}

void loop(){

pwmWrite(led_pin, 127)

}

Explain more.

like this is for eye muscle stimulation it gives certain amount of electricity and in the loop part it gives current and the frequency is the currents frequency but the pulse of the current shouldnt be constant for example the eye muscle should be stimulated 10 seconds how do i limit the time of stimulation in this code?

right now my code is stimulating the muscles infinitely i need it to stop at 10 seconds so it can look like blink

Will you be wanting to re-trigger the occurrence of the output ?

it should only happen when the detection happens i am gonna share the whole code

#include <PWM.h>

int IRsensorPin = 0;
int LedPin = 13;
int latestLevel = 0;
int latestChange = 0;
int changeThreshold = 5;

int led_pin_pwm= 9;
int frequency = 150;
int pulse_duration = 100;

int duration = 100;
float startPoint = 0;

void setup() {

Serial.begin(9600);
pinMode(13,OUTPUT);

InitTimersSafe();
SetPinFrequencySafe(led_pin_pwm, frequency);

}

void loop() {

int IRsensorValue = analogRead(IRsensorPin);
Serial.println(IRsensorValue);

latestChange = IRsensorValue - latestLevel;
latestLevel = IRsensorValue;

if( latestChange >= changeThreshold){

digitalWrite( LedPin, HIGH );
startPoint = millis();

}
if( millis() >= startPoint + duration ){

digitalWrite( LedPin, LOW );

}

pwmWrite(led_pin_pwm, 127);

}

this the final product without pulse duration whenever it detects a blink it should stimulate the other eye so it is if / else statement rather than a loop

So you want the output on led_pin_pwm to be 10 seconds then the pin goes to GND (0V) .


BTW
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.

1 Like

Yes exactly

This is not proven, should give you some ideas:

#include <PWM.h>

int IRsensorPin           = 0;
int LedPin                = 13;
int latestLevel           = 0;
int latestChange          = 0;
int changeThreshold       = 5;

int led_pin_pwm           = 9;
int frequency             = 150;
int pulse_duration        = 100;

int hysteresis            = 5; //set to what's reasonable

bool blinkCheckingFlag    = true;
bool timerFlag            = false;

int duration              = 100;

unsigned long startPoint;


//*************************************************************
void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);

  InitTimersSafe();
  SetPinFrequencySafe(led_pin_pwm, frequency);

} //END of setup()


//*************************************************************
void loop()
{
  int IRsensorValue = analogRead(IRsensorPin);

  Serial.println(IRsensorValue);

  latestChange = IRsensorValue - latestLevel;
  latestLevel = IRsensorValue;

  //****************************
  //if checking is enabled, was there a blink ?
  if (blinkCheckingFlag == true && latestChange >= changeThreshold)
  {
    //disable blink checking
    blinkCheckingFlag = false;

    digitalWrite( LedPin, HIGH );

    //restart the 10 second TIMER
    startPoint = millis();

    //enable the TIMER
    timerFlag = true;

    //start the pulse output
    pwmWrite(led_pin_pwm, 127);
  }

  //****************************
  //if enabled should we stop the pulse output ?
  if (timerFlag == true && millis() - startPoint >= 10000)
  {
    digitalWrite( LedPin, LOW );

    //stop pulse output
    pwmWrite(led_pin_pwm, 0);

    //disable the TIMER
    timerFlag = false;
  }

  //****************************
  //
  if (timerFlag == false  && latestChange < changeThreshold - hysteresis)
  {
    //enable blink checking
    blinkCheckingFlag = true;
  }

} //END of loop()
1 Like

Thanks seems logical i will try it on my device then i will check if it is the solution