Go Down

Topic: How to make more the 1 function work at a time (Read 2406 times) previous topic - next topic

BulldogLowell

#30
May 07, 2016, 08:35 pm Last Edit: May 07, 2016, 08:36 pm by BulldogLowell
Just play with the second values in the constructor until you get the fade speed you like.  Smaller is faster.

Persistence of Vision affects your 'perceived' ramp up/ramp down time... on a linear fade like this one.




steve43

We cant find any other values that can be changed other than the 255 which is max brightness to 0 which is off which line is to be changed? Also i need a relay to come on when the flow switch is activated (same time as the led) can this be done easily

BulldogLowell

We cant find any other values that can be changed other than the 255 which is max brightness to 0 which is off which line is to be changed? Also i need a relay to come on when the flow switch is activated (same time as the led) can this be done easily
As I pointed out above, change the 10 in the constructors to a different number, that affects the speed.

you can easily add a relay to the code, insert the function to turn it on right under the function to set the led target value to 255

Code: [Select]
if(checkMotion())
  {
    ledSet[1].write(255);
    relayON();
    Serial.print(F("Set to max brite at "));
    Serial.println(millis());
    pirTimer = millis();
  }

steve43

Thanks very much for your help much appreciated me and my son will have a play around with adding the code for the relay tomorrow but again thanks very much.

BulldogLowell

Great.

Plus I can teach you to dynamically change the fade rate if you want the appearance of a faster/slower ramp up or ramp down.

;)

steve43

Yes that would be great. Hopefully my son and i can get the relay to work.

steve43

#36
May 08, 2016, 11:39 am Last Edit: May 08, 2016, 12:52 pm by steve43
Ok i got the relay to work were i wont it

really i need to adjust the ramp up speed, its too slow,but the ramp down is ok.

adjusting {led1, 10} sets the speed for ramp up and down. i need to be able to adjust ramp up independently from ramp down

is this possible.

also is it possible to add a line to keep the pir pin high when the flow switch is activated
this is to keep the bathroom leds on when your in the shower as the bathroom leds go out because the pir cant detect movement when in the shower. in other words the bathroom lights come on when walk in to the bathroom, the shower led comes on when the shower is started, the bathroom lights stay on, then shower off, shower led off, leave bathroom, bathroom lights off.but at the moment all this above works but bathroom lights go off when in shower until i come out of shower the bathroom lights come back on.

i hope you can understand what i am trying to do.

Thanks again
steve

BulldogLowell

adjusting {led1, 10} sets the speed for ramp up and down. i need to be able to adjust ramp up independently from ramp down
you can call the writeSpeed() method before you set the target set point like this:

Code: [Select]

  if(checkMotion())
  {
    ledSet[1].writeSpeed(50); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
    ledSet[1].write(255);
    Serial.print(F("Set to max brite at "));
    Serial.println(millis());
    pirTimer = millis();
  }
  if((millis() - pirTimer > TIMEOUT) && ledSet[1].setPoint() == 255)
  {
    ledSet[1].writeSpeed(250);  // Sets the speed of fade to 250ms per increment<<<<<<<<<<
    ledSet[1].write(0);
    Serial.println(F("Motion Timer Expired..."));
    Serial.print(F("Set to zero at "));


Regarding the lights, yes you can do what you want... think in terms of states:

IDLE = No Motion, No Flow     No Lights on
BATH = Motion, No Flow         Bath Lights On
Shower = Flow, No Motion      Shower and Bath Lights on

Then, develop your sensor to respond based on its three states...

BulldogLowell

also is it possible to add a line to keep the pir pin high when the flow switch is activated
this is to keep the bathroom leds on when your in the shower as the bathroom leds go out because the pir cant detect movement when in the shower. in other words the bathroom lights come on when walk in to the bathroom, the shower led comes on when the shower is started, the bathroom lights stay on, then shower off, shower led off, leave bathroom, bathroom lights off.but at the moment all this above works but bathroom lights go off when in shower until i come out of shower the bathroom lights come back on.
Something like this:

Code: [Select]

#include "Fade.h"
#define TIMEOUT 10000

enum BathState{
  EMPTY,
  BATH_OCCUPIED,
  SHOWER_OCCUPIED
};

const char* const stateText[] = {"Empty", "Bath Occupied", "Shower Occupied"};

const byte led2 = 10;          // Shower Led
const byte led1 = 9;           // Led connect to gate
const byte pir =  A1;          // PIR signal out
const byte flow =  A0;

Fade ledSet[] = {   // dimming pin, millis between steps
  {led1, 10},
  {led2, 10}}     
;

BathState bathState = EMPTY;
BathState lastState = BATH_OCCUPIED;

unsigned long flowTimer = 0;
unsigned long pirTimer = 0;

void setup()
{
  Serial.begin(9600);
  for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
  {
    ledSet[i].begin();
    ledSet[i].write(0);
  }
  pinMode(pir, INPUT);
  pinMode(flow, INPUT_PULLUP);
}

void loop()
{
  for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
  {
    ledSet[i].update();
  }
 
  switch (bathState)
  {
    case EMPTY:
      if(checkMotion())
      {
        ledSet[1].write(255);
        Serial.print(F("Set to max brite at "));
        Serial.println(millis());
        pirTimer = millis();
        bathState = BATH_OCCUPIED;
      }
      else if(ledSet[1].read() == 255)  // if it just came out of BATH_OCCUPIED state
      {
        ledSet[1].write(0);
      }
      break;
    case BATH_OCCUPIED:
      if(checkFlow())
      {
        ledSet[0].write(255);
        flowTimer = millis();
        bathState = SHOWER_OCCUPIED;
      }
      else if(ledSet[0].read() == 255) // if it just came out of SHOWER_OCCUPIED state
      {
        ledSet[0].write(0);
      }
      if((millis() - pirTimer > TIMEOUT))
      {
        ledSet[1].write(0);
        Serial.println(F("Motion Timer Expired..."));
        Serial.print(F("Set to zero at "));
        Serial.println(millis());
        bathState = EMPTY;
      }
      if(checkMotion())
      {
        pirTimer = millis(); // as long as motion is detected, prevent the timeout by updating the timer
      }
      break;
    case SHOWER_OCCUPIED:
      if((millis() - flowTimer > TIMEOUT))
      {
        ledSet[0].write(0);
        Serial.println(F("Flow Timer Expired..."));
        bathState = BATH_OCCUPIED;
      }
      if(checkFlow())  // as long as the shower is running, prevent the timeout by updating the timer
      {
        flowTimer = millis();
      }
      break;
  }
 
  if(bathState != lastState)
  {
    Serial.print(F("New Bath State is "));
    Serial.println(stateText[bathState]);
    lastState = bathState;
  }
}

bool checkFlow()
{
  static bool flowState = LOW;
  bool state = digitalRead(flow);
  if(flowState != state)
  {
    if(state == HIGH)
    {
      Serial.println(F("Flow Sensor Triggered..."));
      return true;
    }
    flowState = state;
  }
  return false;
}

bool checkMotion()
{
  static bool motionState = LOW;
  bool state = digitalRead(pir);
  if(motionState != state)
  {
    if(state == HIGH)
    {
      Serial.println(F("Motion Sensor Triggered..."));
      return true;
    }
    motionState = state;
  }
  return false;
}

steve43

OK

Thanks again

i have added the line of code but the led still on fade up slow if i put in a lower number or high it still only fade up slow. what would like is both leds to have a fast ramp up and slow ramp down.

But other than that it works great

BulldogLowell

maybe post your final code so I can see what you mean by ramp up different than ramp down...

they are the same function!


steve43

Hi

ok here is my sketch

#include "Fade.h"
#define TIMEOUT 100

enum BathState{
  EMPTY,
  BATH_OCCUPIED,
  SHOWER_OCCUPIED
};

const char* const stateText[] = {"Empty", "Bath Occupied", "Shower Occupied"};

const byte led2 = 10;          // Shower Led
const byte led1 = 9;           // Led connect to gate
const byte pir =  A1;          // PIR signal out
const byte flow =  A0;
const byte relay =  11;


Fade ledSet[] = {   // dimming pin, millis between steps
  {led1, 10},
  {led2, 10}}      
;

BathState bathState = EMPTY;
BathState lastState = BATH_OCCUPIED; 

unsigned long flowTimer = 0;
unsigned long pirTimer = 0;

void setup()
{
  digitalWrite(relay,HIGH);        // set pin HIGH first
  Serial.begin(9600);
  for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
  {
    ledSet.begin();
    ledSet.write(0);
  }
  pinMode(pir, INPUT);
  pinMode(flow, INPUT_PULLUP);
  pinMode(relay, OUTPUT);
}

void loop()
{
  for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
  {
    ledSet.update();
  }
  
  switch (bathState)
  {
    case EMPTY:
      if(checkMotion())
      {
        ledSet[1].writeSpeed(25); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
        ledSet[1].write(255);
        digitalWrite (relay, HIGH);
        Serial.print(F("Set to max brite at "));
        Serial.println(millis());
        pirTimer = millis();
        bathState = BATH_OCCUPIED;
      }
      else if(ledSet[1].read() == 255)  // if it just came out of BATH_OCCUPIED state
      {
        ledSet[1].write(0);
      }
      break;
    case BATH_OCCUPIED:
      if(checkFlow())
      {
        ledSet[1].writeSpeed(25); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
        ledSet[0].write(255);
        digitalWrite (relay, LOW);
        flowTimer = millis();
        bathState = SHOWER_OCCUPIED;
      }
      else if(ledSet[0].read() == 255) // if it just came out of SHOWER_OCCUPIED state
      {
        ledSet[0].write(0);
      }
      if((millis() - pirTimer > TIMEOUT))
      {
        ledSet[1].write(0);
        
        Serial.println(F("Motion Timer Expired..."));
        Serial.print(F("Set to zero at "));
        Serial.println(millis());
        bathState = EMPTY;
      }
      if(checkMotion())
      {
        pirTimer = millis(); // as long as motion is detected, prevent the timeout by updating the timer
      }
      break;
    case SHOWER_OCCUPIED:
      if((millis() - flowTimer > TIMEOUT))
      {
        ledSet[1].writeSpeed(50); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
        ledSet[0].write(0);
        
        Serial.println(F("Flow Timer Expired..."));
        bathState = BATH_OCCUPIED;
      }
      if(checkFlow())  // as long as the shower is running, prevent the timeout by updating the timer
      {
        flowTimer = millis();
      }
      break;
  }
  
  if(bathState != lastState)
  {
    Serial.print(F("New Bath State is "));
    Serial.println(stateText[bathState]);
    lastState = bathState;
    
  }
}

bool checkFlow()
{
  static bool flowState = LOW;
  bool state = digitalRead(flow);
  if(flowState != state)
  {
    if(state == HIGH)
    {
      Serial.println(F("Flow Sensor Triggered..."));
      return true;
    }
    flowState = state;
  }
  return false;
}

bool checkMotion()
{
  static bool motionState = LOW;
  bool state = digitalRead(pir);
  
  if(motionState != state)
  {
    if(state == HIGH)
    {
      Serial.println(F("Motion Sensor Triggered..."));
      return true;
    }
    motionState = state;
  }
  return false;
}


how do you add a sketch in to a post corretly

Delta_G

The compiler might have an issue with all the italics.  If you want help with your code then why don't you make it a little easier on us to help and put the code in code tags so we can work with it without having to go through and figure out why it is in italics. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

BulldogLowell

#43
May 08, 2016, 05:46 pm Last Edit: May 08, 2016, 05:46 pm by BulldogLowell
Code: [Select]

ledSet[1].writeSpeed(50); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
ledSet[0].write(0);


change the speed on object one and then dim object zero...?

try:
Code: [Select]

ledSet[0].writeSpeed(50); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
ledSet[0].write(0);


there are two fade objects to manipulate 0 and 1.

Got it?

steve43

Hi again

ok i think i got working now, ramp up speed is a bit slow but it does look great

Code: [Select]

   #include "Fade.h"
   #define TIMEOUT 100

   enum BathState{
   EMPTY,
   BATH_OCCUPIED,
   SHOWER_OCCUPIED
};

   const char* const stateText[] = {"Empty", "Bath Occupied", "Shower Occupied"};

   const byte led2 = 10;                           // Shower Led
   const byte led1 = 9;                            // Bathroom Led
   const byte pir =  A1;                           // PIR signal in
   const byte flow =  A0;                          // Flow Switch
   const byte relay =  11;                         // Fan Relay


   Fade ledSet[] = {                               // dimming pin, millis between steps
   {led1, 20},
   {led2, 10}}     
;

   BathState bathState = EMPTY;
   BathState lastState = BATH_OCCUPIED;

   unsigned long flowTimer = 0;
   unsigned long pirTimer = 0;

   void setup()
{
   digitalWrite(relay,HIGH);                          // set pin HIGH first
   Serial.begin(9600);
   for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
{
   ledSet[i].begin();
   ledSet[i].write(0);
}
   pinMode(pir, INPUT);
   pinMode(flow, INPUT_PULLUP);
   pinMode(relay, OUTPUT);
}

   void loop()
{
   for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
{
   ledSet[i].update();
}
 
   switch (bathState)
{
   case EMPTY:
   if(checkMotion())
{
   ledSet[1].writeSpeed(10);                               // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
   ledSet[1].write(255);
   digitalWrite (relay, HIGH);
   Serial.print(F("Set to max brite at "));
   Serial.println(millis());
   pirTimer = millis();
   bathState = BATH_OCCUPIED;
}
   else if(ledSet[1].read() == 255)                         // if it just came out of BATH_OCCUPIED state
{
       
   ledSet[1].write(0);
}
   break;
   case BATH_OCCUPIED:
   if(checkFlow())
{
       
    ledSet[0].write(255);
    ledSet[0].writeSpeed(15);                              // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
    digitalWrite (relay, LOW);
    flowTimer = millis();
    bathState = SHOWER_OCCUPIED;
}
    else if(ledSet[0].read() == 255)                       // if it just came out of SHOWER_OCCUPIED state
{
    ledSet[0].write(0);
}
    if((millis() - pirTimer > TIMEOUT))
{
    ledSet[1].write(0);
       
    Serial.println(F("Motion Timer Expired..."));
    Serial.print(F("Set to zero at "));
    Serial.println(millis());
    bathState = EMPTY;
}
    if(checkMotion())
{
    pirTimer = millis();                                  // as long as motion is detected, prevent the timeout by updating the timer
}
    break;
    case SHOWER_OCCUPIED:
    if((millis() - flowTimer > TIMEOUT))
{
       
    ledSet[0].write(0);
       
    Serial.println(F("Flow Timer Expired..."));
    bathState = BATH_OCCUPIED;
}
    if(checkFlow())                                       // as long as the shower is running, prevent the timeout by updating the timer
{
    flowTimer = millis();
}
    break;
}
 
    if(bathState != lastState)
{
    Serial.print(F("New Bath State is "));
    Serial.println(stateText[bathState]);
    lastState = bathState;
   
}
}

    bool checkFlow()
{
    static bool flowState = LOW;
    bool state = digitalRead(flow);
    if(flowState != state)
{
    if(state == HIGH)
{
    Serial.println(F("Flow Sensor Triggered..."));
    return true;
}
    flowState = state;
}
    return false;
}

    bool checkMotion()
{
    static bool motionState = LOW;
    bool state = digitalRead(pir);
 
    if(motionState != state)
{
    if(state == HIGH)
{
    Serial.println(F("Motion Sensor Triggered..."));
    return true;
}
    motionState = state;
}
    return false;
}




Thanks again BULLDOGLOWELL for all help, this is just what i have wanted for some time.

i put together a control system that just turns on the leds and fan using a pir, relays and 2 din rail times but this just doesn't look as good as fading in and out also it will be a lot quieter now with out all of the relays operating all the time.

Thanks again. i have learnt a lot from this but i am not any ware near you level.


Thanks

Regards

Steve

Go Up