Bonus Challenges

Bonus Challenge 2

Here we are again ready to have another adventure. In this bonus challenge you will be working with one new piece of hardware, and the new piece of hardware is… an IR receiver. An IR receiver will allow us to control motors, LEDs, etc… with a standard TV remote. This bonus challenge will upgrade Challenge 4 to be wireless.

Go ahead and build Challenge 4’s gadget; just leave out the push button and potentiometer as we will not be using them for this challenge.

Finished?

Okay, your gadget should look something like this:

first

Additional components you will need for this challenge:

IR receiver (Digikey – PNA4602M-ND)
.1uF Ceramic Capacitor
1kohm resistor

Challenge_2_Parts

Creating The Gadget:

First, you will need to add the IR receiver to the solderless bread board at points E-43, E-44, and E-46. Then connect +5V at point A-46 and the power rail. After that, connect ground to point A-44 and the ground rail. Finally connect D-43 to digital pin 10 on the Arduino to receive the data from the IR receiver.

attachIR

Now connect the .1uF ceramic capacitor to B-46 and B-44. This capacitor is used to get rid of some of the noise found on the power signal.

attachCap

Finally, add the 1kohm resistor to points A-9 and the ground rail on the solderless breadboard. The resistor is a pull-down resistor, you will notice that when you upload the sketch from your computer to the Arduino that your motor will not move this is because the enable pin of the H-bridge is being pulled to ground which is a 0V reference. If you where to get rid of this resistor the motor would run when you are uploading your sketch to the Arduino.

attachRes

That is it for the hardware portion of this challenge.

Okay, here comes the software portion of this challenge. First you will need to download the IRremote library . This library allows us to decode and encode IR codes.

Add that library to your Arduino IDE’s Library directory (see Challenge 6: Chapter 24).

You should have a standard TV remote for this challenge any remote should work. I am using a Toshiba remote.

Challenge_2_Buttons_Used

The first sketch is used to figure out what value your Arduino receives when a button is pressed, so go ahead and upload this example to your Arduino.

// example 32.1 – IR receiver code repeater
// http://tronixstuff.com/tutorials > chapter 32
// based on code by Ken Shirriff – http://arcfn.com
#include // use the library
int receiver = 10; // pin 1 of IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver); // create instance of ‘irrecv’
decode_results results;
void setup()
{
Serial.begin(9600); // for serial monitor output
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
Serial.println(results.value, HEX); // display it on serial monitor in hexadecimal
irrecv.resume(); // receive the next value
} // Your loop can do other things while waiting for an IR command
}

(A big thanks to TRONIXSTUFF for this sketch)

*Note: You should not write 0xFFFFFFFF as your button code as this is the button repeater code

Open your Serial Monitor and press one of the buttons you would like to use for this challenge (if you hold this button down you should see 0xFFFFFFFF this code indicates that you are holding the button down), and write down the code you see in the Serial Monitor. This button will make the motor run in a clockwise rotation and turn the green LED on.

clockwise_button

Now go ahead and press another button on your TV remote. Once again the code should be displayed on the Serial Monitor. Go ahead and right it down. This button turns the motor, and LEDs off.

off_button

Finally, press a third button on your remote. Again write the code you see in the Serial Monitor down. This button will make the motor turn in a counter-clockwise rotation and turn the red LED on.

counter-clock_button

Now it is time for the sketch for this bonus challenge! Here it is:

// IR Library
#include

int IRPin = 10;
IRrecv irrecv(IRPin);
decode_results results;

// Initialize all of the pins variables
int LEDPin1 = 12;
int LEDPin2 = 11;
int MotorPWMPin = 3;
int MotorDirPin = 4;

void setup()
{
// Set pins to be outputs
pinMode(LEDPin1, OUTPUT);
pinMode(LEDPin2, OUTPUT);
pinMode(MotorPWMPin, OUTPUT);
pinMode(MotorDirPin, OUTPUT);

Serial.begin(9600);

// Start the receiver
irrecv.enableIRIn();

}

void loop()
{

if (irrecv.decode(&results)) // have we received an IR signal?
{
switch (results.value) {
case “FIRST BUTTON HERE”:
Serial.println(“pressed 1”);
analogWrite(MotorPWMPin, 255);
digitalWrite(MotorDirPin, HIGH);
digitalWrite(LEDPin1, HIGH);
digitalWrite(LEDPin2, LOW);
break;
case “THIRD BUTTON HERE”:
Serial.println(“pressed 2”);
analogWrite(MotorPWMPin, 255);
digitalWrite(MotorDirPin, LOW);
digitalWrite(LEDPin2, HIGH);
digitalWrite(LEDPin1, LOW);
break;
case “SECOND BUTTON HERE”:
Serial.println(“Off”);
// Turn motor off
digitalWrite(MotorPWMPin, LOW);
digitalWrite(MotorDirPin, LOW);
digitalWrite(LEDPin2, LOW);
digitalWrite(LEDPin1, LOW);
break;
}
irrecv.resume();
}

}

Explaining The Sketch:

The first bit of the sketch includes the IRremote library, so that we can use its functions throughout the sketch; it also dictates which pin the IR receiver is connected. Then we create instances of the irrecv and decode_results class.

#include
int IRPin = 10;
IRrecv irrecv(IRPin);
decode_results results;

Then we initialize the various I/O variables:

int LEDPin1 = 12;
int LEDPin2 = 11;
int MotorPWMPin = 3;
int MotorDirPin = 4;

Now we enter the setup() structure. First we set the I/O pins used in this challenge to inputs or outputs in this case all of the I/O’s are outputs. Next, serial communication is started. Then the IR receiver is enabled.

pinMode(LEDPin1, OUTPUT);
pinMode(LEDPin2, OUTPUT);
pinMode(MotorPWMPin, OUTPUT);
pinMode(MotorDirPin, OUTPUT);

Serial.begin(9600);

irrecv.enableIRIn();

Now we enter the loop() structure. You may notice a new conditional statement called a Switch Statement. This statement acts like a multi-If Statement, but it is easier to read. For example the first case executes when the results.value() is equal to your first buttons code (remember when I asked you to write the codes down from the Serial Monitor? You will need to put your codes where you see “FIRST BUTTON HERE”, ”THIRD BUTTON HERE”, and “SECOND BUTTON HERE” for example my Switch Statement looks like this:

switch (results.value) {
case 0x2FD9867:
Serial.println(“pressed 1”);
analogWrite(MotorPWMPin, 255);
digitalWrite(MotorDirPin, HIGH);
digitalWrite(LEDPin1, HIGH);
digitalWrite(LEDPin2, LOW);
break;
case 0x2FDB847:
Serial.println(“pressed 2”);
analogWrite(MotorPWMPin, 255);
digitalWrite(MotorDirPin, LOW);
digitalWrite(LEDPin2, HIGH);
digitalWrite(LEDPin1, LOW);
break;
case 0x2FD916E:
Serial.println(“Off”);
// Turn motor off
digitalWrite(MotorPWMPin, LOW);
digitalWrite(MotorDirPin, LOW);
digitalWrite(LEDPin2, LOW);
digitalWrite(LEDPin1, LOW);
break;

The last bit of code uses the recv.resume() function to tell the IR receiver to wait for the next button press.

irrecv.resume();

Well, there you have it another fun and exciting Arduino Adventure that allows you to incorporate wireless communication I hope you enjoyed your Adventure.

You can get the Sketch used for this bonus challenge HERE

——

Bonus Challenge 1

So I bet you may be looking back at challenge one and wondering how you can make this program better; well I thought of one, but I didn’t want to introduce it to you until after you got your feet wet with some Arduino programming fundamentals. Now that you have those skills we can go back and make challenge one’s code more user friendly. How? Easy I want to show you how you can make a push button only register once even if you hold down the button; this will make it easier to send the mapped potentiometer data to the Serial Monitor. Remember in the first challenge the data just keeps streaming into the Serial Monitor, so it is really hard to enter a code; this will make that task easier. In order to complete this project you need to use a potentiometer (like the one found in Challenge 1) and push-button (like the one found in Challenge 2). Figure 1 demonstrates how the potentiometer and push-button should be connected to the Arduino.

 photo Bonus_Challenge_01_Image_zps49a836fb.jpg

Everything connected? Good. Now we need to move onto the sketch. This sketch combines both Challenge 1 and Challenge 2, but with the added While Loop to dictate whether the push button is being held down or has been released. Listing 1 is the sketch for this bonus challenge.

Listing 1

// Create values to store the pins we are using
int buttonPin = 2;
int potPin = A0;
// Create variables that store the inputs values
int buttonValue = 0;
int potValue = 0;
// Create a variable that stores the mapped data of the potentiometer
int mappedPotVal = 0;

void setup()
{
// Start serial communication
Serial.begin(9600);
// Set up the pins to inputs
pinMode(buttonPin, INPUT);
pinMode(potPin, INPUT);
// Engage internal pull-up resistor at digital pin 2
digitalWrite(buttonPin, HIGH);
// Print a message to the serial port
Serial.println(“Enter The Password”);
}

void loop()
{
// Read the buttons value
buttonValue = digitalRead(buttonPin);
// Read the potentiometers value
potValue = analogRead(potPin);
// Set the potValue to a number 0 through 9 and store it in
// mappedPotVal.
mappedPotVal = map(potValue, 0, 1023, 0, 9);

if (buttonValue == 0)
{
// Print mappedPotVal value to the serial port
Serial.print(mappedPotVal);
// This while loop makes sure that nothin happens until
// the button is released and pressed again
while(buttonValue == 0)
{
// Keep reading buttonValue until the button is released
buttonValue = digitalRead(buttonPin);
// Do nothing as the button is being held down
}
}

delay(100);
}

What is going on here? Well the first bit of code you should be familiar with:

// Create values to store the pins we are using
int buttonPin = 2;
int potPin = A0;
// Create variables that store the inputs values
int buttonValue = 0;
int potValue = 0;
// Create a variable that stores the mapped data of the potentiometer
int mappedPotVal = 0;

This is where we are creating all of the variables we will use for this sketch. The next bit of code should be familiar as well:

void setup()
{
// Start serial communication
Serial.begin(9600);
// Set up the pins to inputs
pinMode(buttonPin, INPUT);
pinMode(potPin, INPUT);
// Engage internal pull-up resistor at digital pin 2
digitalWrite(buttonPin, HIGH);
// Print a message to the serial port
Serial.println(“Enter The Password”);
}

In the Setup Structure we start Serial communication with the Serial.begin(9600); command. Then we set both the potentiometer and push-button as an input. After that, we engage the pull-up resistor on digital pin 2. Finally, we print “Enter The Password” to the Serial Monitor. Again the next bit of code you have seen before:

// Read the buttons value
buttonValue = digitalRead(buttonPin);
// Read the potentiometers value
potValue = analogRead(potPin);
// Set the potValue to a number 0 through 9 and store it in
// mappedPotVal.
mappedPotVal = map(potValue, 0, 1023, 0, 9);

Remember this code? It looks like code from both Challenge 1 and Challenge 2. The first bit of code is setting the reading of buttonPin to buttonValue. The next part is setting the reading of potPin to potValue. Finally we use the same map() function we used in Challenge 1 to scale the potentiometers reading from 0 to 9 (instead of 0 to 1023). Now we get to the new stuff (well newer):

if (buttonValue == 0)
{
// Print mappedPotVal value to the serial port
Serial.print(mappedPotVal);
// This while loop makes sure that nothin happens until
// the button is released and pressed again
while(buttonValue == 0)
{
// Keep reading buttonValue until the button is released
buttonValue = digitalRead(buttonPin);
// Do nothing as the button is being held down
}
}

If buttonValue is being pressed then the Serial Monitor will display the value of the mappedPotVal, but instead of printing forever it will only print one value each time the push-button is pressed. How? Well once we enter the While Loop (if buttonValue is equal to 0 which means you are holding the button down) the sketch keeps reading the buttonValue until buttonValue is no longer equal to 0 (which means you have released the push-button). The last line of code is a delay for 100 miliseconds.

Ok Now upload this code to your Arduino and open the Serial Monitor, and you will see something like this (Figure 2).

 photo Bonus_Challenge_01_Image2_zpsc8814b02.jpg

If you move the potentiometer to a different position and press the push-button you will notice that the Serial monitor will update; keep a hold of the push-button and one value will update, but only one value.

 photo Bonus_Challenge_Image3_zps816d999d.jpg

That’s it! You now know how to read one value at a time on the Serial Monitor with a push-button. It is easy to see where this bit of code may be useful, for example; you may need a remote that only updates once every time a button is pressed. Think of other ways this may be useful and comment with your ideas below.

Get the Sketch HERE

2 thoughts on “Bonus Challenges

  1. David

    At the end of the void setup section I added the following line of code to get a carriage return after the Enter Password:
    Serial.print(‘\n’);

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s