ברוכים הבאים לשיעור!
השיעור מורכב מסדרה של סרטונים, צפו בהם לפי הסדר.
תהנו 🙂
הקוד מסוף השיעור הקודם:
//IR
#include <IRremote.h>
IRsend irsend;
//FAST LED
#include <FastLED.h>
#define NUM_LEDS 64
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
//Green -> A0
//Yellow -> A1
//Red -> A2
// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin = 9;
// Create a servo object
Servo Servo1;
// Radio config
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00003";
int lap_counter=0;
void setup() {
//FastLED
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(50);
//Radio
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
Serial.println("Listening");
//Servo Config
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
//Config leds pin
pinMode(A0,OUTPUT);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
// Blink Begin
//Turn on
digitalWrite(A0,HIGH);
digitalWrite(A1,HIGH);
digitalWrite(A2,HIGH);
//wait for one second
delay(1000);
//Turn off
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
//Blink END
//wait another second
delay(1000);
//Turn on red
digitalWrite(A2,HIGH);
//Close the gate
for(int i=80;i<175;i++)
{
Servo1.write(i);
delay(50);
}
}
void loop()
{
//Send IR
irsend.sendSony(0x9999, 16);
delay(20);
// put your main code here, to run repeatedly:
if (radio.available())
{
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text);
if (text[0] == 'S')
{
//Slow open
for(int i=175;i>80;i--)
{
Servo1.write(i);
delay(50);
}
}
if (text[0] == 'F')
{
//Slow open
for(int i=80;i<175;i++)
{
Servo1.write(i);
delay(50);
}
}
if ( (text[0] == 'L') || (text[0] == 'B') )
{
lap_counter++;
if (lap_counter==1)
{
//Write 1
leds[4] = CRGB::Red;
leds[11] = CRGB::Red;
leds[12] = CRGB::Red;
leds[20] = CRGB::Red;
leds[28] = CRGB::Red;
leds[36] = CRGB::Red;
leds[44] = CRGB::Red;
leds[52] = CRGB::Red;
leds[59] = CRGB::Red;
leds[60] = CRGB::Red;
leds[61] = CRGB::Red;
FastLED.show();
}
if (lap_counter==2)
{
//Turn off 1 before writing 2
leds[4] = CRGB::Black;
leds[11] = CRGB::Black;
leds[12] = CRGB::Black;
leds[20] = CRGB::Black;
leds[28] = CRGB::Black;
leds[36] = CRGB::Black;
leds[44] = CRGB::Black;
leds[52] = CRGB::Black;
leds[59] = CRGB::Black;
leds[60] = CRGB::Black;
leds[61] = CRGB::Black;
FastLED.show();
//Write 2
leds[2] = CRGB::Red;
leds[3] = CRGB::Red;
leds[4] = CRGB::Red;
leds[9] = CRGB::Red;
leds[13] = CRGB::Red;
leds[21] = CRGB::Red;
leds[29] = CRGB::Red;
leds[36] = CRGB::Red;
leds[43] = CRGB::Red;
leds[50] = CRGB::Red;
leds[57] = CRGB::Red;
leds[58] = CRGB::Red;
leds[59] = CRGB::Red;
leds[60] = CRGB::Red;
leds[61] = CRGB::Red;
FastLED.show();
}
if (lap_counter==3)
{
//Turn off 2 before writing 3
leds[2] = CRGB::Black;
leds[3] = CRGB::Black;
leds[4] = CRGB::Black;
leds[9] = CRGB::Black;
leds[13] = CRGB::Black;
leds[21] = CRGB::Black;
leds[29] = CRGB::Black;
leds[36] = CRGB::Black;
leds[43] = CRGB::Black;
leds[50] = CRGB::Black;
leds[57] = CRGB::Black;
leds[58] = CRGB::Black;
leds[59] = CRGB::Black;
leds[60] = CRGB::Black;
leds[61] = CRGB::Black;
FastLED.show();
//Write 3
leds[2] = CRGB::Red;
leds[3] = CRGB::Red;
leds[4] = CRGB::Red;
leds[9] = CRGB::Red;
leds[13] = CRGB::Red;
leds[21] = CRGB::Red;
leds[27] = CRGB::Red;
leds[28] = CRGB::Red;
leds[37] = CRGB::Red;
leds[45] = CRGB::Red;
leds[49] = CRGB::Red;
leds[53] = CRGB::Red;
leds[58] = CRGB::Red;
leds[59] = CRGB::Red;
leds[60] = CRGB::Red;
FastLED.show();
}
}
}
}
הקוד של הפונקציה:
void turn_off_matrix()
{
for (int i=0;i<64;i++)
{
leds[i] = CRGB::Black;
}
FastLED.show();
}
קוד נגן לפני הsetup
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
קוד נגן בתוך הsetup
mySoftwareSerial.begin(9600);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
while (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(10); //Set volume value. From 0 to 30
myDFPlayer.play(1); //Play the first mp3
רשימת הקבצים:
קוד עם הנחיות לביצוע המשימה:
//IR
#include <IRremote.h>
IRsend irsend;
//FAST LED
#include <FastLED.h>
#define NUM_LEDS 64
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
//Green -> A0
//Yellow -> A1
//Red -> A2
// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin = 9;
// Create a servo object
Servo Servo1;
// Radio config
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00003";
int lap_counter=0;
int calc(int a, int b)
{
int c = a+b;
return c;
}
void turn_off_matrix()
{
for (int i=0;i<64;i++)
{
leds[i] = CRGB::Black;
}
FastLED.show();
}
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(6, 4); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup() {
Serial.begin(9600);
//MP3 player
mySoftwareSerial.begin(9600);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
while (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(20); //Set volume value. From 0 to 30
myDFPlayer.play(4); //Play the first mp3
//FastLED
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(50);
//Radio
int result = calc(5,9);
Serial.println(result);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
Serial.println("Listening");
//Servo Config
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
//Config leds pin
pinMode(A0,OUTPUT);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
// Blink Begin
//Turn on
digitalWrite(A0,HIGH);
digitalWrite(A1,HIGH);
digitalWrite(A2,HIGH);
//Turn on LED matrix
for(int i=0;i<64;i++)
{
leds[i] = CRGB::Green;
FastLED.show();
delay(20);
}
//wait for one second
delay(1000);
//Turn off
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
//Turn on LED matrix
for(int i=0;i<64;i++)
{
leds[i] = CRGB::Black;
FastLED.show();
delay(20);
}
//Blink END
//Close the gate
for(int i=80;i<175;i++)
{
Servo1.write(i);
delay(50);
}
}
void loop()
{
//Send IR
irsend.sendSony(0x9999, 16);
delay(20);
// put your main code here, to run repeatedly:
if (radio.available())
{
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text);
if (text[0] == 'S')
{
//RACE BEGIN
//Play 3..2..1
myDFPlayer.play(4);
//Traffic light Yellow
//Delay
//Traffic light Green
//Fast open
Servo1.write(80);
}
if (text[0] == 'F')
{
//Slow open
for(int i=80;i<175;i++)
{
Servo1.write(i);
delay(50);
}
}
if ( (text[0] == 'L') || (text[0] == 'B') )
{
if (text[0] == 'L')
// play best lap music
lap_counter++;
if (lap_counter==1)
{
//Write 1
leds[4] = CRGB::Red;
leds[11] = CRGB::Red;
leds[12] = CRGB::Red;
leds[20] = CRGB::Red;
leds[28] = CRGB::Red;
leds[36] = CRGB::Red;
leds[44] = CRGB::Red;
leds[52] = CRGB::Red;
leds[59] = CRGB::Red;
leds[60] = CRGB::Red;
leds[61] = CRGB::Red;
FastLED.show();
}
if (lap_counter==2)
{
//Turn off 1 before writing 2
turn_off_matrix();
//Write 2
leds[2] = CRGB::Red;
leds[3] = CRGB::Red;
leds[4] = CRGB::Red;
leds[9] = CRGB::Red;
leds[13] = CRGB::Red;
leds[21] = CRGB::Red;
leds[29] = CRGB::Red;
leds[36] = CRGB::Red;
leds[43] = CRGB::Red;
leds[50] = CRGB::Red;
leds[57] = CRGB::Red;
leds[58] = CRGB::Red;
leds[59] = CRGB::Red;
leds[60] = CRGB::Red;
leds[61] = CRGB::Red;
FastLED.show();
}
if (lap_counter==3)
{
turn_off_matrix();
//Write 3
leds[2] = CRGB::Red;
leds[3] = CRGB::Red;
leds[4] = CRGB::Red;
leds[9] = CRGB::Red;
leds[13] = CRGB::Red;
leds[21] = CRGB::Red;
leds[27] = CRGB::Red;
leds[28] = CRGB::Red;
leds[37] = CRGB::Red;
leds[45] = CRGB::Red;
leds[49] = CRGB::Red;
leds[53] = CRGB::Red;
leds[58] = CRGB::Red;
leds[59] = CRGB::Red;
leds[60] = CRGB::Red;
FastLED.show();
}
if (lap_counter==4)
{
//Game over
//Play Game over music
//Traffic light RED
//Close gate slowly
}
}
}
}
פתרון המשימה:
//IR
#include <IRremote.h>
IRsend irsend;
//FAST LED
#include <FastLED.h>
#define NUM_LEDS 64
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
//Green -> A0
//Yellow -> A1
//Red -> A2
// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin = 9;
// Create a servo object
Servo Servo1;
// Radio config
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00003";
int lap_counter=0;
int calc(int a, int b)
{
int c = a+b;
return c;
}
void turn_off_matrix()
{
for (int i=0;i<64;i++)
{
leds[i] = CRGB::Black;
}
FastLED.show();
}
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(6, 4); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup() {
Serial.begin(9600);
//MP3 player
mySoftwareSerial.begin(9600);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
while (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(20); //Set volume value. From 0 to 30
myDFPlayer.play(4); //Play the first mp3
//FastLED
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(50);
//Radio
int result = calc(5,9);
Serial.println(result);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
Serial.println("Listening");
//Servo Config
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
//Config leds pin
pinMode(A0,OUTPUT);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
// Blink Begin
//Turn on
digitalWrite(A0,HIGH);
digitalWrite(A1,HIGH);
digitalWrite(A2,HIGH);
//Turn on LED matrix
for(int i=0;i<64;i++)
{
leds[i] = CRGB::Green;
FastLED.show();
delay(10);
}
//wait for one second
delay(1000);
//Turn off
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,HIGH);
//Turn OFF LED matrix
turn_off_matrix();
//Blink END
//Close the gate
for(int i=80;i<175;i++)
{
Servo1.write(i);
delay(50);
}
}
void loop()
{
//Send IR
irsend.sendSony(0x9999, 16);
delay(20);
// put your main code here, to run repeatedly:
if (radio.available())
{
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text);
if (text[0] == 'S')
{
//RACE BEGIN
//Play 3..2..1
myDFPlayer.play(2);
delay(1500);
//Traffic light Yellow
digitalWrite(A0,LOW);
digitalWrite(A1,HIGH);
digitalWrite(A2,LOW);
delay(1500);
//Traffic light Green
digitalWrite(A0,HIGH);
digitalWrite(A1,LOW);
//Fast open
Servo1.write(80);
}
if (text[0] == 'F')
{
//Slow close
for(int i=80;i<175;i++)
{
Servo1.write(i);
delay(50);
}
}
if ( (text[0] == 'L') || (text[0] == 'B') )
{
if (text[0] == 'L')
myDFPlayer.play(5);
lap_counter++;
if (lap_counter==1)
{
//Write 1
leds[4] = CRGB::Red;
leds[11] = CRGB::Red;
leds[12] = CRGB::Red;
leds[20] = CRGB::Red;
leds[28] = CRGB::Red;
leds[36] = CRGB::Red;
leds[44] = CRGB::Red;
leds[52] = CRGB::Red;
leds[59] = CRGB::Red;
leds[60] = CRGB::Red;
leds[61] = CRGB::Red;
FastLED.show();
}
if (lap_counter==2)
{
//Turn off 1 before writing 2
turn_off_matrix();
//Write 2
leds[2] = CRGB::Red;
leds[3] = CRGB::Red;
leds[4] = CRGB::Red;
leds[9] = CRGB::Red;
leds[13] = CRGB::Red;
leds[21] = CRGB::Red;
leds[29] = CRGB::Red;
leds[36] = CRGB::Red;
leds[43] = CRGB::Red;
leds[50] = CRGB::Red;
leds[57] = CRGB::Red;
leds[58] = CRGB::Red;
leds[59] = CRGB::Red;
leds[60] = CRGB::Red;
leds[61] = CRGB::Red;
FastLED.show();
}
if (lap_counter==3)
{
turn_off_matrix();
//Write 3
leds[2] = CRGB::Red;
leds[3] = CRGB::Red;
leds[4] = CRGB::Red;
leds[9] = CRGB::Red;
leds[13] = CRGB::Red;
leds[21] = CRGB::Red;
leds[27] = CRGB::Red;
leds[28] = CRGB::Red;
leds[37] = CRGB::Red;
leds[45] = CRGB::Red;
leds[49] = CRGB::Red;
leds[53] = CRGB::Red;
leds[58] = CRGB::Red;
leds[59] = CRGB::Red;
leds[60] = CRGB::Red;
FastLED.show();
}
if (lap_counter==4)
{
//Game over
//Play Game over music
myDFPlayer.play(7);
//Traffic light RED
digitalWrite(A0,LOW);
digitalWrite(A2,HIGH);
//LCD Animation
//Turn on LED matrix
for(int i=0;i<64;i++)
{
leds[i] = CRGB::Red;
FastLED.show();
delay(10);
}
turn_off_matrix();
for(int i=0;i<64;i++)
{
leds[i] = CRGB::Red;
FastLED.show();
delay(10);
}
turn_off_matrix();
for(int i=0;i<64;i++)
{
leds[i] = CRGB::Red;
FastLED.show();
delay(10);
}
turn_off_matrix();
//Close gate slowly
//Slow close
for(int i=80;i<175;i++)
{
Servo1.write(i);
delay(50);
}
}
}
}
}