Introduction: Button Count Is (Prime, Even, or Odd)

Picture of Button Count Is (Prime, Even, or Odd)

Project Goals:

If you press a button (x) times,

--turn on the primeLED if (x) is a prime number

--turn on the evenLED if (x) is a even number

--turn on the oddLED if (x) is a odd number

Step 1: Set-up the Circuit

Picture of Set-up the Circuit

Button

-Connect a 10K resistor to the (+) bus on the breadboard. (Note: This is a Pull-Up Resistor)

-Attach a 10K resistor to the button and also connect a wire from pin 2 to the same row as the resistor.

-The other terminal on the button should be connected to ground using a black wire.

Prime LED

-Connect a wire from pin 8 to the (+) terminal of the LED.

-The (-) terminal of the LED should be connected to ground using a 330 ohm resistor.

Even LED

-Connect a wire from pin 13 to the (+) terminal of the LED.
-The (-) terminal of the LED should be connected to ground using a 330 ohm resistor.

Odd LED

-Connect a wire from pin 12 to the (+) terminal of the LED.
-The (-) terminal of the LED should be connected to ground using a 330 ohm resistor.

Step 2: Declare the Variables

//Assign the pins

const int buttonPin = 2;

const int evenLED = 13;

const int oddLED = 12;

const int primeLED = 8;

//These are used as delays

int wait = 100;

int reset = 1000;

//This is the value of the input...it will increase by 1 everytime the button is pressed

int buttonCount = 0;

//These are both digital values can be (HIGH or LOW) or also written as (0 or 1). These allow to detect if the button was pressed by checking the current instance with the previous instance.

int currentState = 0;

int previousState = 0;

//This is an array of numbers, we can modify this array by adding additional numbers.

int primeSet[] = {2,3,5,7};

//Use the following analogy to understand how this works. Imagine you have a pallet with a stack of boxes. To calculate that the # of boxes, first measure the "sizeof" or volume of the entire pallet, then measure the "sizeof" or volume of an individual box...use division to count the number of boxes on the pallet.

int primeSize = sizeof (primeSet) / sizeof(int);

Step 3: Setup

void setup()

{

pinMode(buttonPin, INPUT); //press the button (x) of times

pinMode(primeLED, OUTPUT); //turn this on when x is prime

pinMode(evenLED, OUTPUT); //turn this on when x is even

pinMode(oddLED, OUTPUT); //turn this on when x is odd

Serial.begin(9600); //we want to print and review real-time data. Very useful for troubleshooting.

}

Step 4: Loop

void loop()
{

currentState = digitalRead(buttonPin); //is the button pressed? Lets read it using the buttonPin.

if (currentState != previousState) //if the button state has changed..meaning its NOT like it was before.

{

if (currentState == LOW) //Note: I used a pull-up resistor so its set as (LOW) when its pressed.

{

if (buttonCount >= 10) //When the buttonCount reaches 10...send the user a RESTART signal.

{

buttonCount = 0; //RESTART the buttonCount back to zero.

//Display a RESTART signal so that the user knows they are starting over. digitalWrite(primeLED, HIGH);

digitalWrite(evenLED, HIGH);

digitalWrite(oddLED, HIGH);

delay(reset);

digitalWrite(primeLED, LOW);

digitalWrite(evenLED, LOW);

digitalWrite(oddLED, LOW);

delay(reset);

digitalWrite(primeLED, HIGH);

digitalWrite(evenLED, HIGH);

digitalWrite(oddLED, HIGH);

delay(reset);

digitalWrite(primeLED, LOW);

digitalWrite(evenLED, LOW);

digitalWrite(oddLED, LOW);

}

else // If the buttonCount is less than 10, then Arduino will perform specific functions.

{

buttonCount++; //increment the buttonCount by 1.

Serial.print("Button Count = "); //Print a string

Serial.println(buttonCount); //Print the current value of the buttonCount

//Start by turning off all the values when buttonCount = 0

digitalWrite(primeLED, LOW);

digitalWrite(evenLED, LOW);

digitalWrite(oddLED, LOW);

//This code allows you perform a specific function if the condition "is_prime(buttonCount)" is True

if (is_prime(buttonCount))

{

digitalWrite(primeLED, HIGH);

}

else

{

digitalWrite(primeLED, LOW);

}

//This code allows you perform a specific function if the condition "is_even(buttonCount)" is True.

if (is_even(buttonCount))

{

digitalWrite(evenLED, HIGH);

digitalWrite(oddLED, LOW);

}

else

{

digitalWrite(evenLED, LOW);

digitalWrite(oddLED, HIGH);

}

}

}

previousState = currentState; //This helps you save data of whether the button is being pressed or not.

}

delay(wait);

}

boolean is_prime(int inputNumber)

{

for (int i = 0; i < primeSize; i++)

{

if (inputNumber == primeSet[i])

{

return true;

}

}

return false;

}

boolean is_even(int n)

{

return (n % 2 == 0);

}

Comments

AdamW5 made it! (author)2014-11-30

Here are the photos of my circuit.

On to the next project...

About This Instructable

1,702views

5favorites

License:

More by AdamW5:MIT Android App Using the Bluetooth HC06 to Connect to an Arduino UnoButton Count is (Prime, Even, or Odd)
Add instructable to: