Loading...
Pages: [1]   Go Down
Author Topic: arduino code help  (Read 138 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im a beginner with arduino and i was wandering if my code looks correct. i am trying to turn on leds by means of buttons.

const int a = 3;
const int b = 4;
const int c = 5;     // the number of the pushbutton pin
const int ledpina =  10;
const int ledpinb =  11;
const int ledpinc =  12;

// variables will change:
int buttonStatea = 0;
int buttonStateb = 0;// variable for reading the pushbutton status
int buttonStatec = 0;
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledpina, OUTPUT);       
  pinMode(ledpinb, OUTPUT);
  pinMode(ledpinc, OUTPUT);     
  pinMode(a, INPUT);       
  pinMode(b, INPUT);       
  pinMode(c, INPUT);       
 
 
 
}

void loop(){
  // read the state of the pushbutton value:
  buttonStatea = digitalRead(a);
  buttonStateb = digitalRead(b);
  buttonStatec = digitalRead(c);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (a == HIGH) {     
    // turn LED on:   
    digitalWrite(ledpina, HIGH);
   
  }
  if (b == HIGH) {     
    // turn LED on:   
    digitalWrite(ledpinb, HIGH);
   
  }
  if (c == HIGH) {     
    // turn LED on:   
    digitalWrite(ledpinc, HIGH);
   
  }
   if (a == LOW) {
    // turn LED off:
    digitalWrite(ledpina, LOW);
   
  }
  if (b == LOW) {
    digitalWrite(ledpinb, LOW);
  }   
  if (c == LOW) {
    digitalWrite(ledpinc, LOW);
  }

   
Logged

Queens, New York
Online Online
Edison Member
*
Karma: 27
Posts: 1539
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It would work if you changed (a==HIGH) to (buttonStatea == HIGH) an everything else too.
Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Poole, Dorset, UK
Offline Offline
God Member
*****
Karma: 8
Posts: 669
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Buttons need to be de-bounced. See the examples and the playground.

Mark
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 311
Posts: 35470
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Switches also need to be wired correctly. How are yours wired?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you it worked i just had to change (a == HIGH) to (buttonStatea == High)
Logged

Pages: [1]   Go Up
Print
 
Jump to: