Loading...
  Show Posts
Pages: [1]
1  Using Arduino / Project Guidance / Re: 4 pushbutton lock coding on: September 10, 2012, 12:10:42 pm
finally got it working!! Thanks all you guys!! I couldn't get the while loop to work but i managed something else... ignore the motor pins..

Code:
const int buttonPin1 = 1;
const int buttonPin4 = 4;
const int buttonPin3 = 3;
const int buttonPin2 = 2; // the pin that the pushbutton is attached to
const int correctLedPin = 7;       // the pin that the LED is attached to
const int wrongLedPin = 6;   
const int motorPin = 11;// the pin that the LED is attached to
// variables that are changed by the program
int buttonPushCounter = 0;   // counter for the number of button presses
int val = 0;
int bS1 = 0;         // current state of the button
int lBS1 = 0;     // previous state of the button


int bS2 = 0;         // current state of the button
int lBS2 = 0;

int bS3 = 0;         // current state of the button
int lBS3 = 0;

int bS4 = 0;         // current state of the button
int lBS4 = 0;
// configure the pins to the right mode
void setup() {
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(correctLedPin, OUTPUT);
  pinMode(wrongLedPin, OUTPUT);
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
}

// the main loop will constantly check to see if the button has been pressed
// if it has, a counter is incremented, and then some action can be taken
void loop() {

  // read the state of the button
  static int buttonPushCounter = 0;
  bS1 = digitalRead(buttonPin1);
  bS2 = digitalRead(buttonPin2);
  bS3 = digitalRead(buttonPin3);
  bS4 = digitalRead(buttonPin4);

  // check to see if it different than the last time we checked
  if (bS1 != lBS1) {
    // either the button was just pressed or just released
    if (bS1 == HIGH) {
      // it was just pressed
      buttonPushCounter = buttonPushCounter + 1;
      Serial.println(buttonPushCounter);
      digitalWrite(correctLedPin, HIGH);
      delay(100);
      digitalWrite(correctLedPin, LOW);
      if(val == 0)
      {
        val = 1;
      }
    }
  }
  lBS1 = bS1;


if (bS2 != lBS2) {
    // either the button was just pressed or just released
    if (bS2 == HIGH) {
      // it was just pressed
      buttonPushCounter = buttonPushCounter + 1;
      Serial.println(buttonPushCounter);
      digitalWrite(correctLedPin, HIGH);
      delay(100);
      digitalWrite(correctLedPin, LOW);
      if(val == 1)
      {
        val = 2;
      }
    }
  }
  lBS2 = bS2;


if (bS3 != lBS3) {
    // either the button was just pressed or just released
    if (bS3 == HIGH) {
      // it was just pressed
      buttonPushCounter = buttonPushCounter + 1;
      Serial.println(buttonPushCounter);
      digitalWrite(correctLedPin, HIGH);
      delay(100);
      digitalWrite(correctLedPin, LOW);
      if(val == 2)
      {
        val = 3;
      }
    }
  }
  lBS3 = bS3;


if (bS4 != lBS4) {
    // either the button was just pressed or just released
    if (bS4 == HIGH) {
      // it was just pressed
      buttonPushCounter = buttonPushCounter + 1;
      Serial.println(buttonPushCounter);
      digitalWrite(correctLedPin, HIGH);
      delay(100);
      digitalWrite(correctLedPin, LOW);
      if(val == 3)
      {
        val = 4;
      }
    }
  }
  lBS4 = bS4;

if(val == 4 && buttonPushCounter == 4)
{
  digitalWrite(correctLedPin, HIGH);
  val = 0;
  buttonPushCounter = 0;
  analogWrite(motorPin
}

else if(val != 4 && buttonPushCounter == 4)
{
  digitalWrite(wrongLedPin, HIGH);
  delay(3000);
  digitalWrite(wrongLedPin, LOW);
  val = 0;
    buttonPushCounter = 0;
}


}
2  Using Arduino / Project Guidance / Re: 4 pushbutton lock coding on: September 09, 2012, 08:15:17 am
thanks cawhiteley... and grumpy_mike i do have external resistors and the buttons work just fine independently.. sorry but could please tell me the exact coding part of whats wrong and the right code.. coz i don't understand what you mean when you're saying im reading the button and not the state..
3  Using Arduino / Project Guidance / Re: 4 pushbutton lock coding on: September 09, 2012, 06:18:43 am
okay.. My next attempt.. I did put a serial print for button presses on pressing the first button, but it never seems to go into that if loop.. and the order of buttons is determined by the 'val' variable..

Code:

int correctLedPin = 7;
int wrongLedPin = 6;
int b1 = 1; //the four buttons
int b2 = 2;
int b3 = 3;
int b4 = 4;

void setup()
{
  pinMode(correctLedPin, OUTPUT);
  pinMode(wrongLedPin, OUTPUT);
  pinMode(b1, INPUT);
  pinMode(b2, INPUT);
  pinMode(b3, INPUT);
  pinMode(b4, INPUT);
  Serial.begin(9600);
}

void loop()
{
  int bs1 = digitalRead(b1);
  int bs2 = digitalRead(b2);
  int bs3 = digitalRead(b3);
  int bs4 = digitalRead(b4);
  int val = 0;
  int buttonPresses;
  int lastButtonState = LOW;
 
if(bs1 == LOW && val == 0)
{
  delay(200)
  if(bs1 != lastButtonState)
  {
  digitalWrite(correctLedPin, HIGH);
  delay(100);
  digitalWrite(correctLedPin, LOW);
  val = 1;
  buttonPresses++;
  Serial.println(buttonPresses);
  delay(1000);
  }

  else
  {
    delay(200);
  }



else if(bs2 == LOW && val == 1)
  {
    delay(200);
    if(lastButtonState != bs2)
  {
    digitalWrite(correctLedPin, HIGH);
    delay(100);
    digitalWrite(correctLedPin, LOW);
    val = 2;
  buttonPresses++;
  }
  else
  {
    delay(200);
  }
  }
 

   
else if(bs3 == LOW && val == 2)
    {
delay(200)
      if(lastButtonState != bs3)
  {
      digitalWrite(correctLedPin, HIGH);
      delay(100);
      digitalWrite(correctLedPin, LOW);
      val = 3;
      buttonPresses++;
      delay(1000);
    }
    }


     
     else if(bs4 == LOW && val == 3)
      {
        if(lastButtonState == bs4)
  {
        val = 4;
        buttonPresses++;
   }
      }
   
   else
{
  val = 0;
  buttonPresses = 0;
  digitalWrite(wrongLedPin, HIGH);
}

if(val == 4 && buttonPresses == 4)
{
  digitalWrite(correctLedPin, HIGH);
        digitalWrite(wrongLedPin, LOW);
}

}

4  Using Arduino / Project Guidance / Re: 4 pushbutton lock coding on: September 09, 2012, 02:31:11 am
I tried incorporating all of that but it still doesn't seem to work... Please Help!!   

Code:
int correctLedPin = 7;
int wrongLedPin = 6;
int b1 = 1; //the four buttons
int b2 = 2;
int b3 = 3;
int b4 = 4;

void setup()
{
  pinMode(correctLedPin, OUTPUT);
  pinMode(wrongLedPin, OUTPUT);
  pinMode(b1, INPUT);
  pinMode(b2, INPUT);
  pinMode(b3, INPUT);
  pinMode(b4, INPUT);
  Serial.begin(9600);
}

void loop()
{
  int bs1 = digitalRead(b1);
  int bs2 = digitalRead(b2);
  int bs3 = digitalRead(b3);
  int bs4 = digitalRead(b4);
  int val;
  int buttonPresses;
 
if(bs1 == LOW && val == 0)
{
  digitalWrite(correctLedPin, HIGH);
  delay(100);
  digitalWrite(correctLedPin, LOW);
  val = 1;
  buttonPresses++;
}

if(bs2 == LOW && val == 1)
  {
    digitalWrite(correctLedPin, HIGH);
    delay(100);
    digitalWrite(correctLedPin, LOW);
    val = 2;
  buttonPresses++;
   
  }
 

 
   
 if(bs3 == LOW && val == 2)
    {
      digitalWrite(correctLedPin, HIGH);
      delay(100);
      digitalWrite(correctLedPin, LOW);
      val = 3;
      buttonPresses++;
    }
     
      if(bs4 == LOW && val == 3)
      {
       
        val = 4;
        buttonPresses++;
   }
   
     else
{
  digitalWrite(wrongLedPin, HIGH);
  delay(1000);
  digitalWrite(wrongLedPin, LOW);
}
 



if(val == 4 && buttonPresses == 4)
{
  digitalWrite(correctLedPin, HIGH);
        digitalWrite(wrongLedPin, LOW);
}

}


5  Using Arduino / Project Guidance / Re: 4 pushbutton lock coding on: September 08, 2012, 12:38:40 pm
Thanks. Will do..
6  Using Arduino / Project Guidance / 4 pushbutton lock coding [SOLVED] on: September 08, 2012, 11:35:56 am
Hey guys. I'm still a novice to arduino and I want to make a 4 pushbutton lock as my first project. All the buttons seem to be working all right independently, but after the first pushbutton is pressed the other pushbuttons don't cause the correctLedPin (refer to the program) to light up for one tenth of a second. There must be something wrong with the 'if' loop but I'm not able to figure out what it is.
Code:
int correctLedPin = 7;
int wrongLedPin = 6;
int b1 = 1; //the four buttons
int b2 = 2;
int b3 = 3;
int b4 = 4;

void setup()
{
  pinMode(correctLedPin, OUTPUT);
  pinMode(wrongLedPin, OUTPUT);
  pinMode(b1, INPUT);
  pinMode(b2, INPUT);
  pinMode(b3, INPUT);
  pinMode(b4, INPUT);
}

void loop()
{
  int bs1 = digitalRead(b1);
  int bs2 = digitalRead(b2);
  int bs3 = digitalRead(b3);
  int bs4 = digitalRead(b4);
if(bs1 == LOW)
{
  digitalWrite(correctLedPin, HIGH);
  delay(100);
  digitalWrite(correctLedPin, LOW);

  if(bs2 == LOW)
  {
    digitalWrite(correctLedPin, HIGH);
    delay(100);
    digitalWrite(correctLedPin, LOW);
    
    if(bs3 == LOW)
    {
      digitalWrite(correctLedPin, HIGH);
      delay(100);
      digitalWrite(correctLedPin, LOW);
      
      if(bs4 == LOW)
      {
        digitalWrite(correctLedPin, HIGH);
        digitalWrite(wrongLedPin, LOW);
      }
    }
  }
}

else
{
  digitalWrite(wrongLedPin, HIGH);
}


Pages: [1]