why might this not be working ?

I have a switch that keeps the pin HIGH when its switched on and LOW when its switched off.. As youd expect.

I want a function to be initiated once when the switch gets changed from off to on, but then after it has called the function i want it to immediately appear like the switch has been swicthed off even though its still sitting in the on position.. otherwise if it stays on it keeps initiating the SendCode function in my code and that function only needs to happen once like as if a button was pushed instead of a switch staying on..

here is what i have but ive been racking my brain why this isnt working..

oldswitchstate = 0;

switchstate = digitalRead(mainswitch);

if ((switchstate == HIGH) && (oldswitchstate == LOW)) {SendCode(24,0x33);
delay (100);
oldswitchstate = 1;
}

if ((switchstate == LOW) && (oldswitchstate == HIGH)) {SendCode(24,0x33);
delay (100);
oldswitchstate = 0;
}

To me that looks like it should detect the change in the switch and initiate the SendCode function once before changing oldswitchstate to the opposite value.. When i have this code uploaded to the arduino nothing happens.. been stumped on this for hours now.. Ive enjoyed trying to solve it but im starting to get exhausted because im out of ideas and i feel like im going round in circles..

oldswitchstate = 0; => oldswitchstate = LOW;
oldswitchstate = 1; => oldswitchstate = HIGH;

maybe this helps

furthermore in both cases you send the same code {24 33}

yeah the code is the same to start and stop a video camera.. i'll try your suggestion and see.. i feel like im playing bingo at the moment.

Without seeing the complete code, I might be wrong, but:

did you do something like:

void setup()
{
oldswitchstate = 0;
}
void loop()
{
//rest of the logic
}

?

Or is the "oldswitchstate = 0;" instruction in the loop? If so, it won't work as intended because the oldswitchstate will always be zero.

Also, like pointed out by robtillaart, to prevent logic errors, portability errors and to improve readibility of your code, you should stick with the values defined with tags instead of numbers.

oldswitchstate = 0; => oldswitchstate = LOW;
oldswitchstate = 1; => oldswitchstate = HIGH;

Hope it helps.

yeah i put the switchstate variable before void setup ().. nothing i do seems to work... this has really got me stumped..

this has really got me stumped.

Looks like it's time to post all of your code, then.

This is what i have.. im working off code someone else wrote to control a sony video camera via LANC which is the protocol that speaks to the camera.. The interfacing with the camera is working ok so i wont post all the function calls at the bottom of the code cause it will be a lot.. im just trying to get buttons and switches to behave certain ways before calling the function to send the code.. theres a few variables that get set at the start which might not look active in what i have posted but during all my attempts to get it working i have evolved through different ideas and some thing are left over.. Also some of the functions use them as well..

The switchstate section is the main part im trying to get working.. The other millis() stuff is working well..

Also im just starting out so it might be really badly written at the moment.. Once i had it going i was planning to clean it up and maybe optimise things into more cohesive methods, at the moment im just trying to find my way..

#define lancPin 2
#define aPin 0
#define bitMicroSeconds 104
#define button 3
#define buttonTwo 4
#define mainswitch 7

byte ledPinState = 1;

long previousMillis = 601;
long previousMillistwo = 601;
unsigned long currentMillis;
unsigned long currentMillistwo;
int switchstate;
int oldswitchstate = 0;

void setup() {

// define pin modes for tx, rx, led pins:
pinMode(lancPin, INPUT);
pinMode(aPin,INPUT);
pinMode(button,INPUT);
pinMode(buttonTwo,INPUT);

Serial.begin(300);

}

int commandCode = 0;

void loop() {

currentMillis = millis();
currentMillistwo = millis();

if (digitalRead(button) == HIGH) {SendCode(40,0x1a);
previousMillis = currentMillis;
}

if ((digitalRead(button) == LOW) && (currentMillis - previousMillis) < 200) {SendCode(40,0x18);}
if ((digitalRead(button) == LOW) && (currentMillis - previousMillis) > 200 && (currentMillis - previousMillis) < 400) {SendCode(40,0x16);}
//if ((digitalRead(button) == LOW) && (currentMillis - previousMillis) > 400 && (currentMillis - previousMillis) < 500) {SendCode(40,0x14);}
//if ((digitalRead(button) == LOW) && (currentMillis - previousMillis) > 500 && (currentMillis - previousMillis) < 600) {SendCode(40,0x12);}

if (digitalRead(buttonTwo) == HIGH) {SendCode(40,0x0a);
previousMillistwo = currentMillistwo;

}

if ((digitalRead(buttonTwo) == LOW) && (currentMillistwo - previousMillistwo) < 200) {SendCode(40,0x08);}
if ((digitalRead(buttonTwo) == LOW) && (currentMillistwo - previousMillistwo) > 200 && (currentMillistwo - previousMillistwo) < 400) {SendCode(40,0x06);}
//if ((digitalRead(buttonTwo) == LOW) && (currentMillistwo - previousMillistwo) > 400 && (currentMillistwo - previousMillistwo) < 500) {SendCode(40,0x04);}
//if ((digitalRead(buttonTwo) == LOW) && (currentMillistwo - previousMillistwo) > 500 && (currentMillistwo - previousMillistwo) < 600) {SendCode(40,0x02);}

switchstate = digitalRead(mainswitch);

if ((switchstate == HIGH) && (oldswitchstate == LOW)) {SendCode(24,0x33);

//delay (100);
oldswitchstate = HIGH;
}

if ((switchstate == LOW) && (oldswitchstate == HIGH)) {SendCode(24,0x33);

//delay (100);
oldswitchstate = LOW;
}

So with the switchstate stuff im trying to get a normal switch (when switched on) to send a code (start recording) to the camera as if it was pressed once on a button and then when it gets switched off, send the code again (to stop the camera recording)... I want to use a switch instead of a button so it doesnt get knocked on the control panel i plan to build... if i could turn the switch on once and start recording then when i want to stop i just flick it back, thats what im hoping..

problem i have at the moment is when i switch it on, i think it keeps sending the code to the camera over and over, it doesnt seem to want to send only one short instance and thats it.. At least this is what i think is happening due to a few other experiments..

Your not debouncing mainswitch.

Is the camera connected to the serial port?

Camera is connected to vin/ground and lancpin (digital 2) via a cable i made up that goes out to a small 1/8th plug that plugs into the camera..

How do i debounce the switch ? I tried the delay line but it didnt seem to do anything.. Sorry if my naivety is annoying..

EDIT: I'll search round for debounce info and see how i go.. Thanks for the help.

If the camera is NOT connected to the serial port on the Arduino, use the Serial Monitor, and Serial.print() statements to see when the switch state changes. See if that corresponds to when the button is pressed/released. Print the old and new switch states, and see if they change appropriately.