Newbie Questions (Soz lol)

hay guy's my name's Hector and i live in NSW,AUS
im new to arduino(as of yesterday) and have just started playing around with my breedborad and a hd44780 lcd and makeing a IR repeter:) was stoked coz if never touched any programeing or PIC's before and had it down in like 2 hours ,,,,

but here's the prob,im tryin to make a LED turn on for 1500ms then turn off with the press of a button witch i can do but i with to add a nother button to do the same thing IE
/*

  • test 2 button's //pulldowned via G/r
    */
    int switchPin1 = 2;
    int switchPin2 = 3; // switch is connected to pin 2
    int led1Pin = 13;

int val; // variable for reading the pin status
int val2; // variable for reading the delayed/debounced status
int buttonState1; // variable to hold the button state
int buttonState2;
int lightMode = 0; // Is the light on or off?

void setup() {
pinMode(switchPin1, INPUT); // Set the switch pin as input
pinMode(switchPin2, INPUT);

pinMode(led1Pin, OUTPUT);

Serial.begin(9600); // Set up serial communication at 9600bps
buttonState1 = digitalRead(switchPin1); // read the initial state
buttonState2 = digitalRead(switchPin2);
}

void loop(){
val = digitalRead(switchPin1); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(switchPin1); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState1) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (lightMode == 0) { // is the light off?
lightMode = 1; // turn light on!

digitalWrite(led1Pin, HIGH);

delay(1500);

digitalWrite(led1Pin, LOW);

delay(100);

} else {
lightMode = 0; // turn light off!
// No need to do anything because the light is 'off' when it finishes with mode 1
val = digitalRead(switchPin2); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(switchPin2); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState2) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (lightMode == 0) { // is the light off?
lightMode = 1; // turn light on!

digitalWrite(led1Pin, HIGH);

delay(1500);

digitalWrite(led1Pin, LOW);

delay(100);

} else {
lightMode = 0; // turn light off!
// No need to do anything because the light is 'off' when it finishes with mode 1

}
}
}
buttonState1 = val; // save the new state in our variable
buttonState2 = val;
}

}
}
}
}
}
but thats dose not work :frowning:
not sure why maybe some one can help?????

This could be a hardware problem or a software problem.

If it is a hardware problem, this code is way complex and confusing to determine if that is the case.

To determine if it IS a hardware problem, create a sketch that turns the LED on when the button is pressed, and off when it is released. Forget about the debouncing stuff for now.

If the light comes on when the button is pressed, and goes off when it is released, then you know the switch and LED are wired correctly.

You are not enabling the pull-up resistors on the switch pins, so, you must be using external pull-up or pull-down resistors, right?

but here's the prob,im tryin to make a LED turn on for 1500ms then turn off with the press of a button witch i can do but i with to add a nother button to do the same thing IE

but thats dose not work
not sure why maybe some one can help?????

We are supposed to look at the code, and guess what part works and what doesn't work, without having the hardware in front of us? I don't think that will result in very good answers.

Explain what does happen and what doesn't happen, and what you really want to have happen. With two switches, is either switch supposed to turn the LED on for 1.5 seconds, or is one supposed to turn it on and the other turn it off? If that's the case, what is the 1.5 second duration for?

Also, it looks more professional if you enable the spell checker in your browser (and actually correct the misspellings).

hay thnx for the reply

k yea im useing a 47ohm resister as pulldown that i got out of the starter pack the code i got from Arduino Tutorial - Lesson 5 ,i got the code to work with one push button then i just tryed to dubble it ,its hard to explane,, this is the start of the code UNtouched

int switchPin = 3; // switch is connected to pin 2
int led1Pin = 13;

int val; // variable for reading the pin status
int val2; // variable for reading the delayed/debounced status
int buttonState; // variable to hold the button state
int lightMode = 0; // Is the light on or off?

then i started editing it

int switchPin1 = 2;
int switchPin2 = 3; // switch is connected to pin 2
int led1Pin = 13;

int val; // variable for reading the pin status
int val2; // variable for reading the delayed/debounced status
int buttonState1; // variable to hold the button state
int buttonState2;
int lightMode = 0; // Is the light on or off?

and so on and so on,,,, just copying and pasteing and adding in 1 and 2 then i got to the LOOP and copyed that and added in (switchPin2) ,,
so yeah sorry about the spellcheck not my bigist strong point and always forgot to check it Lol + im yet to install the Ie addon

so maybe i just need to be pointed to some tut's for useing push button's ,,, what im planing is i've been useing the IR examples and have dumped my tv remote power,volup,down,chanup,down and have got my code to a point where when its turned on its will send the power command every 1.5 sec's but i wont to add 5 push button's and assign a ir command to each button ie, button1 on pin2 for power,button2 on pin3 for volup,ect.. geting rid of the 1.5 sec all to gether i got the library for A Multi-Protocol Infrared Remote Library for the Arduino :slight_smile: cheers

Look at this:-

There are also lots of other tutorials to play with.
By the way 47R is too low for a pull down, use something like 1K to 10K.

yep thanks just what i needed got 2 buttons working with that code :slight_smile:
ie
/*
Button

Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.

The circuit:

  • LED attached from pin 13 to ground

  • pushbutton attached to pin 2 from +5V

  • 10K resistor attached to pin 2 from ground

  • Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

created 2005
by DojoDave http://www.0j0.org
modified 28 Oct 2010
by Tom Igoe

This example code is in the public domain.

*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin1 = 3; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);

// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin1);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
}

then i got my ir code to work from that with one button to power on and off next i will use the dubble button code from above to put in more command's

*/
#include <IRremote.h>

IRsend irsend;

int inPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status

void setup() {

pinMode(inPin, INPUT); // declare pushbutton as input
Serial.begin(9600);
}

void loop(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
(Serial.read() != -1); {
for (int i = 0; i < 3; i++) {
irsend.sendNEC(0x2FD48B7, 32); // MY TV power code
}
}
}
}
:slight_smile: :slight_smile:

ok back again lol so my 2 button 1 LED code work's

/*
 Button

Turns on and off a light emitting diode(LED) connected to digital  
pin 13, when pressing a pushbutton attached to pin 2. 


The circuit:
* LED attached from pin 13 to ground 
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.


created 2005
by DojoDave <http://www.0j0.org>;
modified 28 Oct 2010
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int buttonPin1 = 3;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);      
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT); 
pinMode(buttonPin1, INPUT);    
}

void loop(){
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 if (buttonState == HIGH) {     
   // turn LED on:    
   digitalWrite(ledPin, HIGH);  
 } 
 else {
   // turn LED off:
   digitalWrite(ledPin, LOW); 
   
     // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin1);

 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 if (buttonState == HIGH) {     
   // turn LED on:    
   digitalWrite(ledPin, HIGH);  
 } 
 else {
   // turn LED off:
   digitalWrite(ledPin, LOW); 
 }
 }
}

but now i edited it for IR commands (just to turn the tv on with pushbutton)

/* Basic Digital Read
 * ------------------ 
 *
 * turns on and off a light emitting diode(LED) connected to digital  
 * pin 13, when pressing a pushbutton attached to pin 7. It illustrates the
 * concept of Active-Low, which consists in connecting buttons using a
 * 1K to 10K pull-up resistor.
 *
 * Created 1 December 2005
 * copyleft 2005 DojoDave <http://www.0j0.org>
 * http://arduino.berlios.de
 *
 */
#include <IRremote.h>

IRsend irsend;

int inPin = 2;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void setup() {

  pinMode(inPin, INPUT);    // declare pushbutton as input
  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
   (Serial.read() != -1); {
    for (int i = 0; i < 3; i++) {
      irsend.sendNEC(0x2FD48B7, 32); // Sony TV power code
      delay(1500);
    }
   }
  }
}

Witch work's yay :slight_smile: so then i tryed to dubble it and make two button IR (one for power ,one for chan-up

 */

#include <IRremote.h>

IRsend irsend;

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int buttonPin1 = 4;     // the number of the pushbutton pin
     // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
     Serial.begin(9600);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 
pinMode(buttonPin1, INPUT);    
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    (Serial.read() != -1); {
    for (int i = 0; i < 3; i++) {
      irsend.sendNEC(0x2FD48B7, 32); // MY TV power code

    
      // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin1);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    
     (Serial.read() != -1); {
    for (int i = 0; i < 3; i++) {
      irsend.sendNEC(0x2FDF807, 32); // MY tv Chan-up
    }
     }
  }
    }
    }
  }
}

Witch uploaded fine but only the First ir code get sent and when i press the 2'nd button nothing happen's ive checked it with my phone camera and nothing but the first button is working and turning my tv on and off :slight_smile:

Soz lol

Is this part of the question or some kind of Antipodean argot?
What does it mean, please?

soz == sorry

thank you for the information! I learned a lot of new things!


Cheap online cigars

yea bro im young....Slang.... like sorry for the noobish questions

But realy i have know idea what type of community im introducing myself into here ,but i usually dont find two many people hang up on my grammar and spelling ,like everywhere rotceh_dnih has been im only here to have fun whilst learning a new skill and like most who are new i'll ask some really Dumb question's then get my head around it and think why didnt i figer that out before lol...

so i guess i'll try to be a little more professional with my layout as not to provide any unwanted distraction like i may not be the smartest or punctual person but i don't need to be turkey slapped with it lol :sunglasses:

@rotceh_dnih

... some really Dumb question ...

No, questions are never dumb, not asking them may be dumb. :slight_smile:

Rob

but i usually dont find two many people hang up on my grammar

People may not, but the compiler surely will. ;D

Thank's rob
yea i've never really programed pic's before (the closest ive been to codeing is Glove-pie as a hid with wiimote's lol) but i feel in two day's of playing with my arduino that i have come so far but only scratched the serface ,i just with i could figer out how to use 2 push button's in the way i want... i thought it would be easy but hay im only new so i caint just think im going to make thing's work right away lol

for i am only a Young Grasshopper with many field's to pass,,, Rotfl

edit: yea the amount of times ive had to fix my code coz of a missing } or something lol ...but im getting there,it sure is pulling me up and makeing me think for a change lol

yea i've never really programed pic's before

Nor have most people here. I gave up on them years ago - RAM paging just too much hassle to keep a track of.

Don't forget, that like yourself, many people here don't have English as a first language, so a little effort in posing questions without txt or regional slang means that more people will be more inclined to read and help out.

ok so arduino isnt PIC??? .... lol i really dont know much

and yea thanks for the info ill try to be ezz up on the slang

i lol'd

Don't forget, that like yourself, many people here don't have English as a first language,

here in australia english sorta is our first language and it is mine also i just suck at wrighting lol

ok so as far as my script goes i've got it working with one stiwch and added in whats needed for the 2'nd im just not sure what i need to do to make it work in the LOOP

/* testing
 * ------------------ 
 *
 * 
 * 
 * 
 * 
 *
 * 
 * 
 * 
 *
 */
#include <IRremote.h>

IRsend irsend;

int inPin = 2; // choose the input pin (for a pushbutton)
int inpin1 = 4; // as pin 3 is for IRled with Pwm..
int val = 0;     // variable for reading the pin status

void setup() {

  pinMode(inPin, INPUT);    // declare pushbutton as input
  pinMode(inpin1, INPUT);
  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
   (Serial.read() != -1); {
    for (int i = 0; i < 1; i++) {
      irsend.sendNEC(0x2FDD827, 32); // My TV CHan-up code
      delay(40);
    }
   }
  }
}

irremote.h

/*
 * IRremote
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.htm http://arcfn.com
 *
 * Interrupt code based on NECIRrcv by Joe Knapp
 * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
 * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
 */

#ifndef IRremote_h
#define IRremote_h

#include "IRremoteInt.h"

// The following are compile-time library options.
// If you change them, recompile the library.
// If DEBUG is defined, a lot of debugging output will be printed during decoding.
// TEST must be defined for the IRtest unittests to work.  It will make some
// methods virtual, which will be slightly slower, which is why it is optional.
// #define DEBUG
// #define TEST

// Information on a generic space encoding code.
// This is a code that varies the mark width (or the space
// width) to distingish bits.
class space_enc_data {
public:
  int headerMark; // Mark time for header in us
  int headerSpace; // Space time for header in us
  int mark0; // Mark time in us for 0 bit
  int space0; // Space time in us for 0 bit
  int mark1; // Mark time in us for 1 bit
  int space1; // Space time in us for 1 bit
  int trailer; // Trailer mark time in us or 0
  int frequency; // Not used by IRrecv
};

// Results returned from the decoder

class decode_results {
public:
  int decode_type; // SPACE_ENC, RC5, RC6, NEC_REPEAT, UNKNOWN
  unsigned long long value; // Decoded value
  int bits; // Number of bits in decoded value
  volatile unsigned int *rawbuf; // Raw intervals in .5 us ticks
  int rawlen; // Number of records in rawbuf.
  class space_enc_data spaceEncData;
};

// Values for decode_type
#define NEC_REPEAT 1
#define RC5 3
#define RC6 4
#define SPACE_ENC 7 // Generic space encoding
#define UNKNOWN -1

// Decoded value for NEC when a repeat code is received
#define REPEAT 0xffffffff

// main class for receiving IR
class IRrecv
{
public:
  IRrecv(int recvpin);
  int decode(decode_results *results);
  void enableIRIn();
  void pause();
  void resume(); // deprecated
private:
  // These are called by decode
  int getRClevel(decode_results *results, int *offset, int *used, int t1);
  long decodeSpaceEnc(decode_results *results);
  long decodeNecRepeat(decode_results *results);
  long decodeRC5(decode_results *results);
  long decodeRC6(decode_results *results);
  long decodeHash(decode_results *results);
  int compare(unsigned int oldval, unsigned int newval);
  volatile irparams_t irparams; // Receive state information
} 
;

// Only used for testing; can remove virtual for shorter code
#ifdef TEST
#define VIRTUAL virtual
#else
#define VIRTUAL
#endif

class IRsend
{
public:
  IRsend() {}
  void sendSpaceEnc(unsigned long long data, int nbits, space_enc_data *spaceEncData);
  void sendNEC(unsigned long data, int nbits); // deprecated
  void sendSony(unsigned long data, int nbits); // deprecated
  void sendRaw(unsigned int buf[], int len, int hz);
  void sendRC5(unsigned long data, int nbits);
  void sendRC6(unsigned long data, int nbits);
  // private:
  void enableIROut(int khz);
  VIRTUAL void mark(int usec);
  VIRTUAL void space(int usec);
}
;

// Some useful constants

#define USECPERTICK 50  // microseconds per clock interrupt tick

// Marks tend to be 100us too long, and spaces 100us too short
// when received due to sensor lag.
#define MARK_EXCESS 100

#endif
(Serial.read() != -1); {

Is there supposed to be an "if" there?
If there is, there shouldn't be a ";".

im not sure if there was ment to be but i tryed it

if (Serial.read() != -1) {

and that stoped even the first code geting sent so put back in the ;

if (Serial.read() != -1); {

and it started working again but still not the 2'nd button so puting in "if " seemed to make no difference ,with or with out it the 2'nd ircommand still isnt being send :cry:

would a Picture of my breedboard/layout help???