ברוכים הבאים לשיעור!
השיעור מורכב מסדרה של סרטונים, צפו בהם לפי הסדר.
תהנו 🙂
העתיקו את הקוד מקופסה זו:
/*
* IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
* An IR detector/demodulator must be connected to the input RECV_PIN.
* Version 0.1 July, 2009
* Copyright 2009 Ken Shirriff
* http://arcfn.com
*/
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
// In case the interrupt driver crashes on setup, give a clue
// to the user what's going on.
Serial.println("Enabling IRin");
irrecv.enableIRIn(); // Start the receiver
Serial.println("Enabled IRin");
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
delay(100);
}
העתיקו את הקוד מקופסה זו:
/*
* IRremote: IRsendDemo - demonstrates sending IR codes with IRsend
* An IR LED must be connected to Arduino PWM pin 3.
* Version 0.1 July, 2009
* Copyright 2009 Ken Shirriff
* http://arcfn.com
*/
#include <IRremote.h>
IRsend irsend;
void setup()
{
}
void loop() {
for (int i = 0; i < 3; i++)
{
irsend.sendSony(0x9999, 16);
delay(40);
}
delay(1000); //5 second delay between each signal burst
}
הקוד שעברתי עליו בסרטון:
#include <IRremote.h>
IRsend irsend;
void setup()
{
pinMode(0, OUTPUT);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
}
void loop() {
for (int j = 0; j < 51; j++)
{
for (int i = 0; i < 1; i++) {
//read code type pins
int code_type1 = digitalRead(3);
int code_type2 = digitalRead(4);
if ((code_type1==1)&&(code_type2==1))
irsend.sendSony(0x9999, 16);
if ((code_type1==0)&&(code_type2==0))
irsend.sendSony(0x8888, 16);
if ((code_type1==1)&&(code_type2==0))
irsend.sendSony(0x7777, 16);
if ((code_type1==0)&&(code_type2==1))
irsend.sendSony(0x6666, 16);
}
if (j%50==0)
{
digitalWrite(0, HIGH);
delay(20); // wait
digitalWrite(0, LOW);
}
else
delay(20); // wait
}
}
פתרון המשימה:
/*
* IRremoteESP8266: IRrecvDemo - demonstrates receiving IR codes with IRrecv
* This is very simple teaching code to show you how to use the library.
* If you are trying to decode your Infra-Red remote(s) for later replay,
* use the IRrecvDumpV2.ino (or later) example code instead of this.
* An IR detector/demodulator must be connected to the input kRecvPin.
* Copyright 2009 Ken Shirriff, http://arcfn.com
* Example circuit diagram:
* https://github.com/crankyoldgit/IRremoteESP8266/wiki#ir-receiving
* Changes:
* Version 0.2 June, 2017
* Changed GPIO pin to the same as other examples.
* Used our own method for printing a uint64_t.
* Changed the baud rate to 115200.
* Version 0.1 Sept, 2015
* Based on Ken Shirriff's IrsendDemo Version 0.1 July, 2009
*/
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
#define MOTOR_PIN_AI1 5
#define MOTOR_PIN_AI2 2
#define MOTOR_PIN_SPEED 4
#define SPEED_CHANNEL 5
#define PWM_FREQ 20000
#define SPEED_RESOLUTION 8
//ESP32Servo
#include <ESP32Servo.h>
Servo myservo;
#define SERVO_PIN 32
// An IR detector/demodulator is connected to GPIO pin 14(D5 on a NodeMCU
// board).
// Note: GPIO 16 won't work on the ESP8266 as it does not have interrupts.
const uint16_t kRecvPin = 34;
IRrecv irrecv(kRecvPin);
decode_results results;
void setup() {
Serial.begin(115200);
irrecv.enableIRIn(); // Start the receiver
while (!Serial) // Wait for the serial connection to be establised.
delay(50);
Serial.println();
Serial.print("IRrecvDemo is now running and waiting for IR message on Pin ");
Serial.println(kRecvPin);
//Motor Setup
pinMode(MOTOR_PIN_AI1, OUTPUT);
pinMode(MOTOR_PIN_AI2, OUTPUT);
ledcSetup(SPEED_CHANNEL, PWM_FREQ, SPEED_RESOLUTION);
ledcAttachPin(MOTOR_PIN_SPEED, SPEED_CHANNEL);
//Servo Setup
myservo.setPeriodHertz(50);
myservo.attach(SERVO_PIN, 500, 2400);
myservo.write(90);
}
void loop() {
if (irrecv.decode(&results)) {
// print() & println() can't handle printing long longs. (uint64_t)
// serialPrintUint64(results.value, HEX);
if (results.value == 0X9999)
{
Serial.println("Yes! we got 9999");
//Drive forward
digitalWrite(MOTOR_PIN_AI1, LOW);
digitalWrite(MOTOR_PIN_AI2, HIGH);
//set speed
ledcWrite(SPEED_CHANNEL, 50);
delay(1000);
//Stop
digitalWrite(MOTOR_PIN_AI1, LOW);
digitalWrite(MOTOR_PIN_AI2, LOW);
}
if (results.value == 0X8888)
{
Serial.println("Yes! we got 8888");
//Drive backward
digitalWrite(MOTOR_PIN_AI1, HIGH);
digitalWrite(MOTOR_PIN_AI2, LOW);
//set speed
ledcWrite(SPEED_CHANNEL, 50);
delay(1000);
//Stop
digitalWrite(MOTOR_PIN_AI1, LOW);
digitalWrite(MOTOR_PIN_AI2, LOW);
}
if (results.value == 0X7777)
{
Serial.println("Yes! we got 7777");
//Move servo to 70
myservo.write(70);
}
if (results.value == 0X6666)
{
Serial.println("Yes! we got 6666");
//Move servo to 110
myservo.write(110);
}
irrecv.resume(); // Receive the next value
}
delay(5);
}