[chronojump] Blinking controled by interruptions in the timmer



commit d79d5fd7593a389397818163c2c4ac9ccb65d774
Author: Xavier Padullés <testing chronojump org>
Date:   Mon Jun 21 21:38:16 2021 +0200

    Blinking controled by interruptions in the timmer

 arduino/lightChro/control/control.ino | 115 +++++++++++++++++++++-------------
 1 file changed, 73 insertions(+), 42 deletions(-)
---
diff --git a/arduino/lightChro/control/control.ino b/arduino/lightChro/control/control.ino
index 5255bb855..420d66e7e 100644
--- a/arduino/lightChro/control/control.ino
+++ b/arduino/lightChro/control/control.ino
@@ -3,6 +3,7 @@
 #include <nRF24L01.h>
 #include <RF24.h>
 #include <printf.h>
+#include <MsTimer2.h>
 
 String version = "LightChro-Controler-1.05";
 //
@@ -25,6 +26,8 @@ RF24 radio(A3, A4);
 #define LED_on digitalWrite(2,HIGH)
 #define LED_off digitalWrite(2,LOW)
 
+bool blinkingLED = false;
+int blinkPeriod = 75; //Time between two consecutives rising flank of the LED
 
 // binary commands: each bit represents RED, GREEN, BLUE, BUZZER, BLINK_RED, BLINK_GREEN, BLINK_BLUE, SENSOR
 // 1 means ON
@@ -52,7 +55,6 @@ struct sample_t
 {
   bool state;           //State of the sensor
   short int termNum;    //Terminal number. Configured with the switches.
-  //TODO: treat as a binary to activate each terminal separately
   unsigned long int time;
 };
 
@@ -71,11 +73,12 @@ void setup(void)
 
   Serial.begin(115200);
   //When something arrives to the serial, how long to wait for being sure that the whole text is received
-  //TODO: Try to minimize this parameter as it adds lag from instruction to sensor activation
+  //TODO: Try to minimize this parameter as it adds lag from instruction to sensor activation. 1 is too low.
+  //Maybe increasing the baud rate we could set it to 1
   Serial.setTimeout(2);
-  printf_begin();       //Needed by radio.printDetails(); 
+  printf_begin();       //Needed by radio.printDetails();
   pinMode(2, OUTPUT);   //The LED is in output mode
-  LED_off;  //turn off the LED
+  LED_on;  //turn off the LED
 
   radio.begin();
 
@@ -96,21 +99,21 @@ void setup(void)
 
 void loop(void)
 {
-    while (radio.available()) //Some terminal has sent a response
-    {
-      radio.read(  &sample, sample_size);
-
-        Serial.print(sample.termNum);
-        Serial.print("\t");
-        Serial.print(sample.time);
-        Serial.print("\t");
-        Serial.println(sample.state);
-        //TODO: Use the timer to blink the LED after a command is sent
-        LED_on;
-        delay(10);
-        LED_off;
-//      }
-    }
+  while (radio.available()) //Some terminal has sent a response
+  {
+    radio.read(  &sample, sample_size);
+
+    blinkStop();
+    Serial.print(sample.termNum);
+    Serial.print("\t");
+    Serial.print(sample.time);
+    Serial.print("\t");
+    Serial.println(sample.state);
+    LED_off;
+    delay(50);
+    LED_on;
+    //      }
+  }
 }
 
 
@@ -118,41 +121,45 @@ void serialEvent()
 {
 
   String inputString = Serial.readString();
-//  Serial.print("Instruction received from Serial: \"");
-//  Serial.print(inputString);
-//  Serial.println("\"");
+  //  Serial.print("Instruction received from Serial: \"");
+  //  Serial.print(inputString);
+  //  Serial.println("\"");
   int separatorPosition = inputString.lastIndexOf(":");
-  
+
   String commandString = inputString.substring(separatorPosition + 1, inputString.lastIndexOf(";"));
   instruction.command = commandString.toInt();
-//  Serial.print("Command: ");
-//  Serial.println(instruction.command);
-  
+  //  Serial.print("Command: ");
+  //  Serial.println(instruction.command);
+
   String termNumString = inputString.substring(0, separatorPosition);
-//  Serial.print("termNumString:\"");
-//  Serial.print(termNumString);
-//  Serial.println("\"");
+  //  Serial.print("termNumString:\"");
+  //  Serial.print(termNumString);
+  //  Serial.println("\"");
 
   if (termNumString == "all")  //The command is sent to all the terminals
   {
     activateAll(instruction.command);
   } else {  // if (termNumString != "all")   Command to a single terminal
-      instruction.termNum = termNumString.toInt();
-//      Serial.print("instruction.termNum:\"");
-//      Serial.print(instruction.termNum);
-//      Serial.println("\"");
-      sendInstruction(&instruction);
+    instruction.termNum = termNumString.toInt();
+    //      Serial.print("instruction.termNum:\"");
+    //      Serial.print(instruction.termNum);
+    //      Serial.println("\"");
+    sendInstruction(&instruction);
+  }
+  if (instruction.command & sensor) {
+    blinkStart(blinkPeriod);
+    blinkingLED = true;
   }
   inputString = "";
 }
 
 void sendInstruction(struct instruction_t *instruction)
 {
-//  Serial.print("Sending command \'");
-//  Serial.print(instruction->command);
-//  Serial.print("\' to terminal num ");
-//  Serial.println(instruction->termNum);
-//  Serial.println(baseChannel + instruction->termNum);
+  //  Serial.print("Sending command \'");
+  //  Serial.print(instruction->command);
+  //  Serial.print("\' to terminal num ");
+  //  Serial.println(instruction->termNum);
+  //  Serial.println(baseChannel + instruction->termNum);
   radio.setChannel(baseChannel + instruction->termNum); //Setting the channel correspondig to the terminal 
number
 
   radio.stopListening();    //To sent it is necessary to stop listening
@@ -160,7 +167,7 @@ void sendInstruction(struct instruction_t *instruction)
   bool en = radio.write( instruction, size_instruction );
   if (en)  //en is 1 if radio.write went OK
   {
-//    Serial.println("Ok");
+    //    Serial.println("Ok");
     radio.startListening();  //Going back to listening mode
   } else {
     Serial.println("Error");
@@ -176,8 +183,8 @@ void activateAll(byte command)
   radio.stopListening();
   for (int i = 0; i <= 31; i++) {
     radio.setChannel(baseChannel + i);
-//  Serial.print("getChannel = ");
-//  Serial.println(radio.getChannel());
+    //  Serial.print("getChannel = ");
+    //  Serial.println(radio.getChannel());
     instruction.termNum = i;
     instruction.command = command;
     sendInstruction(&instruction);
@@ -185,3 +192,27 @@ void activateAll(byte command)
   radio.startListening();
   radio.setChannel(125);
 }
+
+void blinkStart(int period)
+{
+  MsTimer2::set(period / 2, blinkLed);  //A change in the state of the LEDS must occur every period/2 
milliseconds
+  MsTimer2::start();
+  LED_on;
+}
+
+void blinkStop(void)
+{
+  MsTimer2::stop();
+}
+
+void blinkLed(void)
+{
+  digitalWrite(2, !digitalRead(2));
+}
+
+void blinkOnce(void)
+{
+  LED_off;
+  MsTimer2::set(50, blinkStop);
+  LED_on;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]