one more new guy to add to the list ..LOL help with coding

so it has been any days just trying to get to where i am now and if some one could help me feel in what i need and all so show me how in text in my file would help as i still have alot to add

at this time i am trying to get a lighting system working from a set of buttons and relays

i have 2 sets of lighting 12vdc and 110ac lighting that i want to work from one button

loop
push button one time to
pin 6 on
again to make
pin 6 and 7 come on
again to make
pin 6 and 7 off Or just a timed long press for 6 and 7 to go off so you can go right to off from step 1 if need be

all so as it is now if i hold the button to long the lights come on and off so i need to add a delay or code so it dos not do this

all so i have 2 buttons just not in the same place as you can see in my code so the 2ed button can work like pin 6 but on the other side of the home

i am trying to under stand the IF and ELSE but i just being so new at this cant see what i need or where to start

thanks for any help in helping me under stand what i am doing

...

int hallway = 11;
int doorway = 12; // this is working

int relay110 = 7;
int relayout = 5;
int relay12v = 6; // this is working

int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers

void setup()
{
pinMode(doorway, INPUT);
pinMode(hallway, INPUT);
pinMode(relay12v, OUTPUT);
pinMode(relayout, OUTPUT);
pinMode(relay110, OUTPUT);
}

void loop()
{
reading = digitalRead(hallway);

// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();
}

digitalWrite(relay12v, state);

previous = reading;

reading = digitalRead(doorway);

// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();
}

digitalWrite(relay12v, state);

previous = reading;

Please read the two posts at the top of this Forum by Nick Gammon. It will show you the proper way to make a post here, especially the use of code tags for source code listing. Also, get a keyboard that has a Shift key so you can capitalize letters correctly.

[color=#222222]    if (state == HIGH)[/color][color=#222222][/color]
[color=#222222]      state = LOW;[/color][color=#222222][/color]
[color=#222222]    else[/color][color=#222222][/color]
[color=#222222]      state = HIGH;[/color]
[color=#222222]

If state is HIGH, why make it HIGH again with the else? It will not have changed.

Any put your code in [ code ] [ /code ] tags, otherwise copy & paste gets hosed up.

so it has been any days just trying to get to where i am now and if some one could help me feel in what i need and all so show me how in text in my file would help as i still have alot to add

My post How to use this forum which others have recommended you read, suggests not only using code tags, but also to remember that you are asking people to give up their time, for nothing, to help you. Take the courtesy to make proper sentences.

Sentences, you may recall, start with a capital letter. They contain commas, and other punctuation. They end with a period or full-stop.

It is particularly annoying when someone, who has taken no trouble to read the posting guidelines, or post in proper English, then turns around and reports people who are trying to help them to the moderators.

Perhaps another forum would suit you better? Try: http://www.avrfreaks.net/

  pinMode(doorway, INPUT);
  pinMode(hallway, INPUT);

You have external resistors wired with these switches, right?

Are reading and previous supposed to be related to the doorway switch OR the hallway switch? Both is the wrong answer. To save you the trouble, so is yes.

Is state supposed to be the state of the 12v relay, the out relay, or the 110 relay? Yes is the wrong answer.

See if you can figure out what

   state = !state;

does.

Replace 4 lines of code with 1.

Hi,
I think it time for;

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom.... :slight_smile:

Sounds like a simple binary counter that starts at zero then when pressed it goes to 2, pressed again it increments to 3, pressed again it goes to 4 in which case it resets back to 0.

With this you get the results you are looking for. To turn on the corresponding pin, simply mask off the bit.

0x02 would be for pin 6
0x03 both pins 6 and 7 (0x01) would be on
0x04 counter will reset back to zero.

thank you every one for taking your time to help me out

Nick Gammon and econjack sorry my education and spelling is not as high as yours
if it was i would not be here asking for help

sorry i do not know the right way to add the code tags. as i really do not know what to add or how to say it

thanks for adding posts to make others look away or move on .. its sad to see that same thing so small in ones eyes can drive others nuts

to the others thanks again so much for stopping by and giving a helping hand

some more info

i am using a Relay Shield V2. on a Arduino uno

You have external resistors wired with these switches, right? yes i do and have the wiring side right

i am trying really hard to under stand what some of you guys have said but i am really new to alot of this

that is why i was hoping some one could help me feel in the code with // notes to help tell me why and what its for

i under stand the small stander stuff and seen alot videos and read alot of info but its hard to find any info on what i am trying to do and it seems like its alot of ..IF and ..ELSE tags

yes hazards thats about it

HazardsMind:
Sounds like a simple binary counter that starts at zero then when pressed it goes to 2, pressed again it increments to 3, pressed again it goes to 4 in which case it resets back to 0.

With this you get the results you are looking for. To turn on the corresponding pin, simply mask off the bit.

0x02 would be for pin 6
0x03 both pins 6 and 7 (0x01) would be on
0x04 counter will reset back to zero.

so i am looking in the wrong place with the IF statement

CrossRoads:

[color=#222222]    if (state == HIGH)[/color][color=#222222][/color]

     state = LOW;
   else
     state = HIGH;
[color=#222222]





If state is HIGH, why make it HIGH again with the else? It will not have changed.

sorry i do not know the right way to add the code tags. as i really do not know what to add or how to say it

Which is why I posted a graphic on the page How to use this forum:

It's nothing to do with education. Can you understand this or not?

Or, you can type in the things in the red boxes:

sorry i do not know the right way to add the code tags. as i really do not know what to add or how to say it

#7 below. Also if you have problems properly "coding" a written language for people, what are your chances of getting computer code properly formatted?

http://forum.arduino.cc/index.php/topic,148850.0.html

ok i think i under stand now

I was lost as my pop up blocker/ no script was not showing the upper window

I had the texted window and that is why i never seen what you guys where talking about

he was right in saying
Sounds like a simple binary counter that starts at zero then when pressed it goes to 2, pressed again it increments to 3, pressed again it goes to 4 in which case it resets back to 0.

With this you get the results you are looking for. To turn on the corresponding pin, simply mask off the bit.

0x02 would be for pin 6
0x03 both pins 6 and 7 (0x01) would be on
0x04 counter will reset back to zero.

int hallway = 11;        // this well be the same thing as 12 just not felled in the texted yet 
int doorway = 12;      // this is working   wiring button and all  but if i hold to long comes on and off 

int relay110 = 7;
int relayout = 5; 
int relay12v = 6;      // this is working  wiring button and all

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(doorway, INPUT);                  
  pinMode(hallway, INPUT);                    
  pinMode(relay12v, OUTPUT);              
  pinMode(relay110, OUTPUT);
  pinMode(relayout, OUTPUT);               
}

void loop()
{
  reading = digitalRead(hallway);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(relay12v, state);

  previous = reading;

  reading = digitalRead(doorway);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(relay12v, state);

  previous = reading;
}

I don't have a clue what the code is doing, but it does look better. :slight_smile:

  reading = digitalRead(hallway);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {

Wouldn't currState and prevState look like they went together? reading and previous do not look they should be being compared in any way, shape, or form.

If the switch is bouncing, does it matter what the current state or the previous state are?

Sometimes more than one if statement is better than one. This is such a case.

previous = reading;

reading = digitalRead(doorway);

You are STILL trying to use the current state and the previous state variables to hold the current state and previous state of two DIFFERENT pins. That will NEVER work. Get over it.

thanks every one for trying

after weeks of reading and trying to find out how to do this i have decided to move on

this was the only info and video i could find to show what i was trying to do

i even coped his older code and still no go

i have posted else where and every one is fast to show and say what i did wrong but not how to do it or if they are i just did not under stand it

i did not want to leave a open post like this so i wanted to add a end

again thanks every one

No debounce, but this is what I believe you were trying to do.

The video you showed is doing what is called bit shifting. In this case the guy makes a variable of type byte, and when the button is pressed, the code shifts in a single bit (1) into the variable, then on the next press it shifts that bit once to the left then added another bit. The process keeps going forever (which will cause overflow, which then may cause errors down the road) or until it is told to do something when the value of that variable has reached a certain number. What you see is when the value of that variable reaches 31 or (00011111) it then resets back to zero.

00000000 (empty variable)
(press) 00000001 (add bit)
(press) 00000011 (shift bit left by 1 and add another)
(press) 00000011 . . . 00011111 (there will be an IF statement checking the value, and when it is 31 (00011111) reset
00000000 (reset variable)

The code:

const byte LED_1 = 6;
const byte LED_2 = 7;
const byte Button = 2;

byte last = LOW; // last set to LOW, because I am enabling the pullup resistor ie INPUT_PULLUP
byte count = 0;

void setup()
{
  Serial.begin(115200);
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(Button, INPUT_PULLUP);
}

void loop()
{ 
  byte ButtonState = digitalRead(Button);

  if (ButtonState != last) // if the button is different from its last state, go in.
  {
    if (ButtonState == LOW) // if the button was pressed ie brought to ground (LOW), go in.
    {
      if(count == 0) // if count is equal to zero, set it now to 2
        count = 2;
      else // otherwise just increment it by 1
        count++;
      
      if(count >= 4) // if count is equal to 4, reset it back to 0 (I made it greater than or equal, just as a precaution, but you can just have it as count == 4 and it will still work)
        count = 0;
        
      Serial.println(count); // show what count is
      digitalWrite(LED_1, !!(count & 0x02)); // the !! takes what is in the parenthesis and makes it a logical 1 or 0 depending on the value. 0 = 0 and anything greater or less than 0 equals 1.
      // count & 0x02 is what you use to mask off the second bit in the variable
      
      digitalWrite(LED_2, !!(count & 0x01)); //count & 0x01 this masks off the first bit in the variable
    }
    
    last = ButtonState; // update the last state of the button, prevent the code from doing anything when the button is held down.
  }
}