Code needed for my project

Hello, this is my first post.

I’m brand new to arduino and have been at my first project for over a week now... staying up late... reading books... and still can’t get my project done.

I’m excited to learn much more but I’m now desperate for help... I need my project to work before a party coming up and I’m up against the clock.

MY PROJECT:

I have a 4-channel relay. I also have a magnetic door switch. I have a sketch that I can get to play through when the door is open BUT, If the door is still open after the sketch plays through, I don’t want it to loop again.

I want to open the door to trigger the sketch, then absolutely nothing happens again UNTIL the door has SHUT again and then opens again, which would trigger the sketch to play through once again.

For now the way I have it, of course arduino sees that when the switch is triggered it plays through the sketch.. but if the door is still open it sees the switch triggered and plays through again and again because the door is still open.

I’d pull my hair out but I don’t wanna lose any more of it. I’ve tried so many different codes and functions and 1)don’t know what I’m doing yet and 2)haven’t achieved my goal.

I’d be so thankful if anyone could post some code to get my sketch to work properly with the magnetic door switch. I can get the relays to do their thing just fine.. it’s the switch that’s my worst enemy right now. Need arduino to know not to keep looping if the door hasn’t been shut again yet.

Please help :frowning:

You need to detect when the door becomes open rather than when it is open

Look at the StateChangeDetection example in the IDE to see how to do it

Thank you for pointing me in that direction. Hours and hours later, still can’t get it to work. Don’t understand it at all yet.

I’m going to learn this systematically at a later point in time but at the moment I really do just need assistance with what exact code to put into IDE.. not what to do or ideas of what functions would work. I’m simply not at that level yet. Of course everything has to be exactly right and with all the possibilities and codes I keep getting random results. I’m worse off that when I even started now.

As asked for in the first place, if anyone could post the code for this it would be appreciated. And it would be incredibly encouraging to see that code exists to make this “simple” project work. I’m starting to give up already and I don’t usually give up easily on things but arduino is really a pain for me so far, cause I’m not just making an LED blink.. I’m trying to get an actual project running. Again, I know I can learn it given time but I’m against the clock and really need code at this point and I can learn to understand and write if myself at a later time. Does anyone on this forum know how to write code for this project?

  1. Door (magnetic switch) opens: relay sequence plays through.

Then... nothing else happens, even if door is still open. (No looping.. it just played once).

  1. door shuts (still nothing happens).

  2. door opens again (relay sequence plays again, just once then stops)

And on and on...

Anyone? :frowning:

Rockinbocker:
I’m going to learn this systematically at a later point in time but at the moment I really do just need assistance with what exact code to put into IDE.. not what to do or ideas of what functions would work....

Rockinbocker:
As asked for in the first place, if anyone could post the code for this it would be appreciated. And it would be incredibly encouraging to see that code exists to make this “simple” project work. ... Does anyone on this forum know how to write code for this project?

If these are your commitment, then this forum is not a right place for you. I suggest you to ask a mod to move your thread to a more suitable forum, where there will be people who are gladly to write code for you.

The reason is most of us here have a 9-5 job, so we don't have the time or energy to see through a whole project. The "Gigs and Collaboration" forum will have people to fulfill your requirement, but of course, they are also expected to be compensated.
Good luck!

I can’t imagine something as simple as getting a switch to work when you need it to owould require some kind of job order. I’d think it would be like one of the basics of arduino.? I simply don’t know what tactic to use to get the switch working correctly.

I would think paying someone for this elementary bit of code would be like paying a portrait artist to mix some paint up. Unless I’m mistaken and arduino is just a nightmare to make even basic things happen.

I’m a musician and if someone needed to know how to get from the A chord to E, there are several chords I could tell them to use. Or if they need to know how to structure the song I can tell them how to arrange the verses, choruses and bridge to end with a satisfactory result. My point is I’m not asking someone to write me a whole song.. I just need to know what is a good order to put things to make it work. I have my relay sketch working beautifully, and I have my switch which works... just need a little help that’s all.

I’ve found no example sketches that do what I need my switch to do, and nobody has been able to help, although I do honestly appreciate the responses very much.

I’ve seen a lot of other questions answered for other inquires (do this line of code or that line, to make what you want to happen), but no luck on this one.

I’d think it would be like one of the basics of arduino.

It is, and the example suggested illustrates the principle

the door starts closed - that is its previous state
start of loop()
  read the state of the door
    if the door was previously closed and is now open then do what you need
  save the current state of the door as its previous state
end of loop()

Why don’t you post what you have and we can offer suggestions tailored specifically to your situation?

Why is it that people keep turning up in here with no ability to program and absolutely no idea how to do what they want doing but despite that the one thing they're always convinced of is that their project is really simple?

If it was really that simple you'd have written it and had it working by now.

Rockinbocker:
I just need to know what is a good order to put things to make it work. I have my relay sketch working beautifully, and I have my switch which works... just need a little help that’s all.

Fine, so post here your beautifully working relay sketch and the code for your switch which works. Tell us what they do then explain what problems you're having in getting them to work together.

Steve

Well turns out it WAS simple, and for someone who's only been doing this less than a week! Just as I thought, I just needed to know a few simple lines of code. Only took about 15 seconds to type it out. Project works like a charm every single time with no errors.

So since a clear answer wasn't provided I wanted to share my sketch with everyone in case someone else is creating a similar project and would like to know how to make this work.

Wanna hook up a magnetic reed switch to a door and have a sketch play through once, and ONLY when the door goes from closed to open? Here ya go! (and I'm brand new at this, so I expect there to be formatting issues or something else out of place, but all I know is it gets the job done.)

/*this sketch demonstrates how a magnetic reed switch can control your project.  The switch can be attached to a door or window.    
 * when the door is opened, the sketch plays through once.  Once it returns to the beginning of the loop, the "while" loop 
 * repeatedly checks to see if the door is closed again yet.  If the door is still open, nothing happens.  As soon as the door 
 * closes again it effectively resets the sketch, and when the door opens once again, the sketch plays through entirely.  
*/

//pin 5 is controlling relay #1 on a relay board.  I am calling it "sound" since it controls an MP3 player.
  int sound = 5; 
//pin 6 is controllng relay #2 on the relay board.  I am calling it "spinningLight" since it controls an FX light
  int spinningLight = 6; 
//pin 7 is controlling relay #3 on the relay board.  I am calling it "fan" since, yes it controls a fan.
  int fan = 7;   
//pin 8 is controlling relay #4 on the relay board.  I am calling it "floodlight" since it controls a floodlight.
  int floodlight = 8; 

void setup() {
  //start serial connection
  Serial.begin(9600);
  //configure pin2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT);
  digitalWrite(2, HIGH); 
  //configure pins 5-8 as outputs.  These are feeding signals to the relays
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  //ensure the relays are all off when sketch begins (a high signal will turn the relays off)
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
}


void loop() {
  //read the switch value into a variable
int sensorVal = digitalRead(2);
digitalWrite(2, HIGH);

//the while loop keeps the whole loop from just automatically playing over and over
//I've indicated that if the switch value is "low" that means the door is still open
//return detours everything back to the beginning of the loop
while (sensorVal == LOW)
{return;
  }
{
//the arduino has now gone past the while loop, since the switch is registering "HIGH"
//the door has been shut, so now the arduino will check for a signal again from switch...
  while (digitalRead(2)); 
 //the main sequence of the sketch now begins to play through... things turn on and off
    digitalWrite(sound, LOW);
    delay(5500);
    digitalWrite(spinningLight, LOW);
    delay(1250);
    digitalWrite(fan, LOW);
    delay(500);
    digitalWrite(floodlight, LOW);
    delay(1500);
    digitalWrite(floodlight, HIGH);
    delay(300);
    digitalWrite(floodlight, LOW);
    delay(300);
    digitalWrite(floodlight, HIGH);
    delay(150);
        digitalWrite(floodlight, LOW);
    delay(150);
    digitalWrite(floodlight, HIGH);
    delay(100);
    digitalWrite(fan, HIGH);
        digitalWrite(floodlight, LOW);
    delay(100);
    digitalWrite(floodlight, HIGH);
    delay(50);
        digitalWrite(floodlight, LOW);
    delay(50);
    digitalWrite(floodlight, HIGH);
    delay(35);
        digitalWrite(floodlight, LOW);
    delay(35);
    digitalWrite(floodlight, HIGH);
    delay(25);
        digitalWrite(floodlight, LOW);
    delay(25);
    digitalWrite(floodlight, HIGH);
    delay(25);
    digitalWrite(floodlight, LOW);
    delay(25);
        digitalWrite(floodlight, HIGH);
    delay(25);
    digitalWrite(floodlight, LOW);
    delay(25);
        digitalWrite(floodlight, HIGH);
    delay(25);
    digitalWrite(floodlight, LOW);
    delay(25);
        digitalWrite(floodlight, HIGH);
    delay(25);
    digitalWrite(floodlight, LOW);
    delay(25);
        digitalWrite(floodlight, HIGH);
    delay(25);
    digitalWrite(floodlight, LOW);
    delay(25);
        digitalWrite(floodlight, HIGH);
    delay(25);
    digitalWrite(floodlight, LOW);
    delay(25);
        digitalWrite(floodlight, HIGH);
    delay(25);
    digitalWrite(floodlight, LOW);
    delay(25);
        digitalWrite(floodlight, HIGH);
    delay(25);
    digitalWrite(floodlight, LOW);
    delay(25);
        digitalWrite(floodlight, HIGH);
    delay(25);
    digitalWrite(floodlight, LOW);
    delay(25);
    digitalWrite(floodlight, HIGH);
    digitalWrite(spinningLight, HIGH);
    delay(4000);
    digitalWrite(sound, HIGH);
    delay(250);
}
 }
//main sequence is now over.  Back to the beginning.