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.