Go Down

Topic: Playground Switch tutorial not clear (Read 473 times) previous topic - next topic

mem

Aug 08, 2008, 07:58 pm Last Edit: Aug 08, 2008, 08:07 pm by mem Reason: 1
Mellis, I was going to point someone to the switch tutorial in the playground , but I found that tutorial really confusing.

The first problem is that the wiring diagram is confusing. Its unclear which pins the  wires go to on the arduino and it's not very clear what that resistor is connected to.

And it looks from the diagram like the pin would be normally high, pulled low when pressed but the text says the opposite happens.

Another issue may just be with choice of example. I think it would be difficult for the target audience to figure out how to do a non toggling version from the example code. A simple debounce of on and off would probably be used at least as much as a toggle.

bens

Also, the switch tutorial would be a great place to show people how to use a pin's internal pull-up resistor.  A button circuit for the AVR can be as simple as simply connecting a pin through a button to ground, but most tutorials I see make a big deal about using an external resistor in the circuit, which tends to complicate things and doubles the number of required components.

- Ben

mem

#2
Aug 08, 2008, 08:32 pm Last Edit: Aug 08, 2008, 08:33 pm by mem Reason: 1
Quote
Also, the switch tutorial would be a great place to show people how to use a pin's internal pull-up resistor.  A button circuit for the AVR can be as simple as simply connecting a pin through a button to ground, but most tutorials I see make a big deal about using an external resistor in the circuit, which tends to complicate things and doubles the number of required components.

- Ben
Yes, but  dealing with pull-ups  is one extra concept that would need  to be grasped by someone just starting out.  Having the physical resistor in a basic tutorial helps people new to electronics visualize what is happening. I suggest pull-ups are covered after the basic switch with resistor is explained.

Personman

How about a 2 or more part tutorial via links.  Progressive learning is easy when there are links on where to go next to expand on the current topic.

worapoht

#4
Aug 09, 2008, 09:06 pm Last Edit: Aug 09, 2008, 09:18 pm by worapoht Reason: 1
I'd currently use below code for switch tutorial, step-by-step coding and clear.

Code: [Select]

/*
 Toggle LED (13 active-high) with Push Button switch (2 active-low)
*/

int swPin = 2;
int ledPin = 13;
int ledState = 0; // intial state = 0

void setup()
{
 pinMode(ledPin, OUTPUT);
 pinMode(swPin, INPUT);
 digitalWrite(swPin, HIGH); // enable internal pull-up (Ext. R may removed)
}

void loop()
{
 if ((digitalRead(swPin))==LOW) // switch is pressed?
{
   if (ledState==0)  // toggle state and led acion
   {
     digitalWrite(ledPin,HIGH);
     ledState=1;
   }
   else
   {
     digitalWrite(ledPin,LOW);
     ledState=0;
   }

   while ((digitalRead(swPin))==LOW) // wait for switch depressed
   { delay(10); }
}
}


or another code without waiting

Code: [Select]

/*
 Toggle LED (13 active-high) with Push Button switch (2 active-low)
*/

int swPin = 10;
int ledPin = 13;
int ledState = 0; // intial state = 0
int swPressed = 0; // sw hold state =0
void setup()
{
 pinMode(ledPin, OUTPUT);
 pinMode(swPin, INPUT);
 digitalWrite(swPin, HIGH); // enable internal pull-up (Ext. R may removed)
}

void loop()
{
 if ((digitalRead(swPin))==LOW) // switch is pressed?
 {
   if (swPressed==0)
   {
     swPressed=1;
     delay(10);
     if (ledState==0)  // toggle state and led acion
     {
       digitalWrite(ledPin,HIGH);
       ledState=1;
     }
     else
     {
       digitalWrite(ledPin,LOW);
       ledState=0;
     }
   }
 }
 else
 {
   swPressed=0;
 }
}



ckiick

I'd suggest the ladyada Arduino tutorial Lesson 5. It is very basic, step by step and has clear schematics and diagrams.
http://www.ladyada.net/learn/arduino/lesson5.html

Would it be out of line to link to this from the playground?
Chris J. Kiick
Robot builder and all around geek.

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview