Triggering 5 millisecond pulses part #1

changing this part of code I am trying to have red line = 5 millisecond pulses ,
other words ;
each time when sine is crossing 2 ( sine rising side ) 5 millisecond pulse is generated.

////////////////
if( VmsTotal>10)
{
   VmsTotal = 10;
}
//////////////////

int Vms =1;
void setup() {
  Serial.begin(115200);
}

void loop() {

  static long VmsTotal = 0;
  static int VmsCount = 0;

for (int i = 0; i < 360; i += 5) {
    float y = 5.0 * sin(i * M_PI / 180);

    Serial.print( " y = "); Serial.print(y);
    if (y > 2)
    {
      VmsTotal += Vms;
      VmsCount++;

////////////////
if( VmsTotal>10)
{
   VmsTotal = 10;
}
//////////////////
     // Serial.print( "  Vms = "); Serial.print(4);
      Serial.print( "   VmsCount = "); Serial.print( VmsCount);// p
      Serial.print( "   VmsTotal = "); Serial.print( VmsTotal);// why is zero   0 
      
    }
    else
    {
      VmsCount = 0;
      VmsTotal = 0;

     // Serial.print( "  Vms = "); Serial.print(1);
      Serial.print( "    VmsCount = "); Serial.print( VmsCount);//p
      Serial.print( "   VmsTotal = "); Serial.print( VmsTotal); //     zero = ok
    }
    Serial.println();

    delay(50);
  }
}

Hello tom321

Take some time study the BlinkWithoutDelay example of the IDE to design and code a timer() function as needed for your project.
When using a timer based on the milli() function, the delay() function must no longer be used.

Have a nice day and enjoy coding in C++.

Thanks for suggestion
I am almost there , I have 1 impulse on beginning only but I need for every period.
1 pulse



////////////////
int Millis;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;
const int ledPin = LED_BUILTIN;
////////////////////
int Vms =1;
void setup() {
  Serial.begin(115200);
}

void loop() {
  
  unsigned long currentMillis = millis();

  static long VmsTotal = 0;
  static int VmsCount = 0;

for (int i = 0; i < 360; i += 5) {
    float y = 5.0 * sin(i * M_PI / 180);

    Serial.print( " y = "); Serial.print(y);
    if (y > 2)
    {
      VmsTotal += Vms;
      VmsCount++;
   currentMillis ++;
////////////////
if( VmsTotal>10)
{
   VmsTotal = 10;
}

/////////////////
if (  currentMillis - previousMillis  > 0 && currentMillis - previousMillis < 6 )
    {
      Millis = 4.5;
    }
    else
    {
      Millis = 1;
    }
///////////////////
     // Serial.print( "  Vms = "); Serial.print(4);
      Serial.print( "   VmsCount = "); Serial.print( VmsCount);// p
      Serial.print( "   VmsTotal = "); Serial.print( VmsTotal);// why is zero   0 
      
    }
    else
    {
      VmsCount = 0;
      VmsTotal = 0;

     // Serial.print( "  Vms = "); Serial.print(1);
      Serial.print( "    VmsCount = "); Serial.print( VmsCount);//p
      Serial.print( "   VmsTotal = "); Serial.print( VmsTotal); //     zero = ok
    }
    Serial.print( "    Millis = "); Serial.print( Millis);//p
    Serial.println();

    delay(50);
  }
}

I am adding it until sum is 5 millisecond, then I need to reset it to 0 and here I am stuck

when sine wave is > 2 pulses start when sine is <2 pulses stop

If you change your prints to a format like VmsCount:value VmsTotal:value Millis:value per https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-serial-plotter the legends and hoverovers will be ore sane:

      Serial.print( "   VmsCount:"); Serial.print( VmsCount);// p
      Serial.print( "   VmsTotal:"); Serial.print( VmsTotal);// why is zero   0 
 ...
    Serial.print( "    Millis:"); Serial.print( Millis);//p

2 what?

value of sine wave on y-axis, which is +/- 5

+/- 5 WHAT?

look at post # 3

I did, what am I supposed to see?

the numbers on y

Well, I see the blue line ramping up to 10, then falling back to 0. But, what does the scale on the left, going from -10 to +30 represent? Volts, ADC counts, degrees?

no name just numbers or values which can be calibrated to volts for example.

I see. So a simple 5000 micro one shot would work, right?

no one shot but every time when sine wave is rising above 2

I have done a little code review.

With the best will in the world, I don't understand the program logic.

Describe in simple terms what the program is supposed to do and quite simply.

5 millisecond pulses when sine wave is rising above threshold level


void setup() {
  Serial.begin(115200);
}

void loop() {
  for (int i = 0; i < 360; i += 5) {
    float y = 5 * sin(i * M_PI / 180);


    Serial.print(" y = "); //
    Serial.print(y);
    Serial.print(" threshold = "); //
    Serial.print(2);               // threshold =2
    

    Serial.println(); //

    delay(20);
  }
}

Wait, are you calculating the SIN wave? Not reading it in a Analog pin?

-jim lee

Hello tom321

Keep it simple and stupid and take search engine of your choice and ask the WWW for '50hz trigger +arduino'.