Go Down

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

BulldogLowell

#15
May 07, 2016, 06:57 pm Last Edit: May 07, 2016, 07:01 pm by BulldogLowell
they are both in this directory?

C:\Documents and Settings\Steve\My Documents\Arduino\NewFade\

can you see them in the IDE like this:




steve43

My son has made the two fade files work and the main sketch worked but the triggering is slightly incorrect. one sent of leds pulse rapidly

BulldogLowell

My son has made the two fade files work and the main sketch worked but the triggering is slightly incorrect. one sent of leds pulse rapidly
not sure what you mean...

Can you post the output from the Serial Monitor when motion is detected?

steve43

Thanks a lot for the code there are some things that arnt quite right with it so it will need debugging like you said.
In serial moniter it says flow sensor triggered then after ten seconds flow timer expired and then the leds go out,but when you push the button again the leds flicker and the serial moniter says flow timer expired and flow sensor triggered repeatedly.
When pir is triggered the other set of leds flicker in the same way the serial moniter is then frozen and no more info is displayed the only way to unfreeze is to reset the arduino and restart serial moniter.

BulldogLowell

try debugging one at a time...

I commented out a block and added some more prints

Can you post the output from the Serial Monitor when it detects motion?

Make sure your pins are connected as indicated in the top of the sketch

Code: [Select]
#include "Fade.h"
#define TIMEOUT 1000

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}}     
;

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);
}

void loop()
{
  for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
  {
    ledSet[i].update();
  }
 
//  if(checkFlow())
//  {
//    ledSet[0].write(255);
//    flowTimer = millis();
//  }
//  if((millis() - pirTimer > TIMEOUT) && ledSet[0].setPoint() == 255)
//  {
//    ledSet[0].write(0);
//    Serial.println(F("Flow Timer Expired..."));
//  }
 
  if(checkMotion())
  {
    ledSet[1].write(255);
    Serial.print(F("Set to max brite at "));
    Serial.println(millis());
    pirTimer = millis();
  }
  if((millis() - flowTimer > TIMEOUT) && ledSet[1].setPoint() == 255)
  {
    ledSet[1].write(0);
    Serial.println(F("Motion Timer Expired..."));
    Serial.print(F("Set to zero at "));
    Serial.println(millis());
  }
}

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

Now only one strip of leds work and this is the info given from the serial monitor.
Motion Sensor Triggered...
Set to max brite at 2855
Motion Timer Expired...
Set to zero at 2885
Motion Sensor Triggered...
Set to max brite at 2941
Motion Timer Expired...
Set to zero at 2990
Motion Sensor Triggered...
Set to max brite at 3046
Motion Timer Expired...
Set to zero at 3094
Motion Sensor Triggered...
Set to max brite at 3149
Motion Timer Expired...
Set to zero at 3197

BulldogLowell

#21
May 07, 2016, 08:02 pm Last Edit: May 07, 2016, 08:04 pm by BulldogLowell
yes, we are debugging PIR only right now


ahhhh.....

change this line (line 54) :EDIT added line number

Code: [Select]
if((millis() - flowTimer > TIMEOUT) && ledSet[1].setPoint() == 255)

to this:

Code: [Select]
if((millis() - pirTimer > TIMEOUT) && ledSet[1].setPoint() == 255)

see my (cut/paste) error?

steve43

Okay the pir now works fine. Where do i adjust the ramp up and down speed of the fade?

BulldogLowell

Okay the pir now works fine. Where do i adjust the ramp up and down speed of the fade?
second value in the constructor:

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


change 10 to 50 and see what happens...

steve43

Changing them to 50 slows the time it takes to dim but not ramp up. Also the second strip of leds still flickers like the first set before the fix.

BulldogLowell

Changing them to 50 slows the time it takes to dim but not ramp up. Also the second strip of leds still flickers like the first set before the fix.
it may just appear that way, make it longer like 100 or 200 and see.

you haven't debugged the other Led yet!


BulldogLowell

#26
May 07, 2016, 08:22 pm Last Edit: May 07, 2016, 08:22 pm by BulldogLowell
Changing them to 50 slows the time it takes to dim but not ramp up. Also the second strip of leds still flickers like the first set before the fix.
Ha!

Same error on line 41... should be:

Code: [Select]
if((millis() - flowTimer > TIMEOUT) && ledSet[0].setPoint() == 255)

so all together:

Code: [Select]

#include "Fade.h"
#define TIMEOUT 1000

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}}     
;

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);
}

void loop()
{
  for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
  {
    ledSet[i].update();
  }
 
  if(checkFlow())
  {
    ledSet[0].write(255);
    flowTimer = millis();
  }
  if((millis() - flowTimer > TIMEOUT) && ledSet[0].setPoint() == 255)
  {
    ledSet[0].write(0);
    Serial.println(F("Flow Timer Expired..."));
  }
 
  if(checkMotion())
  {
    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].write(0);
    Serial.println(F("Motion Timer Expired..."));
    Serial.print(F("Set to zero at "));
    Serial.println(millis());
  }
}

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

Second set of leds work once and displays this below but does not stay on for the set time and then dims down but wont start again and the led just flickers rapidly.
Flow Sensor Triggered...
Flow Sensor Triggered...
Flow Sensor Triggered...
Flow Sensor Triggered...
Flow Sensor Triggered...
Flow Sensor Triggered...
Flow Sensor Triggered...
Flow Timer Expired...
Flow Sensor Triggered...
Flow Timer Expired...
Flow Sensor Triggered...
Flow Timer Expired...
Flow Sensor Triggered...
Flow Timer Expired...
Flow Sensor Triggered...
Flow Timer Expired..

BulldogLowell

your flow sensor looks bouncy... you can see it is changing state frequently, not like the PIR at all

How do you want it to work, considering that it is changing states so often?

you can ignore the additional pulses after the detection of the first while the leds are on, for example.

steve43

I have tested both sets of leds and they both work as i want them to perfectly but the ramp up time is to slow i want them to brighten up quicker

Go Up