Pages: [1]   Go Down
Author Topic: if structure takes too much time  (Read 383 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
//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  smiley-mr-green
Logged

Offline Offline
Edison Member
*
Karma: 43
Posts: 1556
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's probably because of this:
Code:
//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
Logged

Where are the Nick Gammons of yesteryear?

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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...
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25800
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: January 10, 2013, 04:37:09 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?

Code:
  delay (200);
Called on each and every pass through loop(). Unconditionally.

Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12562
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote from: 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:
Code:
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...
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25800
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Still waiting for the schematic...
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here it is

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25800
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And these ?  (Et ceux-ci?)
Code:
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
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Crap! Forgot the pull down resistors!

Yes i'm a noob  smiley-grin


Thanks for the help, i'll try it later
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25800
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Forgot the pull down resistors!
Rather than do a heap of soldering, invert your logic, and use the built-in pullups.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: