Coding is fun... until it's not

Hello all,

I have some questions regarding some simple arduino R3 code. I am playing around with a little “arduino start up kit” that has numerous push buttons, leds, resistors etc. and am trying to get a simple circuit to work.

What I am aiming to do:

Use one push button to turn the green LED on only while its being pressed (red LED must be off)

Use another push button to turn the red LED on only while its being pressed (green LED must be off)

Use a motion sensor to trigger a sequence of red and green LED on off states if its tripped

note: the priority is the push buttons, the code must check their state FIRST continuously, and then if nothing is pushed it can check the motion sensor

I have the circuit built correctly, so that both LEDs do turn on and the push buttons will control them (each one respectively), the problem that I am having is that I can only get each button to turn either LED on indefinitely, with only a single push. I have tried if/else statements, while loops, functions and am flat out too inexperienced to make this thing work the way I want it to. Any thoughts?

//Begin serial communications at 9600 bits/sec

const int forward = 3;
const int reverse = 4;
const int msense = 6;
const int Bforward = 11;
const int Breverse = 10;
int yellow = 13;


void setup() {
    Serial.begin(9600);
    pinMode(msense, INPUT_PULLUP); //This is the motionsensor
    pinMode(forward, OUTPUT); //This is the green LED
    pinMode(reverse, OUTPUT); //This is the red LED
    pinMode(yellow, OUTPUT); //This is the built in LED that I am using to monitor when the code is working
    pinMode(Bforward, INPUT); //This is the green LED button
    pinMode(Breverse, INPUT); //This is the red LED button 
    digitalWrite(forward, LOW);
    digitalWrite(reverse, LOW);
}

void loop() {
  int detect = digitalRead(msense);
  int Bpress = digitalRead(Bforward);
  int Bpress1 = digitalRead(Breverse);
  delay(5);
// I'm trying to check both buttons at the same time with this code... Could be a //better way?
    if (Bpress == LOW || Bpress1 == LOW){ 
      digitalWrite(yellow, HIGH);
        if (Bpress == LOW){
        digitalWrite(reverse, LOW);
        digitalWrite(forward, HIGH);
        }
       
        else if (Bpress1 == LOW){
        digitalWrite (forward, LOW);
        digitalWrite(reverse, HIGH);
        }
        else {
        digitalWrite(reverse, LOW);
        digitalWrite(forward, LOW);
        digitalWrite(yellow, LOW);
        }
    }
 //This is the motion sensor sequence that should run after motion is detected and no buttons are pressed   
else if (detect == LOW){
      digitalWrite(yellow, LOW);
      delay(1000);
      digitalWrite (yellow, HIGH); 
      delay(1000);
      digitalWrite(forward, HIGH);
      delay(5000);
      digitalWrite(forward, LOW);
      delay (5000);
      digitalWrite(reverse, HIGH);
      delay(5000);
      digitalWrite(reverse, LOW);
      delay(1000);
      digitalWrite(yellow, LOW);
      }
    
}

Rather than try to think through the logic in C language, you might try writing the program in what is called pseudocode, i.e. simple English, then figure out how to translate that to C. It is easier to see all the possibilities that way. Use simple if statements if you can.

For example, something like this:

Loop:

If Button1 is pressed and Button2 is not pressed, turn on lamp 1.

If Button1 is not pressed and Button2 is pressed, turn on lamp 2.

if Button1 is not pressed and Button2 is not pressed, turn off all lamps.

if Button1 is pressed and Button2 is pressed, do something else.

if MotionDetector is active, LightSequence().

goto Loop

Psst…

Digitalwrite (greenled, greenbutton);

Capiche?

hneve: You, my friend, are a genius.

jremington: This is definitely something that I didnt not think to do, I will try that approach and see if I can not iron out my issues. Thank you

Haha!

This is the perfect ‘classic example’ of the contradiction to the thread title, lol

consider this as 1st article… “there are many ways to skin a cat”

-thus- Coding isn’t fun, until it becomes fun!

Like most everything in our conscious reality, standards will vary from perspective to perspective…

as the scale to be applied is not a constant…

So, “different strokes for different folks”

Meaning – since there are many solutions to a given problem, in varying degrees of difficulty and range,

some may find that a solution they perceived as unworkable, was truly unworkable, but only to them…

while another would find that unworkable solution only slightly in error, and devise an instant fix…

But the inverse of an equation happens to sometimes be the equal, and only to a select few…

With the penultimate experience proving that when people pay attention to others solutions and methodology of solution, they can learn pretty high value experiences without the effort required otherwise.

Thats why these type of problem solver forums might very well be the most effective education format, producing results at the highest rates.

Me thinks future education formats will become more effective if they follow this design in some way.

Classes can be merely refined question/answer forum type interactive arenas, with critical controls and moderate guidance in the directors hands, leaving the element of fresh uncertainty to be constantly experienced by the students.

This = maximum fun /attention levels since it can be almost presented as a ‘game’ or similar competitive challenge…

If I twisted anyones mellon with this,

my apologies, LOl :sunglasses:

So true :slight_smile:

Lol…

You may want to look into state machines when you get a chance. They are overkill for this program, but I have a feeling you will be heading in that direction in the future :slight_smile:

/mike