Reading a square wave period on Arduino Uno Rev3

Hello. I have a project in which I have to determine ratio. I have generated 2 square signals, for the second I added a potentiometre for changing his period. Those two I have conected to other 2 pins as input signals. This is the code for the 2 signals generated(and also for the ratio , calculated with the periods already known):

#include <LiquidCrystal.h>
float P=1.2;
float val=0; 
const byte LED1 = 6;
const byte LED2 = 7;
int LED3 = 13;
float sensorPin = A0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
// Assigning delays.
float LED1_interval = 1000;
float LED2_interval = val; 
 
// Declaring the variables holding the timer values for each LED.
float LED1_timer;
float LED2_timer; 
 
// Setting 3 digital pins as output pins and resetting timer

void setup ()
  {
  pinMode (LED1, OUTPUT);
  pinMode (LED2, OUTPUT);
  pinMode (LED3, OUTPUT);
  LED1_timer = millis ();
  LED2_timer = millis ();
  
  }  // end of setup
 
 
//LED1 loop that turns it ON if it is OFF and vice versa
void toggle_LED1 ()
  {
   if (digitalRead (LED1) == LOW)
      digitalWrite (LED1, HIGH);
   else
      digitalWrite (LED1, LOW);
 
 
  // remember when we toggled it
  LED1_timer = millis (); 
  }  // end of toggleLED_1
 
 
//LED2 loop
void toggle_LED2 ()
  {
   if (digitalRead (LED2) == LOW)
      digitalWrite (LED2, HIGH);
   else
      digitalWrite (LED2, LOW);
 
 
  // remember when we toggled it
  LED2_timer = millis (); 
  }  // end of toggle_LED2
  
 
void loop ()
  {
  val=float(float(analogRead(sensorPin))/1023*300+1000);
 
  // Handling the blink of LED1.
  if ( (millis () - LED1_timer) >= LED1_interval)
     toggle_LED1 ();
 
 
  // Handling the blink of LED2.
  if ( (millis () - LED2_timer) >= val)
   { toggle_LED2 ();
   }
  P=float(val/LED1_interval);
  if (P<1.15)
      {digitalWrite (LED3, HIGH);
      }
      else
      {digitalWrite (LED3, LOW);  
      } 
   print();
  
}
void print()
{
  lcd.begin(16, 2);
  lcd.print( P ,3 );
  
}

I found out of the function pulseIn() that reads the period, but when I add it to my code is either modifying my period of the signal that is calculates the period, or it doesn't return the period value. This is how the full code looks like :

#include <LiquidCrystal.h>
float P=1.2;
float val=0; 
const byte LED1 = 6;
const byte LED2 = 7;
const byte LED4 = 9;
const byte LED5 = 10;
int LED3 = 13;
float sensorPin = A0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
float duration;
 
// Assigning delays.
float LED1_interval = 1000;
float LED2_interval = val;

 
// Declaring the variables holding the timer values for each LED.
float LED1_timer;
float LED2_timer; 
 
 
// Setting 3 digital pins as output pins and resetting timer

void setup ()
  {
  pinMode (LED1, OUTPUT);
  pinMode (LED2, OUTPUT);
  pinMode (LED3, OUTPUT);
  pinMode (LED4, INPUT);
  pinMode (LED5, INPUT);
  LED1_timer = millis ();
  LED2_timer = millis ();
  
  }  // end of setup
 
 
//LED1 loop that turns it ON if it is OFF and vice versa
void toggle_LED1 ()
  {
   if (digitalRead (LED1) == LOW)
      digitalWrite (LED1, HIGH);
   else
      digitalWrite (LED1, LOW);
 
 
  // remember when we toggled it
  LED1_timer = millis (); 
  }  // end of toggleLED_1
 
 
//LED2 loop
void toggle_LED2 ()
  {
   if (digitalRead (LED2) == LOW)
      digitalWrite (LED2, HIGH);
   else
      digitalWrite (LED2, LOW);
   
 
  // remember when we toggled it
  LED2_timer = millis (); 
  }  // end of toggle_LED2
  
 
void loop ()
  {
    
  val=float(float(analogRead(sensorPin))/1023*300+1000);
 
  // Handling the blink of LED1.
  if ( (millis () - LED1_timer) >= LED1_interval)
     toggle_LED1 ();
 
 
  // Handling the blink of LED2.
  if ( (millis () - LED2_timer) >= val)
   { toggle_LED2 ();
   }
  duration = pulseIn(LED4, HIGH);
  P=float(val/LED1_interval);
  if (P<1.15)
      {digitalWrite (LED3, HIGH);
      }
      else
      {digitalWrite (LED3, LOW);  
      } 
   
   print();
  
}
void print()
{
  
  lcd.begin(16, 2);
  lcd.print(duration);
  
}

I have also tried to make a function just for period:

int N=0,M=0;
float count;
void period()
{ if(digitalRead (LED1) == LOW)
      N+=1;
    else 
      M+=1;
    if(N<2 && M<2)
       count++;
}

Do you have any ideea what I did wrong? Sorry for my english and I hope you understood what I wanted to say.

millis() doesn't return a float. Why are you storing the unsigned long value in a float?

I have generated 2 square signals, for the second I added a potentiometre for changing his period.

How? Where?

Those two I have conected to other 2 pins as input signals.

But, we are to guess which pins those are.

  val=float(float(analogRead(sensorPin))/1023*300+1000);

Why is it necessary to cast a float as a float? Quit doing integer arithmetic here, and no casts will be needed. 1023 != 1023.0.

PaulS:
millis() doesn't return a float. Why are you storing the unsigned long value in a float?

I have generated 2 square signals, for the second I added a potentiometre for changing his period.

How? Where?

Those two I have conected to other 2 pins as input signals.

But, we are to guess which pins those are.

  val=float(float(analogRead(sensorPin))/1023*300+1000);

Why is it necessary to cast a float as a float? Quit doing integer arithmetic here, and no casts will be needed. 1023 != 1023.0.

1,3:The second signal (LED2) is the one with the period changed. If you read the code you will see that he's period is the variable "val" that is read as a digital value from the first signal. When i rotate the potentiometer the valeu of the signal varies from 0 to 5V , when i read with the function digitalread it results a value from 0 to 1023. I needed for this interval to be 1000-1300 so that's why i made that change. I used float to get a result with 3 decimals(maybe I used too many floats...).
2:I connected to pins 9 and 10, so the input signals where I read them are LED4 and LED5(LED3 is a diode).

Do not embed your answers in quoted material. It makes it impossible to figure out your answers to the questions.

Better? Now you understand? Any more questions? Or suggestions...

I found out of the function pulseIn() that reads the period,

Not true. The pulseIn() function returns the pulse width, either the high or low portion of the signal as specified in calling the function. If the square wave has good duty cycle symmetry then the returned value represents one half of the period of the square wave. If the duty cycle is not symmetrical or unknown symmetry then you have to call pulse in twice once for the high pulse value and once for the low pulse value and add them together to obtain the period value for the signal.

Lefty

Pfff.... I know that.... I read what this function does... it is a 50% duty cycle (so the ratio between 2 halfs or two full signals is the same)... thanks anyway...

Vladut:
Pfff.... I know that.... I read what this function does... it is a 50% duty cycle (so the ratio between 2 halfs or two full signals is the same)... thanks anyway...

Sorry, I didn't know you knew that, I had only what you posted to go by. :wink:

I found out of the function pulseIn() that reads the period,

Lefty

Nvm, it was my bad in expression, the problem is where I have to put the line : duration = pulseIn(LED5, HIGH);
If I put it in void loop(), it makes the second signal(LED5, the one with the potentiometer) double of the first, and let's me changing it's period with only 2 options: as big as the first, or double of it's size.
If i move it on void setup(), it remains 0 forever. :frowning: