Author Topic: Arduino Coding Pushbuttons  (Read 1265 times)

0 Members and 1 Guest are viewing this topic.

Offline XyverTopic starter

  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 0
Arduino Coding Pushbuttons
« on: July 19, 2011, 06:41:10 PM »
Ok, so I've got my prototype made, and I'm working on the code. What I'm making is a pair of gloves with RGB LEDs on the fingers, a button on each knuckle, and a main button.  The button switches what lights are on, controlling all the fingers at once (3 LEDS in the prototype.)  Right now, I have the main button working going through 7 modes.  (It counts the button pushes, one 1-7 it cycles through colors, on 8 it reverts back to 1).  What I want to do is make a 8th mode, where it allows each finger to be controlled individually.  


Code: [Select]
/* Techno gloves!  Here is some code for techno gloves with individual
finger control.  Written in July 2011 by Bryan Hellard */


const int button1 = 10;
const int button2 = 6;
const int button3 = 2;
const int buttonm = A1;
const int red1 = 13;
const int green1 = 12;
const int blue1 = 11;
const int red2 = 9;
const int green2 = 8;
const int blue2 = 7;
const int red3 = 5;
const int green3 = 4;
const int blue3 = 3;

// Variables will change:
int buttonPushCounterm = 0;
int buttonStatem = 0;
int lastButtonStatem = 0;
int buttonPushCounter1 = 0;
int buttonState1 = 0;
int lastButtonState1 = 0;
int buttonPushCounter2 = 0;
int buttonState2 = 0;
int lastButtonState2 = 0;
int buttonPushCounter3 = 0;
int buttonState3 = 0;
int lastButtonState3 = 0;

void setup() {
  // initialize the button pin as a input:
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(buttonm, INPUT);
  pinMode(blue1, OUTPUT);
  pinMode(green1, OUTPUT);
  pinMode(red1, OUTPUT);
  pinMode(blue2, OUTPUT);
  pinMode(green2, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(blue3, OUTPUT);
  pinMode(green3, OUTPUT);
  pinMode(red3, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}

void allblue(){
  digitalWrite(blue1, HIGH);
  digitalWrite(blue2, HIGH);
  digitalWrite(blue3, HIGH);}

void allblueoff(){
  digitalWrite(blue1, LOW);
  digitalWrite(blue2, LOW);
  digitalWrite(blue3, LOW);}  
  
void allred(){
  digitalWrite(red1, HIGH);
  digitalWrite(red2, HIGH);
  digitalWrite(red3, HIGH);}

void allredoff(){
  digitalWrite(red1, LOW);
  digitalWrite(red2, LOW);
  digitalWrite(red3, LOW);}

void allgreen(){
  digitalWrite(green1, HIGH);
  digitalWrite(green2, HIGH);
  digitalWrite(green3, HIGH);}

void allgreenoff(){
  digitalWrite(green1, LOW);
  digitalWrite(green2, LOW);
  digitalWrite(green3, LOW);}  

void loop() {
  // read the pushbutton input pin:
 buttonStatem = digitalRead(buttonm);

  // compare the buttonState to its previous state
  if (buttonStatem != lastButtonStatem) {
    // if the state has changed, increment the counter
    if (buttonStatem == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounterm++;
      if (buttonPushCounterm == 9) {
        buttonPushCounterm = 1;}
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounterm, DEC);
    }
    else {
      // if the current state is LOW then the button
      // went from on to off:
      Serial.println("off");
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonStatem = buttonStatem;

  if (buttonPushCounterm == 1) {
     allgreen();
     allredoff();}
  
  if (buttonPushCounterm == 2) {
    allgreen();
    delay(6);
    allgreenoff();
    delay(1);
    allblue();
    delay(6);
    allblueoff();
    delay(1);}
    
  if (buttonPushCounterm == 3){
    allblue();
    allgreenoff();}
    
  if (buttonPushCounterm == 4){
    allblue();
    delay(6);
    allblueoff();
    delay(1);
    allred();
    delay(6);
    allredoff();
    delay(1);}
    
  if (buttonPushCounterm == 5){
    allblueoff();
    allred();}
  
  if (buttonPushCounterm == 6){
    allgreen();
    delay(6);
    allgreenoff();
    delay(1);
    allred();
    delay(6);
    allredoff();
    delay(1);}  
    
  if (buttonPushCounterm == 7){
    allgreen();
    delay(8);
    allgreenoff();
    allblue();
    delay(8);
    allblueoff();
    allred();
    delay(8);
    allredoff();}  
    
  if (buttonPushCounterm == 8 ){
    buttonState3 = digitalRead(button3);

  // compare the buttonState to its previous state
  if (buttonState3 != lastButtonState3) {
    // if the state has changed, increment the counter
    if (buttonStatem == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter3++;
      if (buttonPushCounter3 == 4) {
        buttonPushCounter3 = 1;}
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter3, DEC);
    }}
    else {
      // if the current state is LOW then the button
      // went from on to off:
      Serial.println("off");}}
      
      if (buttonPushCounter3 == 0){
        allblue();}
        
        if (buttonPushCounter3 == 1){
        digitalWrite(green1, LOW);
        digitalWrite(red1, HIGH);}
        
      if (buttonPushCounter3 == 2){
        digitalWrite(red1, LOW);
        digitalWrite(blue1, HIGH);}
        
      if (buttonPushCounter3 == 3){
        digitalWrite(blue1, LOW);
        digitalWrite(green1, HIGH);}
    
  
    
}

It all works, right up until "if (buttonPushCounterm == 8 ){" After that, I'm lost.  How can I make it read the other 3 buttons, and change each light individually?  Once the main button is hit again, it will continue with the patterns, until it reaches the 8th button press again.
« Last Edit: July 20, 2011, 02:19:37 AM by Xyver »

 


Get Your Ad Here