newby help

I have an Arduino mega that I am using to monitor a few switches and act when the state of the switches change. I am trying to write a line to the serial port when the state of a switch changes. Since this is in a loop the string gets continuously printed. I have tried a couple of different approaches that are not quite working. I am thinking that there may be an easier way of doing this. Can anyone enlighten me. Thank you

Here is an example of what I am trying to do. I would like to just println() once when the state of the switch changes.

void loop()
{
if (switch1 == HIGH)
{
Serial.println("switch open");
}
else
{
Serial.println("switch closed");
}

}

Static variables inside a function hold their value between calls to that function.
They can also be given a starting value.

void loop()
{
static char lastState = !switch1;

if( lastState != switch1 ){

  lastState = switch1;

  if (switch1 == HIGH){
    Serial.println("switch open");
  }else{
    Serial.println("switch closed");
  }
}

}

By the code you just have, your if statement is correct, butI don't see that you're using a variable for the conditional value.

What I would suggest is, before the if statement, inside the void loop put a digital read that writes to a variable instead of the pin itself. Before setup, initialize a variable like this: "int Variable = 0"

Here's an example of a working if statement.

void loop()
{
Variable = digitalRead(switch1);
If (Variable == HIGH)
....

If you don't understand, check the "Button" example in the program.

It's probably a good idea to add a call to digitalRead() somewhere, so you actually have the switch state to work with.

Also, have a look at the state change detection example.

Thank you for the replies. I will try these ideas. There is a lot more code, reading inputs and firing relays.

Here is the code that I have for now. It seems to be working OK. If anyone has any suggestions as to how to improve this please show me. thanks again for all of the help.

const int Switch1 = 28;
const int Switch2 = 30;
const int Switch3 = 32;
//unused 34
const int Output1 = 7;              
const int Output2 = 5;
const int Output3 = 6;  

int Switch1_State = 0;
int Switch2_State = 0;
int Switch3_State = 0;
int nDelay = 100;
int loopLatch = 0;

void setup()
{
	pinMode(Switch1, INPUT);
	pinMode(Switch2, INPUT);
	pinMode(Switch3, INPUT);
	pinMode(Output1, OUTPUT);      // sets the digital pin as output
	pinMode(Output3, OUTPUT);      // sets the digital pin as output
	pinMode(Output2, OUTPUT);      // sets the digital pin as output
	Serial.begin(9600);
}

void loop()
{
	if (loopLatch == 0)
	{
		Switch1_State = digitalRead(Switch1);
		Switch2_State = digitalRead(Switch2);
		Switch3_State = digitalRead(Switch3);

		if (Switch3_State == HIGH)
		{
			delay(nDelay);
			if (Switch1_State == HIGH && Switch2_State == HIGH)
			{
				digitalWrite(Output1, HIGH); 
				digitalWrite(Output3, HIGH); 
				digitalWrite(Output2, HIGH); 
                                Serial.println(F("SWITCH1 OK")); 
                                Serial.println(F("SWITCH2 OK")); 
                                Serial.println(F("SWITCH3 OK")); 
                                Serial.println(F("---------------")); 
				loopLatch = 1;
				delay(nDelay);
			}	
		}
		else // Switch3_State = LOW
		{
			delay(nDelay);
			if (Switch1_State == HIGH)
			{
				digitalWrite(Output1, HIGH); 
				digitalWrite(Output3, HIGH);
				digitalWrite(Output2, HIGH); 
                                Serial.println(F("SWITCH1 OK")); 
                                Serial.println(F("SWITCH3 ALARM")); 
                                Serial.println(F("---------------")); 
				loopLatch = 1;
				delay(nDelay);
			}
		}
	}
	else //loopLatch = 1
	{
		Switch1_State = digitalRead(Switch1);
		Switch2_State = digitalRead(Switch2);
		Switch3_State = digitalRead(Switch3);
		if (Switch3_State == HIGH)
		{
			if  (Switch1_State == LOW)
			{
				digitalWrite(Output1, LOW);
				digitalWrite(Output3, LOW);
				digitalWrite(Output2, LOW); 
                                Serial.println(F("SWITCH1 ALARM")); 
                                Serial.println(F("SWITCH3 OK")); 
                                Serial.println(F("---------------")); 
                                 loopLatch = 0;
				delay(nDelay);
			}
			if  (Switch2_State == LOW)
			{
				digitalWrite(Output1, LOW);
				digitalWrite(Output3, LOW);
				digitalWrite(Output2, LOW); 
                                Serial.println(F("SWITCH2 ALARM")); 
                                Serial.println(F("SWITCH3 OK")); 
                                Serial.println(F("---------------")); 
                                loopLatch = 0;
				delay(nDelay);
			}
		}
		else  // Switch3_State = LOW
		{
			if  (Switch1_State == LOW)
			{
				digitalWrite(Output1, LOW);
				digitalWrite(Output3, LOW); 
				digitalWrite(Output2, LOW); 
                                Serial.println(F("SWITCH1 ALARM")); 
                                Serial.println(F("SWITCH3 ALARM")); 
                                Serial.println(F("---------------")); 
                                loopLatch = 0;
				delay(nDelay);	   
			}
		}
	} 
}

If you converted all those variables with numbers in their name into arrays, you would get rid of two thirds of the code and make it much more maintainable.

Here is a sample code that would Serial print if the switches are open or closed:

//use const in front of the variables that will not change
const int switchPin1 = 1;
const int switchPin2 = 2;

//these will change so we just use int
int switch1State;
int switch2State;

void setup() {
  Serial.begin(9600);     //begin Serial communication

  //set your pin modes to inputs since you're reading data
  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);
}

void loop() {
  //set the states of the pins equal to the read value of the pins
  switch1State = digitalRead(switchPin1);
  switch2State = digitalRead(switchPin2);
  
  /* a switch command assesses a variable and performs commands
  *  based on what it might be, we will use this function to
  *  tell the arduino what we want it to do when the switch states
  *  are certain values */

  switch(switchPin1) {
    case HIGH:
      Serial.println("Switch 1 is open");
      break;    //use break; after a case if you are going to have another one
    case LOW:
      Serial.println("Switch 1 is closed");
      //no break here since we will not have another case
  }
  
  switch(switchPin2) {
    case HIGH:
      Serial.println("Switch 2 is open");
      break;
    case LOW:
      Serial.println("Switch 2 is closed");
  }
}

The only problem with that code is that that code will constantly Serial print the data. If you only want to print the data when a state change occurs, then you need to use some different commands that measure the change in state and compare that to the current state. This is useful for Serial printing the values and making it not refresh all the time (making annoying to read). But, if you are firing relays my guess is you want them to run when a switch is open in which case you would use the above code and insert the code for your relays in the appropriate cases in the switch loops. This provides easy to read code and although it may not be the shortest, you don't have to use any complicated commands or loops.

P.S. If you're firing relays when a switch is open make sure to put code in to turn them off in the "case LOW:" cases.

The only problem with that code is that that code will constantly Serial print the data. If you only want to print the data when a state change occurs, then you need to

look at the state change detection example.

I think you have a significant error in your code.

It says
" switch(switchPin1) {...."
when I think it should be
"switch(switch1State) {...."

and the same for switchPin2

....R

AlphaProgrammer:
Here is a sample code that would Serial print if the switches are open or closed:

//use const in front of the variables that will not change

const int switchPin1 = 1;
const int switchPin2 = 2;

//these will change so we just use int
int switch1State;
int switch2State;

void setup() {
  Serial.begin(9600);     //begin Serial communication

//set your pin modes to inputs since you're reading data
  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);
}

void loop() {
  //set the states of the pins equal to the read value of the pins
  switch1State = digitalRead(switchPin1);
  switch2State = digitalRead(switchPin2);
 
  /* a switch command assesses a variable and performs commands
  *  based on what it might be, we will use this function to
  *  tell the arduino what we want it to do when the switch states
  *  are certain values */

switch(switchPin1) {
    case HIGH:
      Serial.println("Switch 1 is open");
      break;    //use break; after a case if you are going to have another one
    case LOW:
      Serial.println("Switch 1 is closed");
      //no break here since we will not have another case
  }
 
  switch(switchPin2) {
    case HIGH:
      Serial.println("Switch 2 is open");
      break;
    case LOW:
      Serial.println("Switch 2 is closed");
  }
}

Yes it should be switch1State. I thought I put that in there; just a typo a didn't catch.

And about the constantly Serial printing data, I didn't think it would be an issues since it didn't sound like that was the main goal. It came to that the Serial printing was just testing if he could read the switches in which case he would want to constantly return that the switches are open so that the relays are always running when the switches are open.

Thanks again for all of the replies. I will sort through all of this wonderful advice.

If you converted all those variables with numbers in their name into arrays, you would get rid of two thirds of the code and make it much more maintainable.

Could you give me an example of how I might use an array for this program?

What I am doing is monitoring 3 dry contacts. If all inputs are closed (inputs high) then all relays are energized (outputs high). If input 3 opens (goes low) I only want to monitor input 1 until input 3 is restored (goes high) then monitor all 3 inputs again. If any of the inputs open (goes low) while they are being monitored I want the relays to de-energize (outputs low). Sending a message to the serial port is really for troubleshooting more than anything right now.

Eventually I hope to write the status of the inputs to an sdram log file.