Weemos Programing INPUT programming Resistance

Ive attached my program and sketch. Can anyone tell me if the code has any issues with calling up resistors and code driving the motors? Not very confident with this code working. D4 has a 10K pullup resistor and D8 has a pulldown 10K resistor. How do I adjust the code to effectively interact with the buttons on D4 and Motor controller signal on D8. So for D4 do I simply type pinMode(RSDin, INPUT_PULLUP)? and D8 type pinMode(RSD, INPUT???

Thats as far as I have gotten

//Left Side Up and Left Side Down (Inputs and Outputs)
int LSUin = D1;
int LSDin = D2;
int RSUin = D3;
int RSDin = D4;
int LSU = D5;
int LSD = D6;
int RSU = D7;
int RSD = D8;

int buttonStateLeftdown = digitalRead(LSDin);
int buttonStateLeftup = digitalRead(LSUin);
int buttonStateRightdown = digitalRead(RSDin);
int buttonStateRightup = digitalRead(RSUin);

}
void setup() {
  // Setting Inputs and Outputs
  pinMode(RSU, OUTPUT);
  pinMode(RSD, OUTPUT);
  pinMode(LSU, OUTPUT);
  pinMode(LSD, OUTPUT);
  pinMode(LSUin, INPUT);
  pinMode(LSDin, INPUT);
  pinMode(RSUin, INPUT);
  pinMode(RSDin, INPUT);
}
void loop(){
  // runs repeatedly:
  if (buttonStateLeftdown == HIGH){
  digitalWrite(LSD, HIGH);
  digitalWrite(LSU, LOW);

  } 
  else if (buttonStateLeftup == HIGH){
  digitalWrite(LSU, HIGH);
  digitalWrite(LSD, LOW);
 
  } 
  else if 
  (buttonStateRightup == HIGH){
  digitalWrite(RSU, HIGH);
  digitalWrite(RSD, LOW);

  } 
  else if
  (buttonStateRightdown == HIGH){
  digitalWrite(RSD, HIGH);
  digitalWrite(RSU, LOW);

 }
//Buttons Released
 else if (buttonStateLeftdown == LOW){
  digitalWrite(LSD, LOW);
  digitalWrite(LSU, LOW);

  } 
  else if (buttonStateLeftup == LOW){
  digitalWrite(LSU, LOW);
  digitalWrite(LSD, LOW);
 
  } 
  else if 
  (buttonStateRightup == LOW){
  digitalWrite(RSU, LOW);
  digitalWrite(RSD, LOW);

  } 
  else if
  (buttonStateRightdown == LOW){
  digitalWrite(RSD, LOW);
  digitalWrite(RSU, LOW);
 }
}

I see two things immediately:

  1. You have a closing curly bracket on the line before setup()
  2. In your loop() function you never read buttonStateLeftdown, buttonStateLeftup, buttonStateRightdown, or
    buttonStateRightup

Doing this

int buttonStateLeftdown = digitalRead(LSDin);

creates a variable and initializes it to be the current condition of the digital pin LSDin.

It does not bind the digital pin to the variable! It is a common mistake. If you want to know the current condition of an input pin, you must digitalRead it again and assign the value to the variable.

buttonStateLeftdown = digitalRead(LSDin);

Otherwise the variable never changes… and your tests on it will not be what you think or want.

a7

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.