Arduino Circuit, 2 buttons, 2 leds, and motor

Update: I got my code working and this is what it looks like now. Functions exactly as intended. Thanks for your help everybody!

// C++ code
//
const int buttonPin1 = 10;     // the number of the pushbutton pin
const int ledPin1 =  13;      // the number of the LED pin
const int buttonPin2 = 5;
const int ledPin2 =  8;



int buttonPushCounter1 = 1;   // counter for the number of button presses
int buttonState1 = 0;         // current state of the button
int lastButtonState1 = 0;

int buttonPushCounter2 = 0;
int buttonState2 = 0;
int lastButtonState2 = 0;

int LedState1 = HIGH;
int LedState2 = LOW;

void setup()
{
  // initialize LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  // initialize  pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);

  // initialize LED 1on, LED2 off
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);
  Serial.begin(9600);
}

void loop()
{
  // PUSH BUTTON 1
  buttonState1 = digitalRead(buttonPin1);

  // if button 1 is psuhed
  if (buttonState1 ==  0)
  {
    buttonPushCounter1++;
    delay(150); // switch debounce

    if (buttonPushCounter1 % 2 == 0)
    {
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, HIGH);
      //analogWrite (3, 255);
    } // end pushButton Counter

    else//if (buttonPushCounter1 % 2 != 0)
    {
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
    } // end pushButton Counter
  } //if push button 1 state == 0

  // PUSH BUTTON 1
  buttonState2 = digitalRead(buttonPin2);

  // if button 2 is psuhed
  if (buttonState2 ==  0)
  {
    buttonPushCounter2++;
    delay(150); // switch debounce

    if (buttonPushCounter2 % 2 == 0)
    {
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, HIGH);
      //analogWrite (3, 255);
    } // end pushButton Counter

    else//if (buttonPushCounter1 % 2 != 0)
    {
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
    } // end pushButton Counter
  } //if push button 2 state == 0

} // end loop


Hi, @davirich
Welcome to the forum.
To add code please click this link;

Can you also post a circuit diagram so we can see how you have wired the hardware, in particular your buttons.
Your code looks for HIGH on your buttons, have you got a 10K resistor between each input pin that has a button and gnd?
This is t o make sure the input gets a LOW when you button is not pressed.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

I can't see any attempt at timing the motor run and the code around buttonPIn2 does nothing with ledPin. Please post code showing that you have tried something and describe exactly what happens.

Steve

Hello
Welcome to the forum.
What do you expect?
What is your current question?
Is this a school assignement?

Ok Thank you very much , Tom. I have added the picture of my circuit

You cannot use the Arduino’s 5v to power a motor.

Sorry, I took out my timer attempt because it wasn't working. I have added it back now along with a picture of my circuit. Also for my code around buttonpin2 when I add the line of digitalWrite(ledPin, HIGH); for the else statement, it makes both buttons able to turn the other led off or on, but not it's own led off and on. So when I press button1 led2 will turn on or off, but led1is not affected

Hello Paul, I would like some help correctly writing the code for my buttons and leds. Right now when I run the circuit the buttons are able to turn each other's leds on or off but not their own. So when I press button, led2 is turned on or off, but led is not affected. Yes this is for a school project for engineering 100

Your code is still posted incorrectly. If you do not understand what you have been asked to do, please say that.

I'm sorry for the errors. I have reuploaded using the copy for forum feature, is this better? Thanks for your help

Yes, thankyou.

When making changes it is better to repost the code in a new message rather than changing the original and making earlier comments look stupid.

That isn't how you use millis(). if (millis() > 3000) will always be true whenever the program has been running for 3 seconds or more. Look at the BlinkWithoutDelay" example in the IDE for some ideas.

Steve

Hello
You could take a view here:

Have a nice day and enjoy coding in C++.

Ok that makes sense, thank you for your help.

With the specific exception of simply adding code tags to an earlier post. it is then quite clear what has been corrected and it then makes far more sense for someone who has just come to the thread. Particularly for extremely long code listings.

Hi,

Thanks for adding the Fritzy image, but please add anything as a new post, rather than go back and edit an earlier post.
The flow of the tread is not very readable when things asked for in later posts appear in earlier ones.

If your code is now working and the code in your original post is now correct, what is the purpose of the forum for others to learn.
This could have been a good thread for someone else with a similar problem to work through.

Tom.... :smiley: :coffee: :coffee: :coffee: :coffee: :australia: