Skip to main content
formatted code for readability. Added tag.
Source Link
sa_leinad
  • 3.2k
  • 2
  • 24
  • 53

I am trying to make a serial monitor print something based on when a button is pushed. 

When the button is not pushed, the variable storing it equals 1. When
When the button is pushed, the variable equals 0. When
When I have an if statement like this, why does it print what I want when I have one where the zero is, but not when it is like that and the button IS pressedpressed.

Thanks

int led = 2;
const int buzzer = 3;

int upButton = 5;
int downButton = 7;
int enterButton = 6;

int up = 1;
int down = 1;
int enter = 1;

bool homeActivated = true;
bool appsActivated = false;

int homeLocation = 1;
int appsLocation = 1;

bool homePrinted = false;
bool appsPrinted = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode (upButton, INPUT);
  pinMode (enterButton, INPUT);
  pinMode (downButton, INPUT);
  Serial.println("Initializing");
  delay(2000);
}

void loop() {
  up = digitalRead (upButton);
  enter = digitalRead (enterButton);
  down = digitalRead (downButton);

  //move up and down
  if (up = 0)
  {
    if (homeActivated = true)
    {
      homeLocation + 1;
      Serial.println("Scrolled Down 1");
      delay(1500);
      homePrinted = false;
    }
    else if (appsActivated = true)
    {
      appsLocation + 1;
      Serial.println("Scrolled Down 1");
      delay(1500);
      appsPrinted = false;
    }
  }

I am trying to make a serial monitor print something based on when a button is pushed. When the button is not pushed, the variable storing it equals 1. When the button is pushed, the variable equals 0. When I have an if statement like this, why does it print what I want when I have one where the zero is, but not when it is like that and the button IS pressed.

Thanks

int led = 2;
const int buzzer = 3;

int upButton = 5;
int downButton = 7;
int enterButton = 6;

int up = 1;
int down = 1;
int enter = 1;

bool homeActivated = true;
bool appsActivated = false;

int homeLocation = 1;
int appsLocation = 1;

bool homePrinted = false;
bool appsPrinted = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode (upButton, INPUT);
  pinMode (enterButton, INPUT);
  pinMode (downButton, INPUT);
  Serial.println("Initializing");
  delay(2000);
}

void loop() {
  up = digitalRead (upButton);
  enter = digitalRead (enterButton);
  down = digitalRead (downButton);

  //move up and down
  if (up = 0)
  {
    if (homeActivated = true)
    {
      homeLocation + 1;
      Serial.println("Scrolled Down 1");
      delay(1500);
      homePrinted = false;
    }
    else if (appsActivated = true)
    {
      appsLocation + 1;
      Serial.println("Scrolled Down 1");
      delay(1500);
      appsPrinted = false;
    }
  }

I am trying to make a serial monitor print something based on when a button is pushed. 

When the button is not pushed, the variable storing it equals 1.
When the button is pushed, the variable equals 0.
When I have an if statement like this, why does it print what I want when I have one where the zero is, but not when it is like that and the button IS pressed.

Thanks

int led = 2;
const int buzzer = 3;

int upButton = 5;
int downButton = 7;
int enterButton = 6;

int up = 1;
int down = 1;
int enter = 1;

bool homeActivated = true;
bool appsActivated = false;

int homeLocation = 1;
int appsLocation = 1;

bool homePrinted = false;
bool appsPrinted = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode (upButton, INPUT);
  pinMode (enterButton, INPUT);
  pinMode (downButton, INPUT);
  Serial.println("Initializing");
  delay(2000);
}

void loop() {
  up = digitalRead (upButton);
  enter = digitalRead (enterButton);
  down = digitalRead (downButton);

  //move up and down
  if (up = 0)
  {
    if (homeActivated = true)
    {
      homeLocation + 1;
      Serial.println("Scrolled Down 1");
      delay(1500);
      homePrinted = false;
    }
    else if (appsActivated = true)
    {
      appsLocation + 1;
      Serial.println("Scrolled Down 1");
      delay(1500);
      appsPrinted = false;
    }
  }

int led = 2; const int buzzer = 3;

int upButton = 5; int downButton = 7; int enterButton = 6;

int up = 1; int down = 1; int enter = 1;

bool homeActivated = true; bool appsActivated = false;

int homeLocation = 1; int appsLocation = 1;

bool homePrinted = false; bool appsPrinted = false;

void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode (upButton, INPUT); pinMode (enterButton, INPUT); pinMode (downButton, INPUT); Serial.println("Initializing"); delay(2000); }

void loop() { up = digitalRead (upButton); enter = digitalRead (enterButton); down = digitalRead (downButton);

//move up and down if (up = 0) { if (homeActivated = true) { homeLocation + 1; Serial.println("Scrolled Down 1"); delay(1500); homePrinted = false; } else if (appsActivated = true) { appsLocation + 1; Serial.println("Scrolled Down 1"); delay(1500); appsPrinted = false; } }

int led = 2;
const int buzzer = 3;

int upButton = 5;
int downButton = 7;
int enterButton = 6;

int up = 1;
int down = 1;
int enter = 1;

bool homeActivated = true;
bool appsActivated = false;

int homeLocation = 1;
int appsLocation = 1;

bool homePrinted = false;
bool appsPrinted = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode (upButton, INPUT);
  pinMode (enterButton, INPUT);
  pinMode (downButton, INPUT);
  Serial.println("Initializing");
  delay(2000);
}

void loop() {
  up = digitalRead (upButton);
  enter = digitalRead (enterButton);
  down = digitalRead (downButton);

  //move up and down
  if (up = 0)
  {
    if (homeActivated = true)
    {
      homeLocation + 1;
      Serial.println("Scrolled Down 1");
      delay(1500);
      homePrinted = false;
    }
    else if (appsActivated = true)
    {
      appsLocation + 1;
      Serial.println("Scrolled Down 1");
      delay(1500);
      appsPrinted = false;
    }
  }

int led = 2; const int buzzer = 3;

int upButton = 5; int downButton = 7; int enterButton = 6;

int up = 1; int down = 1; int enter = 1;

bool homeActivated = true; bool appsActivated = false;

int homeLocation = 1; int appsLocation = 1;

bool homePrinted = false; bool appsPrinted = false;

void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode (upButton, INPUT); pinMode (enterButton, INPUT); pinMode (downButton, INPUT); Serial.println("Initializing"); delay(2000); }

void loop() { up = digitalRead (upButton); enter = digitalRead (enterButton); down = digitalRead (downButton);

//move up and down if (up = 0) { if (homeActivated = true) { homeLocation + 1; Serial.println("Scrolled Down 1"); delay(1500); homePrinted = false; } else if (appsActivated = true) { appsLocation + 1; Serial.println("Scrolled Down 1"); delay(1500); appsPrinted = false; } }

int led = 2;
const int buzzer = 3;

int upButton = 5;
int downButton = 7;
int enterButton = 6;

int up = 1;
int down = 1;
int enter = 1;

bool homeActivated = true;
bool appsActivated = false;

int homeLocation = 1;
int appsLocation = 1;

bool homePrinted = false;
bool appsPrinted = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode (upButton, INPUT);
  pinMode (enterButton, INPUT);
  pinMode (downButton, INPUT);
  Serial.println("Initializing");
  delay(2000);
}

void loop() {
  up = digitalRead (upButton);
  enter = digitalRead (enterButton);
  down = digitalRead (downButton);

  //move up and down
  if (up = 0)
  {
    if (homeActivated = true)
    {
      homeLocation + 1;
      Serial.println("Scrolled Down 1");
      delay(1500);
      homePrinted = false;
    }
    else if (appsActivated = true)
    {
      appsLocation + 1;
      Serial.println("Scrolled Down 1");
      delay(1500);
      appsPrinted = false;
    }
  }
Source Link
Vex
  • 13
  • 3

Troubleshooting an Arduino Computer Program

I am trying to make a serial monitor print something based on when a button is pushed. When the button is not pushed, the variable storing it equals 1. When the button is pushed, the variable equals 0. When I have an if statement like this, why does it print what I want when I have one where the zero is, but not when it is like that and the button IS pressed.

Thanks

int led = 2; const int buzzer = 3;

int upButton = 5; int downButton = 7; int enterButton = 6;

int up = 1; int down = 1; int enter = 1;

bool homeActivated = true; bool appsActivated = false;

int homeLocation = 1; int appsLocation = 1;

bool homePrinted = false; bool appsPrinted = false;

void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode (upButton, INPUT); pinMode (enterButton, INPUT); pinMode (downButton, INPUT); Serial.println("Initializing"); delay(2000); }

void loop() { up = digitalRead (upButton); enter = digitalRead (enterButton); down = digitalRead (downButton);

//move up and down if (up = 0) { if (homeActivated = true) { homeLocation + 1; Serial.println("Scrolled Down 1"); delay(1500); homePrinted = false; } else if (appsActivated = true) { appsLocation + 1; Serial.println("Scrolled Down 1"); delay(1500); appsPrinted = false; } }