Need Coding Help with LEDs and Two Pushbuttons

I'm having trouble creating my Arm Wrestling game using an Arduino, A Breadboard, 9 LEDs, 2 Pushbuttons, 9-200 Ohm Resistors, and two 1k Ohm Resistors for the pushbuttons. the first picture is an image of the first design of my project.

This is the final look of it for now but I cannot figure out how to code it properly to have a constant green LED on in the middle and when one button on either side is pressed, it goes +1 space in the opposite direction of that button itself. So the next image is how far I got with attempting to break down the project and do one side at a time. But only recently I had the idea of coding the LEDs like they're spaces on a board in a line which they are essentially, but the light moving 1 by 1 in two directions only left and right.

If anyone can help me figure how I can code the movement of light back and forth a line of LEDs by pressing one button that would send the light in the opposite direction by 1 LED, that would be greatly appreciated

Post the code that you have so far

This was my code for the full two Pushbutton design so far, unfinished and in the middle of editing before I transferred to attempting my Half Breadboard Design. If you are wondering why there is only 1 Integer setup (sensor Value) is because I was using the blocks option in tinkercad to try and attempt to make it a lot simpler but it didn't really work out so then I moved back to text only in my Half design.

int sensorValue = 0;

void setup()
{
pinMode(A0, INPUT);
pinMode(6, OUTPUT);
pinMode(12, INPUT);
pinMode(8, OUTPUT);
pinMode(13, INPUT);
pinMode(5, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}

void loop()
{
// 2= right white 3= right red 4 = right orange 5 =
// right yellow 6 = green 8 = left yellow 9 = left
// orange 10= left red 11= left white 12 = right
// button 13 = left button
sensorValue = analogRead(A0);
digitalWrite(6, HIGH);
if (analogRead(A0) == HIGH) {
if (digitalRead(12) == HIGH) {
digitalWrite(6, LOW);
digitalWrite(8, HIGH);
} else {
}
if (digitalRead(13) < HIGH) {
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
}
} else {
if (analogRead(A0) < LOW) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
}
}

And this is my coding for the half Arduino Game.

int ledPinG = 6;
int ledPinY = 5;
int ledPinO = 4;
int ledPinR = 3;
int ledPinW = 2;
int inPin = 12;
int val = 0;

void setup()
{
pinMode(ledPinG, OUTPUT);
pinMode(inPin, INPUT);
}

void loop()
{
val = digitalRead(inPin);
if (val == HIGH) {
digitalWrite(ledPinG, LOW);
} else {
digitalWrite(ledPinG, HIGH);

}}

Thanks for posting the code

Please follow the advice on posting code given in Read this before posting a programming question in order to make your sketch easy to follow, download and test

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

As to your code, start by giving your pins meaningful names

Perhaps start with these

const byte leftInput = A0;
const byte rightInput = 12;

void setup()
{
  pinMode(leftInput, INPUT_PULLUP);
  pinMode(rightInput, INPUT_PULLUP);

The later code then becomes easier to read

Note the use of INPUT_PULLUP to turn on the built in pullup resistors to keep the inputs in a known state when the buttons are not pressed. Change your wiring to take the inputs LOW when the buttons are pressed and a state of LOW will indicate that the button has been pressed

Later in the code

  if (analogRead(A0) == HIGH)

Why are you using analogRead() ?

    if (digitalRead(13) < HIGH)

why not test for LOW ?

As to the LEDs, it would make sense for the pin numbers to be put in an array of bytes and for the button inputs to move the index one way or the other to turn on the appropriate LED

You should also look at detecting when a button becomes pressed rather than when it is pressed. Look at the stateChangeDetection example in the IDE to see how to do it

Hi, I have a problem with trying to start coding. My idea is that I have two Pushbuttons on opposite sides of a breadboard and that in between are 9 LEDs all in a line. I would like some help getting the idea of moving space by space linearly by the pushbutton when pressed. The picture below shows what it should look like, and some further explanation of what I'm hoping to try to achieve is that the green LED in the middle stays constantly on until one of the buttons is pressed after they are pressed. Let's say the left button is pressed, the Yellow LED to the right should then come on and the green should shut off, and continuing the situation if the right pushbutton is pressed then the green LED in the middle should then light again. To better explain I want to figure out how to code this to move like it is a +1 then -1 on an x axis or just down a line of LEDS.

Your diagram only shows a y axis of LEDs.

Confused.

I would put the LED pins in an array, and then use the push buttons to work back and forth thru the array.
Something like this, I leave it you to flesh out the rest

byte arrayPins[] = {2,3,4,5,6,7,8,9,10,}; // or as needed
byte leftButton = 11;
byte rightButton = 12;
byte x;
byte currentLed =0;
minLed = 0
maxLed = 8;
void setup(){
currentLed = 0;
define the output pins
for (x = 0; x <9; x=x+1){
pinMode (arrayPins[x], Output);
}
pinMode (leftButton, Input_Pullup); // button connects  pin to Gnd when pressed
pinMode (rightButton, Input_Pullup);
}
void loop(){
if (digitalRead(leftButton) == LOW){ //
if (currentLed == maxLed){ do nothing}
else {
turn off arrayPins[currentLed]
currentLed = currentLed+1
turn on arrayPins[currentLed]
}
if (digitalRead (rightButton) == LOW){
if (currentLed == minLed){ do nothing}
else {
turn off arrayPins[currentLed]
currentLed = currentLed -1
turn on arrayPins[currentLed]
}
}

Make sense?

Topics on a the same project moved to a common location and merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

kyle_isaksen:
If anyone can help me figure how I can code the movement of light back and forth a line of LEDs by pressing one button that would send the light in the opposite direction by 1 LED, that would be greatly appreciated

For that you'll need IDE -> file/examples/digital/state change detection. You want to sense that the switch *became *pressed, not that it *is *pressed.

A possibility:

Declare an unsigned int and initialize it with a value of 0b10000. The set bit (one) represents the constant green LED and the blue zero is the LSB of the int ( rightmost LED ). Whenever a switch becomes pressed * bitShift left or right the int in the appropriate direction. So, if the left switch gets made the binary value in the int would become 0b01000 when right-shifted.

Send these individual bits to the LEDs with a series of bitRead()* statements a la

digitalWrite(someLED, bitRead(theInt, bitMatchingTheLED))

*IDE reference page

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