Go Down

Topic: button ˝mode˝ on pin 2 is not working (Read 320 times) previous topic - next topic

alphasniper

Aug 28, 2019, 05:48 pm Last Edit: Aug 28, 2019, 05:50 pm by alphasniper
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!

Code: [Select]
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);
}

larryd

#1
Aug 28, 2019, 06:19 pm Last Edit: Aug 28, 2019, 06:24 pm by larryd
off;

Should be:

off();

You should use 'change in state' instead of 'level' detection.

See example in IDE.

https://www.arduino.cc/en/Tutorial/StateChangeDetection     

How is your switch wired?

S1, S2 or S3?





No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

alphasniper

#2
Aug 29, 2019, 08:29 am Last Edit: Aug 29, 2019, 08:32 am by alphasniper
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

Code: [Select]
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);
}

Paul__B

I checked the link and fixed my code, but in the simulator, it does not work.
Then don't use a simulator?  :smiley-roll:

Grumpy_Mike

#4
Aug 29, 2019, 02:16 pm Last Edit: Aug 29, 2019, 02:21 pm by Grumpy_Mike
Quote
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.


Go Up