Noob Sketch for laundry notifier Please help

Could you look over the code for any glaring errors? I'm sure there are a bunch. I'm still very new at programming for arduino.

Thanks

/* setup is as follows. Older washer and dryer wish mechanical round timers. Probe timer for 120vac. Connect 5vdc wall wart so when machine is one its outting out 5vdc. Use the wall wart to power a Solid State Relay with dc output, run dc from arduino regulated power thru it to sensor pin. Then I can use a 2N222 or similar to turn on another bigger solid state relay to power my LED status indicators. */ int loopDelay = 1000; // loop delay between readings set by user const int LEDpin = 13; // output pin for on board LED const int dryerlitePin = 5; // dryer lite trigger output pin const int washerlitePin = 4; // washer lite trigger output pin const int dryerPin = 2; // dryer sensor pin const int washerPin = 3; // washer sensor pin int dryerState = 0; // current state of the dryer, set to zero=off int washerState = 0; // current state of the washer, set to zero=off int lastDryerState = 0; // previous state of the dryer, set to zero=off int lastWasherState = 0; // previous state of the washer, set to zero=off

void setup() { pinMode(LEDpin,OUTPUT); // initialize LED pin as output for user monitoring digitalWrite(LEDpin,LOW); // set LED off pinMode(dryerlitePin, OUTPUT); // initialize the dryer lite pin as an output digitalWrite(dryerlitePin,LOW); // set the dryerer lite pin LOW (lite off) pinMode(washerlitePin, OUTPUT); // initialize the washer lite pin as an output digitalWrite(washerlitePin,LOW); // set the washer lite pin LOW (lite off) pinMode(dryerPin, INPUT); // initialize the dryer pin as an input from hall switch pinMode(washerPin, INPUT); // initialize the washer pin as an input from hall switch } void loop() { digitalWrite(LEDpin,HIGH); // flash Green LED to indicate loop is active delay(20); digitalWrite(LEDpin,LOW); { //begin dryer readings if (dryerPin == LOW) digitalWrite(dryerlitePin, HIGH); // check if power is coming from dryer if not switch on dryer done lite //User will need to reset.

//end of dryer readings } { //begin washer readings if (dryerPin == LOW) digitalWrite(washerlitePin, HIGH); // check if power is coming from dryer if not switch on dryer done lite //User will need to reset. } //end of washer readings delay (loopDelay); // optional delay set by user }

const int  dryerPin = 2;    // dryer sensor pin
  //begin dryer readings
  if (dryerPin == LOW) digitalWrite(dryerlitePin, HIGH); // check if power is coming from dryer if not switch on dryer done lite
  //User will need to reset.

2 will never be equal to LOW.

All the curly brackets in the loop function apart from the first and the last are superfluous - get rid of them. You have a copy and paste error on the check of the washer - you're checking the dryer pin instead, Although that won't matter until you address the problem dxwood identified.

That delay() at the end is not doing anything useful. Get rid of it.

dxw00d: const int  dryerPin = 2;    // dryer sensor pin

  //begin dryer readings
  if (dryerPin == LOW) digitalWrite(dryerlitePin, HIGH); // check if power is coming from dryer if not switch on dryer done lite
  //User will need to reset.

2 will never be equal to LOW.

Why not if I may ask?

You need to compare low to the result of a digitalread. You're just comparing the pin number (2) to LOW (0) which will never be true.

Edit: LOW is 0 - d'oh!

Because, in Arduino.h:

...
#define LOW  0x0
#define HIGH 0x1
...

LOW is defined as zero, which will never equal two.

You were too fast for me - I realized my error as soon as I hit post :disappointed_relieved:

Ok. I removed the correction.

but I thought a digital pin was LOW if it had less than 2 volts on it and high if it had higher (if set as an input)? if so why do I have to compare it? and how do I?

OK I see what you mean, I'll keep reading my book.

You want to compare the state of the pin, not the number of the pin, to some value. You get the state of the pin using digitalRead() with the pin number as the argument.

At least this verifies in arduino 1.01... am I even close?

/*setup is as follows. Older washer and dryer wish mechanical round timers. Probe timer for 120vac. Connect 5vdc wall wart so when machine is one its outting out 5vdc. Use the wall wart to power a Solid State Relay with dc output, run dc from arduino regulated power thru it to sensor pin. Then I can use a 2N222 or similar to turn on another bigger solid state relay to power my LED status indicators */ const int LEDpin = 13; // output pin for on board LED const int dryerlitePin = 5; // dryer lite trigger output pin const int washerlitePin = 4; // washer lite trigger output pin const int dryerPin = 2; // dryer sensor pin const int washerPin = 3; // washer sensor pin int dryerState = 0; // current state of the dryer, set to zero=off int washerState = 0; // current state of the washer, set to zero=off int lastDryerState = 0; // previous state of the dryer, set to zero=off int lastWasherState = 0; // previous state of the washer, set to zero=off int washersensor; int dryersensor;

void setup() { pinMode(LEDpin,OUTPUT); // initialize LED pin as output for user monitoring digitalWrite(LEDpin,LOW); // set LED off pinMode(dryerlitePin, OUTPUT); // initialize the dryer lite pin as an output digitalWrite(dryerlitePin,LOW); // set the dryerer lite pin LOW (lite off) pinMode(washerlitePin, OUTPUT); // initialize the washer lite pin as an output digitalWrite(washerlitePin,LOW); // set the washer lite pin LOW (lite off) pinMode(dryerPin, INPUT); // initialize the dryer pin as an input sensor pinMode(washerPin, INPUT); // initialize the washer pin as an input sensor } void loop() { digitalWrite(LEDpin,HIGH); // flash Green LED to indicate loop is active delay(20); digitalWrite(LEDpin,LOW);

//begin washer readings washersensor = digitalRead(washerPin); if (washersensor == HIGH) // check voltage off washer sensor digitalWrite(washerlitePin, HIGH) ; // if sensor voltage is at 5v washer SHOULD be done THEN sets the complete lite else digitalWrite(washerlitePin, LOW) ; // if below 2v washer is NOT done //end washer readings

//begin dryer readings dryersensor = digitalRead(dryerPin); if (dryersensor == HIGH) // check voltage off dryer sensor digitalWrite(dryerlitePin, HIGH) ; // if sensor voltage is at 5v dryer SHOULD be done THEN sets the complete lite else digitalWrite(dryerlitePin, LOW) ; // if below 2v dryer is NOT done //end dryer readings

}

am I even close?

Yes, but let's look at a couple of things.

  washersensor = digitalRead(washerPin);

The name washerPin makes a lot of sense. It suggests to me, without looking at any other part of your code that there is a something attached to a pin that makes some sense as a washer.

Given that there is a similar statement later on that references dryerPin, I can see that this code is doing something with a washer and dryer.

The name washersensor, on the other hand, means very little. I can see that it is related to the washer, but the sensor part means nothing. The unused variable, washerState, makes a lot more sense.

Look at washersensor and washerState. Capitalizing the s in State makes it stand out. Clearly there are two words in the name. In the first name, it is harder to see that there are two words.

Get rid of washersensor and dryersensor, Use washerState and dryerState.

Fix the l in dryerlitePin and washerlitePin.

Finally, what are lastWasherState and lastDryerState for? At some point, you are going to realize that they are needed, and need to be valued at the right time, and that they need to be considered at the appropriate time.

You want to detect when the state of the washer pin transitions from LOW to HIGH. The rest of time, when the pin is LOW before transitioning to HIGH and when it is HIGH after transitioning to HIGH, you don't care about it's state. You only care when it changes from LOW to HIGH. That is what lastWasherState is for. It should hold the state of the pin last time, so that you can compare the current state of the pin to it, to determine when the transition occurred (the two are not the same).

You might even decide that turning the washer light off when the opposite transition occurs is a good idea. That way, you don't need to reset the Arduino each time you do a load of laundry.

ok, ive made a few of your changes, haven't figured out how to do all of them yet...

/*setup is as follows. Older washer and dryer wish mechanical round timers. Probe timer for 120vac. Connect 5vdc wall wart so when machine is one its outting out 5vdc. Use the wall wart to power a Solid State Relay with dc output, run dc from arduino regulated power thru it to sensor pin. Then I can use a 2N222 or similar to turn on another bigger solid state relay to power my LED status indicators */ const int LEDpin = 13; // output pin for on board LED const int dryerLitePin = 5; // dryer lite trigger output pin const int washerLitePin = 4; // washer lite trigger output pin const int dryerPin = 2; // dryer sensor pin const int washerPin = 3; // washer sensor pin int dryerState = 0; // current state of the dryer, set to zero=off int washerState = 0; // current state of the washer, set to zero=off int lastDryerState = 0; // previous state of the dryer, set to zero=off int lastWasherState = 0; // previous state of the washer, set to zero=off

void setup() { pinMode(LEDpin,OUTPUT); // initialize LED pin as output for user monitoring digitalWrite(LEDpin,LOW); // set LED off pinMode(dryerLitePin, OUTPUT); // initialize the dryer lite pin as an output digitalWrite(dryerLitePin,LOW); // set the dryerer lite pin LOW (lite off) pinMode(washerLitePin, OUTPUT); // initialize the washer lite pin as an output digitalWrite(washerLitePin,LOW); // set the washer lite pin LOW (lite off) pinMode(dryerPin, INPUT); // initialize the dryer pin as an input sensor pinMode(washerPin, INPUT); // initialize the washer pin as an input sensor } void loop() { digitalWrite(LEDpin,HIGH); // flash Green LED to indicate loop is active delay(20); digitalWrite(LEDpin,LOW);

//begin washer readings washerState = digitalRead(washerPin); if (washerState == HIGH) // check voltage off washer sensor digitalWrite(washerLitePin, HIGH) ; // if sensor voltage is at 5v washer SHOULD be done THEN sets the complete lite else digitalWrite(washerLitePin, LOW) ; // if below 2v washer is NOT done //end washer readings

//begin dryer readings dryerState = digitalRead(dryerPin); if (dryerState == HIGH) // check voltage off dryer sensor digitalWrite(dryerLitePin, HIGH) ; // if sensor voltage is at 5v dryer SHOULD be done THEN sets the complete lite else digitalWrite(dryerLitePin, LOW) ; // if below 2v dryer is NOT done //end dryer readings

}