Lesson 2

User Feedback

Now we are experts on reading RFID cards, the only thing we have to do now is tell the motor to open the door when we show the right card.

WRONG!

Electronic devices use all kinds of different feedback methods to let the user know their action has been successful, unsuccessful, or even just that the device acknowledges the user did something.

Common ways to provide this feedback are through lights and sounds, we're going to use both!

Using an addressable LED strip

The provided LED strip has 3 pins you need to connect to your Arduino.

The GND pin needs to connect to your Arduino GND.

The V+ or 5V pin needs to connect to your Arduino 5v output pin.

The D or Data pin needs to connect to a digital pin on your Arduino, in this case we'll use pin 6.

LED Code

First you have to make sure you have downloaded the FastLED library and put it in your Documents/Arduino/libraries folder.

#include "FastLED.h"

#define LEDPIN 6 	// Set the pin that the LED is connected to
#define LEDCOUNT 1 	// Set the number of LEDs we have in the strip

CRGB leds[LEDCOUNT];	// Create a variable called "leds" to store all our LED info

void setup(){
	FastLED.addLeds<WS2812B, PIN, RGB>(leds, LEDCOUNT); // Initiate the LED code
	FastLED.setBrightness(50);	// Set the brightness of the LEDs
}

void loop(){
	leds[0] = CRGB(255,0,0);	// Set led 0 to 100% red, 0% green and 0% blue.
	FastLED.show();			// Update all the LEDs to the new colour we set
	delay(1000);

	leds[0] = CRGB(0,255,0);
	FastLED.show();
	delay(1000);

	leds[0] = CRGB(0,0,255);
	FastLED.show();
	delay(1000);
}

Sound Feedback

Now that we can give visual feedback lets add sound.

The speaker is very easy to wire up with only two pins, one goes to GND and the other goes to pin 5 on our Arduino.

Code

#include "FastLED.h"

#define LEDPIN 6 	// Set the pin that the LED is connected to
#define LEDCOUNT 1 	// Set the number of LEDs we have in the strip

#define noteA3 220
#define noteB3 247
#define noteC4 262
#define noteD4 293
#define noteE4 330
#define noteF4 349
#define noteG4 392
#define noteA4 440
#define noteB4 494
#define noteC5 523
#define noteD5 587
#define noteE5 659
#define noteG5 784

int melody[] = {
	noteC5, noteC4, noteA3
};

int melodyTiming = {
	8, 8, 4
}

CRGB leds[LEDCOUNT];	// Create a variable called "leds" to store all our LED info

void setup(){
	FastLED.addLeds<WS2812B, PIN, RGB>(leds, LEDCOUNT); // Initiate the LED code
	FastLED.setBrightness(50);	// Set the brightness of the LEDs
}

void loop(){
	PlayMelody();
	delay(2000);
}

// This function runs through the notes in melody[]
void PlayMelody(){
	for (int thisNote = 0; thisNote < 3; thisNote++) {
		int noteDuration = 1000 / melodyTiming[thisNote];
		tone(5, melody[thisNote], noteDuration);
		int pauseBetweenNotes = noteDuration * 1.30;
		delay(pauseBetweenNotes);
		noTone(5);
	}
}


Homework