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++.
tom321
June 8, 2023, 11:56pm
3
Thanks for suggestion
I am almost there , I have 1 impulse on beginning only but I need for every period.
////////////////
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);
}
}
Delta_G:
Why?
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
DaveX
June 9, 2023, 2:06am
8
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
tom321
June 9, 2023, 3:22am
10
JCA34F:
2 what?
value of sine wave on y-axis, which is +/- 5
JCA34F
June 9, 2023, 3:59am
13
I did, what am I supposed to see?
JCA34F
June 9, 2023, 4:16am
15
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?
tom321
June 9, 2023, 4:30am
16
no name just numbers or values which can be calibrated to volts for example.
JCA34F
June 9, 2023, 4:33am
17
I see. So a simple 5000 micro one shot would work, right?
tom321
June 9, 2023, 4:55am
18
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.
tom321
June 9, 2023, 5:23am
20
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);
}
}
jimLee
June 9, 2023, 5:57am
21
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'.