if structure takes too much time

Hello everybody,

First, i am french, so my english is not very correct, please be tolerant with it.

I am making a small project witch consist to control some additional lights on a motorbike depending of the ambient light conditions and the lighting mode selected.

The problem is, it take too much time to switch from a mode to another (almost a second approx). To me it is annoying...

To make it i used an arduino Fio with an xbee series 1 for wireless programming. I also add a thermal sensor for security but the function is not achieved. I pick some programm parts from ladyada for the light and thermal sensors, the rest is homemade.

//Configuration du thermomètre TMP36
int sensorPin = 1;  //TMP36 branché sur l'entrée analogique 1
                        //Résolution 10 mV / degree centigrade avec
                        // un offset de 500 mV pour les temperatures negatives 

 //Configuration du capteur de luminosité
int photocellPin = 0;            // La cellule et la résistance de 5k1 sont branchées sur a0
int photocellReading;            // La lecture analogique
int LEDpin = 3;                  // Transistor branché sur la pin 3 (PWM pin)
int LEDbrightness;               // Variable du PWM

//Configuration des entrées sorties
int PharesCroisement = 4;            // Variable d'entrée du phare de croisement
int PharesRoute = 5;                 // Variable d'entrée du phare de route
int PharesAntiBrouillard = 6;        // Variable d'entrée du bouton du phare antibrouillard
int PharesDetresse = 7;              // Variable d'entrée des phares de detresse
int PharesAntiBrouillardArriere = 8; // Variable de sortie du phare antibrouillard arrière


void setup()
{
  Serial.begin(57600);                        //Démarrage de la connection série                   
pinMode(LEDpin, OUTPUT);                      //Déclaration du transistor en sortie
pinMode(PharesCroisement, INPUT);             //Déclaration de l'entrée
pinMode(PharesRoute, INPUT);                  //Déclaration de l'entrée
pinMode(PharesAntiBrouillard, INPUT);         //Déclaration de l'entrée
pinMode(PharesDetresse, INPUT);               //Déclaration de l'entrée
pinMode(PharesAntiBrouillardArriere, OUTPUT); //Déclaration de la sortie

}


void loop()                     
{
 //getting the voltage reading from the temperature sensor
 int reading = analogRead(sensorPin);  

 // converting that reading to voltage, for 3.3v arduino use 3.3
 float voltage = reading * 3.3;
 voltage /= 1024.0; 

 // print out the voltage
 Serial.print(voltage); Serial.println(" volts");

 // now print out the temperature
 float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                               //to degrees ((volatge - 500mV) times 100)
 Serial.print(temperatureC); Serial.println(" degrees C");


 //Lecture de la cellule photosensible
   photocellReading = analogRead(photocellPin);  

  Serial.print("Analog reading = ");
  Serial.println(LEDbrightness);     // la valeur de sortie PWM
  delay (200);
  
  
 //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
  LEDbrightness = map(photocellReading, 0, 1023, 0, 255);

  
 //Cas en plein phare
 if (digitalRead(PharesRoute) == HIGH)
 {
  analogWrite(LEDpin, 255);
 }

 //Cas antibrouillard
 else if (digitalRead(PharesAntiBrouillard) == HIGH)
 {
  analogWrite(LEDpin, 255);
  digitalWrite (PharesAntiBrouillardArriere, HIGH);
 }

 //Cas phares de détresse
 else if (digitalRead(PharesDetresse) == HIGH)
 {
  analogWrite(LEDpin, 255);
  delay (100);
  analogWrite(LEDpin, 25);
  delay (800);
 }

  //Cas en phare de croisement
 else
 {
  analogWrite(LEDpin, LEDbrightness);
 }
}

It would be really nice if someone can explain to me why it takes too much time to switch from an if statement to another :grin:

It's probably because of this:

 //Cas phares de détresse
 else if (digitalRead(PharesDetresse) == HIGH)
 {
  analogWrite(LEDpin, 255);
  delay (100);
  analogWrite(LEDpin, 25);
  delay (800);
 }

If the pin is HIGH, the statement delays 900ms which is "(almost a second approx)".

Pete

I forgot to say something: Even if i am not going in this statement, i have the problem

 else if (digitalRead(PharesDetresse) == HIGH)
 {
  analogWrite(LEDpin, 255);
  delay (100);
  analogWrite(LEDpin, 25);
  delay (800);
 }

also, i put smaller delay times (50 and 200) and it is the same...

Maybe you're printing too much, and the stalling due to buffering of characters is causing you to wait too much.

Tell us about your wiring. Show us a schematic.

I also add a thermal sensor for security but the function is not achieved.

How does knowing the temperature relate to security? What does "the function is not achieved" mean?

  delay (200);

Called on each and every pass through loop(). Unconditionally.

cheesepower: it take too much time to switch from a mode to another (almost a second approx). To me it is annoying...

I suggest you comment out all the Serial.prints, and all the delays. Does that solve the problem?

PaulS: How does knowing the temperature relate to security? What does "the function is not achieved" mean?

This system is designed to manage quite high power and it will be in a small box. I am a little bit anxious about the rising température. That's why i added a thermal sensor, it will shut down the system if the temperature goes too high. Just for information, it will manage 25 Watts.

I comment all my serial prints, the 200 millis delay and it's the same

To give you a better idea, if i turn this statement for a very short time:

 else if (digitalRead(PharesDetresse) == HIGH)
 {
  analogWrite(LEDpin, 255);
  delay (100);
  analogWrite(LEDpin, 25);
  delay (800);
 }

it blinks approx 10 times after the input LOW

Normally it should stop immediately after the input LOW...

Still waiting for the schematic...

Here it is

http://floran.morel.free.fr/Divers/plan.pdf

And these ? (Et ceux-ci?)

int PharesCroisement = 4;            // Variable d'entrée du phare de croisement
int PharesRoute = 5;                 // Variable d'entrée du phare de route
int PharesAntiBrouillard = 6;        // Variable d'entrée du bouton du phare antibrouillard
int PharesDetresse = 7;              // Variable d'entrée des phares de detresse
int PharesAntiBrouillardArriere = 8; // Variable de sortie du phare antibrouillard arrière

Crap! Forgot the pull down resistors!

Yes i'm a noob :D

Thanks for the help, i'll try it later

Forgot the pull down resistors!

Rather than do a heap of soldering, invert your logic, and use the built-in pullups.