Results 1 to 10 of 17
Thread: Turning switches off
-
10-11-2013, 02:31 PM #1
- Join Date
- Sep 2013
- Location
- Cornwall, UK
- Posts
- 42
Turning switches off
When I turn off a switch using the simconnect codes will it automatically turn off the switch in the VC? i.e. if I had a toggle switch set to send the code for landing lights on when I turn it off will it turn the landing lights off or would I need to add code to send a separate command?
-
10-11-2013, 05:58 PM #2
- Join Date
- Apr 2011
- Location
- United States of America
- Posts
- 233
Re: Turning switches off
You'll need to send a command for the OFF state of the switch.
-
10-17-2013, 03:30 PM #3
- Join Date
- Sep 2013
- Location
- Cornwall, UK
- Posts
- 42
Re: Turning switches off
How do I detect when it is off?
-
10-17-2013, 09:21 PM #4
- Join Date
- Apr 2011
- Location
- United States of America
- Posts
- 233
Re: Turning switches off
I use I/O pin 13 as an output for quick diagnostics since it has the on-board LED indicator.
Summary:
1.) PinMode13 as an output
2.) PinMode another as your input
3.) digitalRead(InputPin)
4.) if (InputPin == HIGH) then digitalWrite(Output13 , HIGH) else digitalWrite(Output13 , LOW)
5.) After confirming proper operation, replace digitalWrite command with the serialPrint commands
The above is a generic process since I'm unsure of your skill level.
If you have further questions, please post your code so that we can walk through it together with a clear and concise direction.
JM
-
10-18-2013, 03:25 AM #5
- Join Date
- Sep 2013
- Location
- Cornwall, UK
- Posts
- 42
Re: Turning switches off
I'm pretty new to Arduino but that was very helpful. So this would be placed in the main loop, am I correct?
-
10-18-2013, 07:59 AM #6
- Join Date
- Apr 2011
- Location
- United States of America
- Posts
- 233
Re: Turning switches off
Yes. Place it the main loop. Also, you'll want to create a string called InputPinold, or something similar, and use it as such:
if (InputPin != InputPinold){
if (InputPin == HIGH) then digitalWrite(Output13 , HIGH) else digitalWrite(Output13 , LOW)
InputPinold = InputPin
}
The above will prevent the execution of the digitalWrite command at every pass of the loop when it reads the state of the input pin. Without it and using a command to toggle the gear up or down, the arduino will send a command for the gear- even though the switch did not physically change states. This would cause the gear to up on the first pass, down on the second, up on the third, etc.
-
10-19-2013, 02:03 PM #7
- Join Date
- Sep 2013
- Location
- Cornwall, UK
- Posts
- 42
Re: Turning switches off
So I tried this an I had it working with the LED. However I changed it to serial print C01 and C02 for the gear and it filled my serial viewer with C01 - any help?
int val = 0;int inputPin = 2;
int valOld = 0;
void setup()
{
pinMode(inputPin, INPUT);
Serial.begin(9600);
digitalWrite(inputPin, HIGH);
}
void loop() {
val = digitalRead(inputPin);
if (val != valOld); {
if (inputPin == HIGH) { Serial.println("C02"); } else { Serial.println("C01"); };
val == valOld;
}
}
-
10-19-2013, 03:14 PM #8
- Join Date
- Apr 2011
- Location
- United States of America
- Posts
- 233
Re: Turning switches off
inputPin always will equal 2.
ReWrite - if (val == HIGH)
if (val == HIGH) { Serial.println("C02"); } else { Serial.println("C01"); };
-
10-19-2013, 06:47 PM #9
- Join Date
- Sep 2013
- Location
- Cornwall, UK
- Posts
- 42
Re: Turning switches off
You are brilliant! Thank you.
Just one more question: The above code (with the fix that you suggested) is sending the line 'C02'/'C01' repeatedly whereas I thought the assignment of varOld would stop this from happening? Have I done something wrong?
-
10-19-2013, 06:58 PM #10
- Join Date
- Apr 2011
- Location
- United States of America
- Posts
- 233
Re: Turning switches off
Sorry. My mistake from post #6.
edited post #6 to read:
InputPinold = InputPin
JM
На нашем социальном сайте На Скамейке вы найдёте,...
Приглашение на...