Momentary Switch Code Help

Hi,

I'm pretty new to the Arduino (9 days!). I'm having fun learning the code to use in my project.
(Automotive Display with cooling fan control and shift lights).

One aspect of the project is to add buttons, in my case I wanted to use a momentary button as a toggle switch to turn the cooling fan on/off. I want it so one press would turn the fan on and the next would turn it off. I cannot stop or delay the loop, and I want to be able to reuse the code for other buttons. So with that in mind I have tried to make a function that I can call and pass through some variables.

The code I am using is below. Its a modified version of zoomkat's code. I'm just having trouble trying to work out how to make the function fully independent so I can use it with multiple buttons. Serial.Println is just there for testing.

Any help would be greatly appreciated!

Thank you
Adi

//zoomkat LED button toggle test 11-08-2012
//---------------------------Momentary Button 1 Setup--------------------------
#define INPINEX 2
#define OUTPINEX 13
boolean toggle = false;
int press = 0;
//---------------------------------------------------------------------------
void setup()
{
  Serial.begin(9600);
  pinMode(OUTPINEX, OUTPUT);                                    //LED on pin 13
  pinMode(INPINEX, INPUT);                                      //arduino monitor pin state
}

void loop() {
  button(INPINEX, OUTPINEX);
  }

void button(int INPIN, int OUTPIN) {
  press = digitalRead(INPIN);
  if (press == HIGH)
  {
    if (toggle)
    {
      digitalWrite(OUTPIN, LOW);                             // set the LED on
      toggle = !toggle;
      Serial.println("Radiator Cooling FAN OFF");
    }
    else
    {
      digitalWrite(OUTPIN, HIGH);                            // set the LED off
      toggle = !toggle;
      Serial.println("Radiator Cooling FAN ON");
    }
    delay(140);                                               //delay for debounce, adjust as needed
  }
}

It doesn't make a lot of sense to have input arguments that define pins, and then use a single global variable to keep track of the state of the output pin.

It doesn't make sense to not write the state of the pin to the pin.

If your switch bounces for 100 milliseconds, throw it away, and spend a nickel for a better switch.

Is this a class project?

I cannot stop or delay the loop

delay(140); // why then are you using delay?

"toggle"
Could you use a better name?

I totally agree! I just don't understand to get around it!
As I said in the first post, I am new at this and I am using others scripts and "trying" to modify them to the best of my ability.

The delay is only on there for testing, Once I get a button to use I will obviously adjust it!

Is this a class project?

What does this do?

unsigned long lastMillis = millis();
void setup()
{
. . .
}

void loop()
{
if(millis() - lastMillis >= 100)
{
button(INPINEX, OUTPINEX);
lastMillis = lastMillis + 100;
}
. . .

Class Project, No.

This is just something I decided to try and build for my car, when I have time.
My day job is a Application Packager (InstallShield).

Funny you mention lastmillis because in my main sketch for the project, I'm using it! The sketch I posted is just a button test, not the final way it would be implemented. My fault for saying no delay (I can get around that, no issue).

The main issue I have is that I need to track the status of the button between loops and calls for each button.

Thanks
Adi

Show your "main sketch"?

The main issue I have is that I need to track the status of the button between loops and calls for each button.

byte read = digitalRead(fanSwitch);
if (read != fanSwitchsState)
{
fanSwitchState = read;
. . .
}
read = digitalRead(windowSwitch);
if (read != windowSwitchState)
{
windowSwitchState = read;
. . .
}

Would you answer reply #4s example?

.

LarryD:
Is this a class project?

What does this do?

unsigned long lastMillis = millis();
void setup()
{
. . .
}

void loop()
{
if(millis() - lastMillis >= 100)
{
button(INPINEX, OUTPINEX);
lastMillis = lastMillis + 100;
}
. . .

From what I understand it uses millis (Counter in MS since the system was started).
Which then gives you the ability to run the code within the brackets when the condition is true.
aka run this code every 100ms.

run this code every 100ms.

Very good.
You could use this to check your switches every 100mS or so.
This could be used as a very simple de-bounce method in noise free environments (along with #6).
Using the technique keeps your code non blocking.

Show your main sketch.

I will when its finished! :slight_smile:

For the moment it would confuse everyone as it has no bearing on what I am trying to achieve with the buttons.

I like to work on one issue at a time, get it working and then add it into the main sketch.(which has worked so far.