/* */
#include <LiquidCrystal_I2C.h>
#include "TimerOne.h"
LiquidCrystal_I2C lcd(0x27, 20, 4);
// flasher associated variables
int val = 1; // flasher
// potentiometer associated variables
int sensorPin = A0; // input analog pin for potentiometer
int sensorValue = 0; // potentiometer analog value
unsigned int voltageValue = 0; // potentiometer value scaled to 0-1000
int oldValue = 0;
// pwm associated variables and objects
const byte pwmPin = 10; // output pin for PWM
TimerOne Timer1; // instanciation de la classe, i.e. création d'un objet TimerOne
// serial input associated variables
int incomingByte = 0; // for incoming serial data
// pwm sampling associated variables
unsigned long periode = 0; // period in ms
unsigned long temps_on = 0; // time signal is on in ms
bool pinState = LOW; // pwm sampling pin state
bool lastPinState = LOW;
int pinCounter = 0; // counter to bypass first cycles
unsigned long tempsRising0 = 0; // recorded time for rising edge
unsigned long tempsRising1 = 0; // recorded time for rising edge
unsigned long tempsFalling = 0; // recordes time for falling edge
bool toto;
unsigned long II = 0;
bool edgePin(bool value) {
static bool previousValue;
// bool result = (value && !previousValue);
bool result = (value ^ previousValue);
previousValue = value;
return result;
}
void setup() {
// write to console
Serial.begin(115200);
Serial.println("Hello Arduino\n");
// write to ldc
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("periode");
lcd.setCursor(0,1);
lcd.print("duty");
// define pin modes
pinMode(7, OUTPUT); // alternate pwm output
pinMode(8, INPUT); // pwm sampling
// pwm initialization
Timer1.initialize(1000000); // 50000 μs = 20 Hz - modifié pour 1 seconde
Timer1.pwm(pwmPin, 511); // 0-1023 = 0-100% duty
}
void loop() {
// delay(20);
// read the potentiometer
sensorValue = analogRead(sensorPin);
// changer le duty cycle du PWM sur la pin 9
if (sensorValue < 102) {sensorValue = 102;}
if (sensorValue > 921) {sensorValue = 921;}
if (oldValue != sensorValue ) {
Timer1.pwm(pwmPin, sensorValue);
}
oldValue = sensorValue; // store previous value
/*
// alternate pwm output based on potentiometer voltage
voltageValue = map(sensorValue,0,1023,0,1000); // scaling
int tempsON = 1000*voltageValue/100;
int tempsOFF = 1000.0 - tempsON;
digitalWrite(7,HIGH);
delay(tempsON);
digitalWrite(7,LOW);
delay(tempsOFF);
*/
/*
// flasher
val = val * -1;
if (val == 1) {digitalWrite(7,HIGH);} else {digitalWrite(7,LOW);}
*/
/*
// reading from the computer console
if (Serial.available() > 0) {
// read the incoming byte
incomingByte = Serial.read();
// Serial.println(incomingByte);
Serial.write(incomingByte);
}
*/
// reading from the computer console
if (Serial.available() > 0) {
// read the incoming string
String teststr = Serial.readString();
// Serial.println(incomingByte);
Serial.print(teststr);
}
// pwm sampling
pinState = digitalRead(8); // pin state
if (pinState != lastPinState) { // change of state
if (pinState == HIGH) { // rising edge
tempsRising1 = tempsRising0; // store time of rising edge
tempsRising0 = millis();
if (pinCounter++ >= 3) {pinCounter = 3; // flag for stable pwn signal after 3 cycles
}
}
else if (pinState == LOW) { // falling edge
tempsFalling = millis(); // store time of falling edge
if (pinCounter == 3) {
periode = tempsRising0 - tempsRising1;
temps_on = tempsFalling - tempsRising0;
lcd.setCursor(10,0);
lcd.print(periode);
lcd.print(" ");
lcd.setCursor(5,1);
lcd.print(temps_on);
lcd.print(" "); // required to erase the characters at the right end
}
}
}
lastPinState = pinState;
// cannot call the edge function more than one time in a cycle
if (edgePin(pinState)) {
if (pinState) {
Serial.println("rising");
}
else {
Serial.println("falling");
}
}
}