Go Down

Topic: Measuring LED pulses (Read 281 times) previous topic - next topic

HamsterG

Ok so i asked a similar question the other day, but admittedly I worded it very poorly.I want to measure the amount of time an led is on for.So imagine that i have 2 arduinos and one is simply turning an led on and off for random amounts of time, and i have a second arduino, i want to use this second arduino to measure the length of each pulse and then print this value to serial monitor

Any help would be greatly appreciated, Thanks in advance!

UKHeliBob

Save the value of millis() when the LED turns on.
Save the value of millis() when the LED turns off
The difference is the pulse length

Are the two Arduinos connected in any way ?
Do you actually need to use two Arduinos ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

HamsterG

#2
Dec 14, 2018, 03:06 pm Last Edit: Dec 14, 2018, 03:12 pm by HamsterG
Save the value of millis() when the LED turns on.
Save the value of millis() when the LED turns off
The difference is the pulse length

Are the two Arduinos connected in any way ?
Do you actually need to use two Arduinos ?
No I dont need to use 2, i just need to be able to measure the length of time an led is on for.
So how would i go about using millis() to do this then?

LandonW

When digitalwrite is high you'll need a variable to attach the current millis value to and same for when digitalwrite is low.  Then subtract those two values.

HamsterG

so here are a few things ive done but im strggling still:

x = millis();
s();
y = millis();
xy = x - y;
test();

void test() {
  if((xy > 4999) &&
    (xy >= 5255))
  {Serial.print(".");
  }else{
    Serial.print("-");}
   }

But it only ever prints "-", why is that?

LandonW

 X = millis
StartTime = X
EndTime = Y. (However, still X because X is a "time stamp variable" but the value has changed with millis.

So
EndTimeX - StartTimeX = duration

HamsterG

X = millis
StartTime = X
EndTime = Y. (However, still X because X is a "time stamp variable" but the value has changed with millis.

So
EndTimeX - StartTimeX = duration
Sorry i don't quite follow your code, how do you declare all these variables

LandonW

Unsigned Long TimeNow;
Unsigned Long StartTime;
Unsigned Long EndTimd;

TimeNow = millis();

//When LED gets turned on
StartTime = TimeNow;
//When LED gets turned off
EndTime = TimeNow;

EndTime - StartTime = duration.

Post the code that you have.

HamsterG

This is the part that matters:

long time;
long x;
long y;
long xy;
int ledPin = 13;
void setup() {
Serial.begin(9600);
time = millis();
pinMode(ledPin, OUTPUT);
}

void loop() { 
x = millis();
s();
y = millis();
xy = x - y;
test();

x = millis();
o();
y = millis();
xy = x - y;
test();

x = millis();
s();


}

LandonW

The line right after loop is part of the problem. You need time = Millis
You listed time in the globals but didn't use it.
Also use Unsigned Long. Long lets you use negative numbers and we don't need that to measure time.

UKHeliBob

What do those mysterious functions that you have not posted do ?

What is it that is turning the LED on and off ?
Why don't you use descriptive names for your variables ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

LandonW

I suspect that measuring how long an LED was on is not the actual project... I could be wrong though.
posting the true and full code using code tags would be extremely beneficial.

Wawa

  if(xy > 4999 && xy >= 5255) // why that

It says "if xy is more than 4999 and more than 5254"

The second test seems useless.
Leo..


Go Up