How to write 3pin toggle switch program ?

Hello,

I would like to integrate a 3 PIN toggle switch on arduino just like this

How to connect toggle switch.

But in sketch, how would I come to know whether lever has been moved to left or right side ?

What code I need to write ? WHat I want is that if I move it to left, it should store value in a variable as

pos = left, else pos = right

I am newbie in the field of electronics, just started playing around arduino from last month.

Thanks

First you'd have to hook the other side of the switch to another pin. Then, you'll know which way it was switched by which pin was high and which was low.

Hello Delta,

The middle one is a red power pin. Do you mean I need to connect empty right pin the way in which left pin has been attached ? (To the another digital pin at arduino for example 4)

unixbox911:
Hello Delta,

The middle one is a red power pin. Do you mean I need to connect empty right pin the way in which left pin has been attached ? (To the another digital pin at arduino for example 4)

Yes

Yes, if you want a signal from the other side of the switch. Do you understand how that switch works? Either way you switch it causes one pin or the other to be connected to the center pin.

You also need pull down resistors or you leave one pin floating. Easier would be to hook the middle to ground instead of 5V and use the internal pull up resistors. Do a little searching about Arduino and floating pins and pull up resistors for a more detailed explanation.

Hey guys i am new at arduino and i dont understand a single mistake i made because i am just coping a program and circuit to try to make it work so i wrote this code. can any one helpe me please its a simple space ship interface starter kit project!

int switchstate = 0;

void setup() [
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
]
void loop () [
switchState = digitalRead(2);
// this is a comment
If (switchState== LOW) (
// the button is not pressed
digitalWrite (3, HIGH); // green LED
digitalWrite (4, LOW); // red LED
digitalWrite (5, LOW); // red LED
]
else [ // the button is pressed
digitalWrite (3, LOW);
digitalWrite (4, LOW);
digitalWrite (5, HIGH);
]
delay(250): //wait for a quarter second
//toggle the LEDs
digitalWrite (4, HIGH);
digitalWrite (5, LOW);
delay (250): // wait for a quarter second
]
] // go back to the beginning of the loop

and i am geting these errors message>

sketch_oct01a:4: error: array bound is not an integer constant
sketch_oct01a:4: error: expected `]' before ';' token
sketch_oct01a:5: error: expected constructor, destructor, or type conversion before '(' token
sketch_oct01a:6: error: expected constructor, destructor, or type conversion before '(' token
sketch_oct01a:7: error: expected constructor, destructor, or type conversion before '(' token
sketch_oct01a:8: error: expected unqualified-id before ']' token
sketch_oct01a:12: error: expected constructor, destructor, or type conversion before '(' token
sketch_oct01a:15: error: expected constructor, destructor, or type conversion before '(' token
sketch_oct01a:16: error: expected constructor, destructor, or type conversion before '(' token
sketch_oct01a:17: error: expected unqualified-id before ']' token
sketch_oct01a:20: error: expected constructor, destructor, or type conversion before '(' token
sketch_oct01a:21: error: expected constructor, destructor, or type conversion before '(' token
sketch_oct01a:22: error: expected unqualified-id before ']' token
sketch_oct01a:26: error: expected constructor, destructor, or type conversion before '(' token
sketch_oct01a:27: error: expected constructor, destructor, or type conversion before '(' token

You didn't copy correctly. You seem to have confused square brackets [ and ] with curly braces { and }.

Hello,

I wrote following code but I keep on getting value 0

#include <SoftwareSerial.h>
SoftwareSerial ss(2,3);

void setup() {
  Serial.begin( 9600 );  
  Serial.println( F("Started") );  
  ss.begin( 9600 );
}

void loop() {
  Serial.println( ss.available() );
  delay(1000);
}

Yes, that should keep giving you 0 until you send something on that serial line. What's connected to the other end?

I would strongly suggest a day or two of reading in the reference before writing much more code. It's not enough to "think you know" what something means. Programming is an exacting things and sometimes things don't work how you would expect until you read the documentation and learn how it really works.

Delta_G:
Yes, that should keep giving you 0 until you send something on that serial line. What's connected to the other end?

I would strongly suggest a day or two of reading in the reference before writing much more code. It's not enough to "think you know" what something means. Programming is an exacting things and sometimes things don't work how you would expect until you read the documentation and learn how it really works.

Agreed 100%, but where can I find relevant documentation ?

@unibox911:
Could we concentrate on ONE challenge at a time?
Your headline and first post is dealing with a switch (with 2 positions) to be read.
Your second last post is totally different from that headline - thus you will be irritating your readers and potential supporters.

@alijaf:
What has your post to do with this thread?
Pls open your own thread to get a concentrated answer on your specific issue and pls follow @Delta_G's advice to use code tags. (Read the forum rules - link is included in my signature).

Addit:
@unibox911:
What do you want to learn first? Then we can point you to the relevant information source.
My suggestion:
Study the examples in the IDE, beginning with the simplest ones first and you will eventually understand how Arduino sketches work. If possible, play with the different parameters, variables to see the effects.

unixbox911:
Agreed 100%, but where can I find relevant documentation ?

Really? Try this site. See at the top where it says "Learning". Try Google. Search for topics you don't know yet.

If you really can't find reference material for Arduino without help then programming is probably not for you.