- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经为我的 Remote 解码了我的 IR 代码,并让它们在草图中运行时工作,请参阅 IR 代码。
我的 RFID 猫喂食器草图也运行良好,请参阅 RFID 代码。
然而,我在将红外 Remote 与任何其他阻止功能一起使用时遇到了麻烦,特别是这个 RFID 阅读器草图……每个草图似乎都需要一直等待才能为信号做好准备。我想要做的是让 RFID 阅读器正常运行,但能够通过红外 Remote 将其置于手动模式。任何帮助或指示将不胜感激。我有尝试将两者集成的代码,但它从来没有正常工作,请告诉我是否有帮助,谢谢!
IR 代码
#include <IRremote.h>
int RECV_PIN = 4;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup(){
irrecv.enableIRIn(); // Start the receiver
Serial.begin(2400);
}
// Compare two tick values, returning 0 if newval is shorter,
// 1 if newval is equal, and 2 if newval is longer
// Use a tolerance of 20%
int compare(unsigned int oldval, unsigned int newval) {
if (newval < oldval * .8) {
return 0;
}
else if (oldval < newval * .8) {
return 2;
}
else {
return 1;
}
}
// Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param
#define FNV_PRIME_32 16777619
#define FNV_BASIS_32 2166136261
/* Converts the raw code values into a 32-bit hash code.
* Hopefully this code is unique for each button.
*/
unsigned long decodeHash(decode_results *results) {
unsigned long hash = FNV_BASIS_32;
for (int i = 1; i+2 < results->rawlen; i++) {
int value = compare(results->rawbuf[i], results->rawbuf[i+2]);
// Add value into the hash
hash = (hash * FNV_PRIME_32) ^ value;
}
return hash;
}
void loop() {
if (irrecv.decode(&results)) {
unsigned long hash = decodeHash(&results);
switch (hash) {
case 0x4AB0F7B6: // any button held down
Serial.println("Button Repeat");
break;
case 0x2D4AFCCE: // Power
Serial.println("Power");
break;
case 0xE8187BAA: // EQ
Serial.println("EQ");
break;
case 0x95D01E65: // CD/Tuner
Serial.println("CD/Tuner");
break;
case 0xE20BA29: // Mute
Serial.println("Mute");
break;
default:
Serial.print("Unknown ");
Serial.println(hash, HEX);
}
irrecv.resume(); // Resume decoding (necessary!)
}
}
RFID 代码
/*** Cord Slatton
* date 12-13-2012 **/
#include <IRremote.h>
#define RFID_ENABLE 2 //to RFID ENABLE
#define CODE_LEN 10 //Max length of RFID tag
#define START_BYTE 0x0A
#define STOP_BYTE 0x0D
#define MOTOR_RUN_LENGTH 1000 //time, in ms, to run door motor
char tag[CODE_LEN];
int pwm_b = 11; //PWM control for motor outputs 3 and 4 is on digital pin 11
int dir_b = 13; //direction control for motor outputs 3 and 4 is on digital pin 13
int door_status = 1; // 1 == closed, 0 == open
//PIR sensor
const int sensorPIR = A0;
unsigned long rfidHitTime = 0; //when did we get last rfid hit?
unsigned long pirHitTime = 0; //when did we get last PIR hit?
unsigned int pirClose = 0;
unsigned int rfidClose = 0;
void setup() {
Serial.begin(2400); //parallax rfid reader seems to only like serial 2400
Serial.println("RoboFeeder Online");
//rfid
pinMode(RFID_ENABLE,OUTPUT);
//motor
pinMode(pwm_b, OUTPUT);
pinMode(dir_b, OUTPUT);
//PIR
pinMode(sensorPIR, INPUT);
digitalWrite(sensorPIR, LOW);
Serial.println("Warming up PIR...");
delay(10000);
Serial.println("PIR ready");
Serial.println("RoboFeeder startup test: Door");
openDoor();
delay(2500);
closeDoor();
Serial.println("RoboFeeder door test complete");
}
void loop() {
delay(1500);
enableRFID();
getRFIDTag(); //waiting function, till gets any tag
disableRFID();
openDoor();
sendCode();
Serial.flush();
clearCode();
}
/*** Clears out the memory space for the tag to 0s. */
void clearCode() {
for(int i=0; i<CODE_LEN; i++) {
tag[i] = 0;
}
}
/*** Sends the tag to the computer. */
void sendCode() {
Serial.print("TAG:");
for(int i=0; i<CODE_LEN; i++) {
Serial.print(tag[i]);
}
Serial.println("\r");
}
/**************************************************************/
/******************** RFID Functions ***********************/
/**************************************************************/
void enableRFID() {
digitalWrite(RFID_ENABLE, LOW);
}
void disableRFID() {
digitalWrite(RFID_ENABLE, HIGH);
}
/*** Blocking function, waits for and gets the RFID tag. */
void getRFIDTag() {
byte next_byte;
while(Serial.available() <= 0) {
if(door_status == 0){ //open
pirDetection();
rfidClose = millis() - rfidHitTime;
pirClose = millis() - pirHitTime;
Serial.print("RFID Close: ");
Serial.print(rfidClose);
Serial.print(" - PIR Close: ");
Serial.println(pirClose);
//if door has been open for more than a certain amount of time with no rfid hit and PIR no hit for certain time
if( (rfidClose > 30000) && (pirClose > 10000) ){
closeDoor();
}
}
}
pirHitTime = 0;
if((next_byte = Serial.read()) == START_BYTE) {
byte bytesread = 0;
while(bytesread < CODE_LEN) {
if(Serial.available() > 0) { //wait for the next byte
if((next_byte = Serial.read()) == STOP_BYTE) break;
tag[bytesread++] = next_byte;
}
}
rfidHitTime = millis(); //keep track of when last rfid hit was
}
}
/******************** Motor Functions ***********************/
void openDoor() {
if(door_status == 1){
motorRun(150);
digitalWrite(dir_b, HIGH); //Set motor direction, 1 low, 2 high
Serial.println("Door opening");
delay(MOTOR_RUN_LENGTH);
motorStop();
door_status = 1 - door_status;
}
}
void closeDoor() {
if(door_status == 0){
motorRun(150);
digitalWrite(dir_b, LOW); //Reverse motor direction, 1 high, 2 low
Serial.println("Door closing");
delay(MOTOR_RUN_LENGTH);
motorStop();
door_status = 1 - door_status;
}
}
void motorRun(int speed) {
analogWrite(pwm_b, speed); //set motor to run at speed variable value
}
void motorStop() {
analogWrite(pwm_b, 0); //set motor to run at 0
}
/******************** PIR Functions ***********************/
boolean pirDetection() {
//returns true if PIR analog reading has been rising for more than 10 cycles, false if has been falling
int counter = 0;
int pirSensorReading[10];
int avgReading = 0;
while(counter < 10){
pirSensorReading[counter] = analogRead(sensorPIR);
avgReading += pirSensorReading[counter];
counter++;
delay(300);
}
avgReading = avgReading/10;
Serial.print("Avg. PIR: ");
Serial.print(avgReading);
Serial.print(" - Last PIR: ");
Serial.println( pirSensorReading[9] );
if( (pirSensorReading[9] > avgReading+1) || (analogRead(sensorPIR) > 700) ){
pirHitTime = millis(); //keep track of when last PIR hit was
return true;
}
else if(pirSensorReading[9] <= avgReading){
return false;
}
}
最佳答案
我能够使用 AttachInterrupt() 成功解决这个问题。
具体来说,我找到了一个 library for IR Remotes that is interrupt based而不是像我以前尝试使用的那样基于轮询。
如果有人感兴趣,成品 RFID IR 遥控猫喂食器代码是:
/*** Cord Slatton
* date 12-28-2012 **/
//IR includes
//info: http://www.zbotic.com/index.php/download/
//download: http://www.zbotic.com/index.php/download_file/view/12/118/
#include "PCInterrupt.h"
#include "Device.h"
#include "irController.h"
#define RFID_ENABLE 8 //to RFID ENABLE
#define CODE_LEN 10 //Max length of RFID tag
#define START_BYTE 0x0A
#define STOP_BYTE 0x0D
#define MOTOR_RUN_LENGTH 1000 //time, in ms, to run door motor
char tag[CODE_LEN];
int pwm_b = 11; //PWM control for motor outputs 3 and 4 is on digital pin 11
int dir_b = 13; //direction control for motor outputs 3 and 4 is on digital pin 13
int door_status = 1; // 1 == closed, 0 == open
int manual_mode = 1; // 1 == auto, 0 == manual
//PIR sensor
const int sensorPIR = A0;
//IR init
int manualModePin = 10; //led pin for indication of manual mode
IRController irController;
unsigned long rfidHitTime = 0; //when did we get last rfid hit?
unsigned long pirHitTime = 0; //when did we get last PIR hit?
unsigned long pirClose = 0;
unsigned long rfidClose = 0;
void setup() {
Serial.begin(2400); //parallax rfid reader seems to only like serial 2400
Serial.println("RoboFeeder Online");
//rfid
pinMode(RFID_ENABLE,OUTPUT);
//motor
pinMode(pwm_b, OUTPUT);
pinMode(dir_b, OUTPUT);
//PIR
pinMode(sensorPIR, INPUT);
digitalWrite(sensorPIR, LOW);
Serial.println("Warming up PIR...");
delay(10000);
Serial.println("PIR ready");
//IR remote
pinMode(manualModePin, OUTPUT);
digitalWrite(manualModePin, LOW);
int res = irController.begin(IR_PIN, OTHER_DEVICE);
if (res != SUCCESS) {
Serial.println("error=");
Serial.println(res);
}
Serial.println("IR Remote enabled");
Serial.println("RoboFeeder startup test: Door");
openDoor();
delay(2500);
closeDoor();
Serial.println("RoboFeeder door test complete");
}
void loop() {
delay(1500);
enableRFID();
getRFIDTag(); //waiting function, till gets any tag
disableRFID();
openDoor();
sendCode();
Serial.flush();
clearCode();
}
/*** Clears out the memory space for the tag to 0s. */
void clearCode() {
for(int i=0; i<CODE_LEN; i++) {
tag[i] = 0;
}
}
/*** Sends the tag to the computer. */
void sendCode() {
Serial.print("TAG:");
for(int i=0; i<CODE_LEN; i++) {
Serial.print(tag[i]);
}
Serial.println("\r");
}
/**************************************************************/
/******************** RFID Functions ***********************/
/**************************************************************/
void enableRFID() {
digitalWrite(RFID_ENABLE, LOW);
}
void disableRFID() {
digitalWrite(RFID_ENABLE, HIGH);
}
/*** Blocking function, waits for and gets the RFID tag. */
void getRFIDTag() {
byte next_byte;
while(Serial.available() <= 0) {
if(door_status == 0){ //open
pirDetection();
rfidClose = millis() - rfidHitTime;
pirClose = millis() - pirHitTime;
Serial.print("RFID Close: ");
Serial.print(rfidClose);
Serial.print(" - PIR Close: ");
Serial.println(pirClose);
//if door has been open for more than a certain amount of time with no rfid hit and PIR no hit for certain time
if( (rfidClose > 40000) && (pirClose > 15000) ){
closeDoor();
}
}
IRRemote();
if(manual_mode == 0)
manualMode();
}
//PIR set to current loop time to keep it from freaking out
pirHitTime = millis();
if((next_byte = Serial.read()) == START_BYTE) {
byte bytesread = 0;
while(bytesread < CODE_LEN) {
if(Serial.available() > 0) { //wait for the next byte
if((next_byte = Serial.read()) == STOP_BYTE) break;
tag[bytesread++] = next_byte;
}
}
rfidHitTime = millis(); //keep track of when last rfid hit was
}
}
/**************************************************************/
/******************** Motor Functions ***********************/
/**************************************************************/
void openDoor() {
if(door_status == 1){
motorRun(150);
digitalWrite(dir_b, HIGH); //Set motor direction, 1 low, 2 high
Serial.println("Door opening");
delay(MOTOR_RUN_LENGTH);
motorStop();
door_status = 1 - door_status;
}
}
void closeDoor() {
if(door_status == 0){
motorRun(150);
digitalWrite(dir_b, LOW); //Reverse motor direction, 1 high, 2 low
Serial.println("Door closing");
delay(MOTOR_RUN_LENGTH);
motorStop();
door_status = 1 - door_status;
}
}
void motorRun(int speed) {
analogWrite(pwm_b, speed); //set motor to run at speed variable value
}
void motorStop() {
analogWrite(pwm_b, 0); //set motor to run at 0
}
/**************************************************************/
/******************** PIR Functions ***********************/
/**************************************************************/
boolean pirDetection() {
//returns true if PIR analog reading has been rising for more than 10 cycles,
//false if has been falling
int counter = 0;
int pirSensorReading[10];
int avgReading = 0;
while(counter < 10){
pirSensorReading[counter] = analogRead(sensorPIR);
avgReading += pirSensorReading[counter];
counter++;
delay(300);
}
avgReading = avgReading/10;
Serial.print("Avg. PIR: ");
Serial.print(avgReading);
Serial.print(" - Last PIR: ");
Serial.println( pirSensorReading[9] );
if( (pirSensorReading[9] > avgReading+1) || (analogRead(sensorPIR) > 700) ){
pirHitTime = millis(); //keep track of when last PIR hit was
return true;
}
else if(pirSensorReading[9] <= avgReading){
return false;
}
}
/**************************************************************/
/******************** IR Remote Functions ***********************/
/**************************************************************/
void IRRemote(){
int val = irController.read(); // Returns negative value if no new command rcvd
if (val >=0){
Serial.print("Key: ");
Serial.println(val);
if(val == 100){
manual_mode = 1 - manual_mode;
}
else if( (val >= 0) && (manual_mode == 0) ){
if(door_status == 1)
openDoor();
else
closeDoor();
}
}
delay(500);
}
//blocking function that won't let you leave till you turn off manual mode
void manualMode(){
disableRFID();
Serial.println("Manual Mode on");
digitalWrite(manualModePin, HIGH);
while(manual_mode == 0){
IRRemote();
}
Serial.println("Manual Mode exiting");
digitalWrite(manualModePin, LOW);
//set sensors to count from now, esentially zero them out
pirHitTime = millis();
rfidHitTime = millis();
enableRFID();
}
关于c - Arduino 红外 Remote 与其他阻挡功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13904268/
有什么区别 git ls-remote --heads origin 和 git ls-remote . "refs/remotes/*" ? --heads origin 版本需要一段时间才能运行,
当我执行 git remote -v 时,我得到了 origin git@xxx.yyy.cc:name fetch) origin git@xxx.yyy.cc:name (push) 为什么
要构建我的远程环境,我需要设置几个环境变量(它们在 docker-compose 文件中使用)。这些是在我的 ZSH 环境中设置的,因此从终端运行 docker-compose build 可以按预期
我有一个创建多个 Docker 镜像的项目。我想在 vscode-remote 中为每个图像设置一个 devcontainer,以便我可以为每个图像启动一个容器。 我一次只需要启动并连接到一个容器/图
我设置了以下 Remote : $ git remote korg rorg 以及以下分支: $ git branch -a * (no branch) remotes/korg/gingerbr
这是我得到的错误。我有一个简单的 .Net Remoting 设置。连接空闲几分钟后出现此错误。 对象“/7966b546_734e_410d_817a_a0e701f114f6/lfxehvbczn
我有两个远程 git 分支(称它们为“RemoteA”和“RemoteB”)。 “RemoteB”在过去的某个时候从“RemoteA”中分离出来。在那之后,许多提交已应用于两个分支,但我现在希望“Re
我知道使用下面的 git 命令我可以列出我所有的“远程”分支... [someone@someone-pc somerepo]$ git ls-remote Username for 'https:/
我最需要的是一些建议。 我有以下示例: 应用程序1.exe - 有对象 MyList 应用程序2.exe - 需要访问 MyList 并与对象交互,就好像它是从 Application2 创建的一样。
我有一个大型应用程序,它通过 .net 远程处理使用 COM 从 Web 层调用到中间层。在此模式下启动和运行非常慢。 COM 边界的两边都是我们的代码。 我希望能够(可选)在单个进程中运行它。相当多
假设我有两个 Remote :origin 和 upstream。有没有一种方法可以将对象 upstream 推送到 origin,以便其他从 origin 克隆我的 repo 的人能够运行像 git
简而言之; 如何删除远程多个 merge 的 Remote ? 更多背景; 我有一个 git 存储库,其中包含数十个已 merge 到 master 中的 Remote 。我可以使用以下方法一次删除这
我有一个仓库的本地 git clone。我当然可以使用 git remote 查看我的本地远程 origin。但是,我可以看到我的 Remote origin 的 Remote 吗?我可以查看有关这些
先决条件 - 远程服务器和客户端均已设置,客户端能够通过客户端激活的业务对象在服务器端调用方法并从服务器端接收事件。 在保持服务器和客户端运行的情况下更改远程服务器的IP地址,客户端仍然可以执行上述任
我正在设置一个新的远程主机,每次启动它时,我都会收到以下错误输出:有关如何解决此问题的任何反馈或指示? Pseudo-terminal will not be allocated because st
我只是不明白 Git 的帮助页面。那么会发生什么或有什么区别? 假设我有一个带有子模块 B 的 Git 项目 A。子模块 B 确实有一个子模块 C。克隆存储库后,A 指向 B 的特定提交。B 指向 C
我在 Java 中使用 rmi。但是有一个 ExportException“远程对象实现非法远程接口(interface)”。 这是我的代码,有人可以帮帮我吗? public interface Re
我对 git 越来越熟悉了,但是我遇到了一些让我困惑的事情。我们在这里使用本地镜像,因为我们的线路很慢。为了指向镜子,我在 .gitconfig 中有一些常用的东西。 当我想推送时,问题就来了。看来我
我设置了一个带有 conan“远程”存储库的 jfrog artifactory 来代理官方 conan-center 存储库。我看到三个不同的 conan-center URL,具体取决于我阅读的来
我无法使用 VSCode 远程 SSH 在我的远程服务器上保存任何文件,因为我不是 root 用户。 我关注了 official documentation关于如何使用 SSH 配置文件设置 ssh
我是一名优秀的程序员,十分优秀!