button ˝mode˝ on pin 2 is not working

hi, so the problem occurs with the button ˝mode˝ on pin 2, the code doesn’t register the signals from the button.

Thanks for the help!

const int mode = 2;
const int led1 = 7;
const int led2 = 8;
const int led3 = 9;
const int led4 = 10;
const int led5 = 11;


int modeState = 0;
int modecount = 0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(mode, INPUT);
}

void loop() {
  modeState = digitalRead(mode);
  if (modeState)
  {
    modecount = (modecount++) % 4;
  }
  if (modecount == 0) //safety
  {
    off;
    digitalWrite(led1, HIGH);
  }

  else if (modecount == 1) //2 semi
  {
    off;
    digitalWrite(led2, HIGH);
  }

  else if (modecount == 2) //2 burst shot
  {
    off;
    digitalWrite(led3, HIGH);
  }

  else if (modecount == 3) //full auto
  {
    off;
    digitalWrite(led4, HIGH);
  } else {
    off;
    digitalWrite(led5, HIGH);
  }
}


void off() {
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
}

off;

Should be:

off();

You should use ‘change in state’ instead of ‘level’ detection.

See example in IDE.

How is your switch wired?

S1, S2 or S3?

thanks larryd!
I checked the link and fixed my code, but in the simulator, it does not work.
I’m trying to replace my trigger on my airsoft gun with Arduino and I want to make different modes ˝safety, semi, 2 burst-shot, full auto, restart˝, and every mode has its own led and it’s own code on how to control the motor, except the restart, that just restarts the count. I want to switch from mode to mode with a button.
my pushbutton is wired in S1.

this is the full code

const int mode = 2;
const int led1 = 7;
const int led2 = 8;
const int led3 = 9;
const int led4 = 10;
const int led5 = 11;
const int t = 4;
const int r = 5;
const int q = 6;

int tStatus = 0;
int rStatus = 0;
int modeState = 0;
int modecount = 0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(q, OUTPUT);
  pinMode(t, INPUT);
  pinMode(r, INPUT);
  pinMode(mode, INPUT);
  Serial.begin(9600);
}

void loop() {
  tStatus = digitalRead(t);
  rStatus = digitalRead(r);
  modeState = digitalRead(mode);
  if (modeState)
  {
    modecount++;
  }

  if (modecount == 0) //semi
  {
    Serial.println("mode = 0");
    off();
    digitalWrite(led1, HIGH);
    digitalWrite(q, LOW);
  }

  if (modecount == 1) //2 semi
  {
    Serial.println("mode = 1");
    off();
    digitalWrite(led2, HIGH);
    if (tStatus == LOW && rStatus == HIGH)
    {
      digitalWrite(q, LOW);
      delay(750);
    } else {
      digitalWrite(q, HIGH);
    }
  }

  else if (modecount == 2) //2 burst shot
  {
    Serial.println("mode = 2");
    off();
    digitalWrite(led3, HIGH);
    for (int i = 0; i <= 2; i++) {
      if (tStatus == LOW && rStatus == HIGH)
      {
        digitalWrite(q, LOW);
        delay(750);
      } else {
        digitalWrite(q, HIGH);
      }
    }
  }

  else if (modecount == 3) //full auto
  {
    Serial.println("mode = 3");
    off();
    digitalWrite(led4, HIGH);
    if (tStatus == LOW && rStatus == HIGH)
    {
      digitalWrite(q, LOW);
    } else {
      digitalWrite(q, HIGH);
    }
  }

  else if (modecoutn == 4)//restart
  {
    Serial.println("mode = 4");
    modecount = 0;
  }
}


void off() {
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
}

alphasniper:
I checked the link and fixed my code, but in the simulator, it does not work.

Then don’t use a simulator? :roll_eyes:

my pushbutton is wired in S1.

How?
The way your code is written requires your push to be wired up incorrectly. It requires you to wire it between input and 5V with a 10K pull down resistor to ground.

If it doesn’t work it means either your wiring is faulty or you have damaged your Arduino pin by doing something silly with it in the past. Try another pin.

The proper way is to wire the push button between input and ground and enable the internal pull up resistor, as shown by S3 in Larryd’s diagram.