
Setting Up
Take note to setup the pinMode before using the component connected to the Arduino pin.
PIN | Component | Remarks |
---|---|---|
13 | Arduino LED | OUTPUT |
4 | Blue LED | OUTPUT |
6 | Buzzer | OUTPUT |
7 | Push Button | INPUT |
8 | RGB | |
3 | IR LED | OUTPUT |
Blinking the LED
void setup() { pinMode(13, OUTPUT); //Arduino LED pinMode(4, OUTPUT); //Blue LED } void loop() { digitalWrite(13,HIGH); //turn on the Arduino LED digitalWrite(4,HIGH); //turn on the Blue LED delay(150); digitalWrite(13,LOW); //turn off the Arduino LED digitalWrite(4,LOW); //turn off the Blue LED delay(150); }
Sounding the Buzzer
int I; void setup() { pinMode(6, OUTPUT); //Buzzer //beep once for (I = 0; I < 2000; I++) { digitalWrite(6,HIGH); //turn on the Blue LED delayMicroseconds(250); digitalWrite(6,LOW); //turn off the Arduino LED delayMicroseconds(250); } } void loop() { }
Making a shooting sound
int I; void setup() { pinMode(6, OUTPUT); //Buzzer //Shooting sound? for (I = 150; I < 400; I++) { digitalWrite(6,HIGH); //turn on the Blue LED delayMicroseconds(I); digitalWrite(6,LOW); //turn off the Arduino LED delayMicroseconds(I); } } void loop() { }
Using the RGB
When using the RGB, include the Adafruit NeoPixel library in your code
Example:
#include <Adafruit_NeoPixel.h> #define PIN 8 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800); #define RGB_Display(R,G,B) \ pixels.setPixelColor(0, pixels.Color(R,G,B)); \ pixels.show(); void setup() { pixels.begin(); pixels.show(); // Initialize all pixels to 'off' RGB_Display(100,0,0); //Red delay(250); RGB_Display(0,150,0); //Green delay(250); RGB_Display(0,0,80); //Blue delay(250); RGB_Display(0,0,0); //Turn the RGB off } void loop() { }
Getting input from the push button
void setup() { pinMode(7, INPUT); //push button Serial.begin(9600); } void loop() { if ( digitalRead(7) == 0) { Serial.println(“Button Pressed.”); while (digitalRead(7) == 0) { //wait for the user to release the push button } } }
Using the IR LED


When using the IR LED, include the IRLib library into your code.
#include <IRLib> IRsend My_Sender; void setup() { pinMode(7, INPUT); //push button pinMode(13, OUTPUT); //LED Serial.begin(9600); } void loop() { if ( digitalRead(7) == 0) { digitalWrite(13, HIGH); My_Sender.send(NEC,97, 8); delay(150); digitalWrite(13, LOW); while (digitalRead(7) == 0) { //wait for the user to release the push button } delay(100); } }
Serial Communication – Getting data
int RxData; void loop() { RxData = Serial.read() ; if (RxData != -1) { //not equal to if (RxData == 97) { //”a” //turn on LED digitalWrite(13, HIGH); } if (RxData == 98) { //”b” //turn off LED digitalWrite(13, LOW); } }