ברוכים הבאים לשיעור!
השיעור מורכב מסדרה של סרטונים, צפו בהם לפי הסדר.
תהנו 🙂
הקוד מסוף השיעור הקודם:
//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";
void setup() {
//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()
{
// 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);
}
}
}
}
פתרון:
//IR
#include <IRremote.h>
IRsend irsend;
//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";
void setup() {
//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);
}
}
}
}
קוד עם הנחיות לביצוע המשימה:
//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";
void setup() {
//FastLED
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(50);
//Write 1
leds[1] = CRGB::Green;
FastLED.show();
delay(4000);
//Delete 1 before writing 2
leds[1] = CRGB::Black;
//Write 2
FastLED.show();
delay(4000);
//Delete 2 before writing 3
//Write 3
FastLED.show();
delay(4000);
//Delete 3
//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);
}
}
}
}
פתרון:
//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";
void setup() {
//FastLED
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(50);
//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();
delay(4000);
//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();
delay(4000);
//Turn off 3 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();
delay(4000);
//Turn off 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[27] = CRGB::Black;
leds[28] = CRGB::Black;
leds[37] = CRGB::Black;
leds[45] = CRGB::Black;
leds[49] = CRGB::Black;
leds[53] = CRGB::Black;
leds[58] = CRGB::Black;
leds[59] = CRGB::Black;
leds[60] = CRGB::Black;
FastLED.show();
//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);
}
}
}
}
//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();
}
}
}
}