Looking for code help and understanding feasibility

Looking for help on the code below. The idea is that when the button is pushed (1) random number from between 0-99 is chosen. If the that number is equal to 8 the green led blinks 3 times and the servo activates. If the number doe not equal 8 the green led stays off and the Red led blinks 3 times.

Im new to coding and looking for any guidance.

Cheers,
Jake

Push_button_Random_v3.ino (2.08 KB)

You have described what should happen but what happens in practice ?

  if (Push_Button = HIGH);

  if (Push_Button = LOW);

2 lines of code and 4 mistakes

= is for assignment
== is for comparison

What are those semicolons doing there apart from being the only code executed if the wrongly coded test returns true ?

You will get faster help if you post your code according to the forum guidelines in the how to use this forum-please read sticky. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Like so:

//Declare and initialize LED pin variables
int GREEN_1 = 9;
int RED_2 = 10;
int Push_Button = 2;
int incomingByte = 0; // for incoming serial data
int pos = 0;   // Servo
long randNumber;

#include <Servo.h>

Servo myservo;

//this variable will hold a random number generated by the random() function
long randomNumber;

//Set up - this is where you get things "set-up". It will only run once
void setup()
{

   //setup serial communications through the USB
   Serial.begin(9600);

   //Let's print a start messgae to the serial monitor when a new sequence of random numbers starts
   Serial.println("Starting new Random Number Sequence");

   //set the LED pins as outputs
   pinMode(GREEN_1, OUTPUT);
   pinMode(RED_2, OUTPUT);
   pinMode(Push_Button, INPUT);


   //Let's make it more random
   randomSeed(analogRead(A0));

   myservo.attach(5);  //Servo Pin number

}//close setup

void loop()
{

   // Card Swiped to start sequence
   int Push_Button = digitalRead(2);
   // If the buttoin is not pressed do nothing
   if (Push_Button = HIGH);

   // If the button is pressed read the "random number"
   if (Push_Button = LOW);
   {
      randomNumber = random(100);
      Serial.println(randNumber);
      delay(50);
      (Serial.available() > 0);
      // read the incoming byte:
      incomingByte = Serial.read();
   }
   if (Serial.read() == '8');
   {
      // Blink Green LED
      digitalWrite(9, HIGH);
      delay(100);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(9, HIGH);
      delay(100);
      digitalWrite(9, HIGH);
      delay(100);
   }
   else
   {
      // Blink Red LED
      digitalWrite(10, HIGH);
      delay(100);
      digitalWrite(10, LOW);
      delay(100);
      digitalWrite(10, HIGH);
      delay(100);
      digitalWrite(10, HIGH);
      delay(100);
   }
   //Send Door Open Command
   for (pos = 0; pos <= 180; pos += 1);
   {
      myservo.write(pos);
      delay(15);
   }
}

I see that the code to run the servo (Send Door Open Command) is outside of any decision making code so it will run every time through loop(). Is that what you want?

You give the pins names, but use the pin numbers in the code, making the code harder to follow.

How is the button switch wired? Is there a pulldown or pullup resistor on the button pin input pin?

if (Push_Button = LOW);

The semicolon at the end of the if ends the if. All subsequent code in the if block will execute unconditionally. And = is for assignment.

UKHeliBob:

  if (Push_Button = HIGH);

if (Push_Button = LOW);



2 lines of code and 4 mistakes

= is for assignment
== is for comparison

What are those semicolons doing there apart from being the only code executed if the wrongly coded test returns true ?

6 mistakes. Those are pin numbers on the left hand side. Push_Button is defined as 2. HIGH is defined as 1. 2 will never equal 1.

I think you forgot a digitalRead in there somewhere.

if (Push_Button = HIGH);

Maybe a fancy way of creating a very short delay, similar to a ‘NOP’. ???