Go Down

Topic: help with mode select button code (Read 1 time) previous topic - next topic

bloodlust5563

im trying to make a preset ammo counter that has three preset numbers to count back from till empty. im gonna have 2 buttons, one to select the mode and one to act as a switch for the counter trigger, and 3 leds to indicate what mode is active. ive tried doing just about every possible code from scratch and combinations of codes that ive found and tried to modify to my needs. this is what i have now. what it does now is get stuck on the mode select without advancing to display the mode its supposed to be in. any help on this front would be greatly appreciated.

im using autodesk circuits to build the circuit and run the code simulations.the build can be found here. <circuit build>


Code: [Select]
// this is the most recent version of the code ive tried to run

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

// variable for the counter trigger
int switchPin = 10;
int magCount = 0;
// variable to hold the value of the counter trigger
int switchState = 0;
// variable to hold previous value of the counter trigger
int prevSwitchState = 0;

// mode selector variables
int magPin = 8;
int magState = 0;
int prevMagState = 0;

// modes 1, 2, 3 values
int magA = 16;
int magB = 24;
int magC = 50;

// leds for modes
int magAled = 11;
int magAledState = 0;

int magBled = 12;
int magBledState = 0;

int magCled = 13;
int magCledState = 0;

void setup() { 
  // set up the number of columns and rows on the LCD
  lcd.begin(16, 2);
 
  // set up the switch pin as an input
  pinMode(switchPin,INPUT);
  pinMode(magPin, INPUT);
  pinMode(magAled, OUTPUT);
  pinMode(magBled, OUTPUT);
  pinMode(magCled, OUTPUT);
 
  // default mode select screen
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Select a size");
  lcd.setCursor(0, 1);
  lcd.print("of magazine");
}

void loop() { 
 
  // waiting for the input from the mode select button
  do {
    magState = digitalRead(magPin);
    if (magState != prevMagState) {
      if (magState == LOW) { magCount++;}}
   }while(magCount = 0);
 
   prevMagState = magState;
 
  // modes 1, 2, 3 switch, case selects
  switch(magCount)
  {
    case 1:   
     if (magCount == 1); {
        magCount == magA;
        digitalWrite(magAled, HIGH);
        magCount--;
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Mag size entered");
        lcd.setCursor(0, 1);
        lcd.print(magCount); }
    break;
    case 2:   
     if (magCount == 2); {
        magCount == magB;
        digitalWrite(magBled, HIGH);
        magCount--;
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Mag size entered");
        lcd.setCursor(0, 1);
        lcd.print(magCount); }
    break;
    case 3:   
     if (magCount == 3); {
        magCount == magC;
        digitalWrite(magCled, HIGH);
        magCount--;
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Mag size entered");
        lcd.setCursor(0, 1);
        lcd.print(magCount); }
    break;
  }
}


Go Up