我正在尝试使用3个或更多Arduino Uno和1个Mega(作为主设备)来构建电梯。
Arduino Uno(从站):
我已将红外传感器,按钮,LED和7段显示器连接到Uno。
如果我按下按钮,LED将会点亮并保持点亮状态,直到红外传感器检测到电梯轿厢为止。然后,LED指示灯将熄灭,并且7段显示器将显示楼层编号。
Arduino Mega(Master):
主机用于步进电机和键盘。
主站要做的是询问从站(在这种情况下是Uno的)关于是否按下了按钮。
示例场景:
笼子位于2楼,由IR传感器检测到。如果我按下2楼的按钮,那么Mega必须知道笼子已经在那了。如果我按下3楼的按钮,Mega必须知道笼子在2楼并且按下3楼的按钮,并且它必须控制马达将笼子带到3楼并显示在7段显示器上
我必须使用i2c。
很抱歉,我很难理解文字内容。我的英语不好。
这是奴隶代码:
#include<arduino.h>
const int dataPin = 11; // wire to 74HC595 pin 11
const int latchPin = 8; // to 74HC595 pin 8
const int clockPin = 12; // to 74HC595 pin 12
int nummers[6] = {126, 12, 182, 158, 204, 204}; //0, 1, 2, 3, 4, 5
int buttonvalue = 0;
int button = 2;
int buttonLed = 3;
// ir sensor and irleds
int irLedGreen = 5;
int irLedRed = 6;
#define IR 4
int detect = 0;
void setup() {
Serial.begin(9600);
//ir sensor
pinMode(irLedGreen, OUTPUT);
pinMode(IR, INPUT);
pinMode(irLedRed, OUTPUT);
//shift out
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
//button
pinMode(button, INPUT);
pinMode(buttonLed, OUTPUT);
}
void loop(){
buttonvalue = digitalRead(button);
detect = digitalRead(IR);
// ir sensor led. It will be green if it detects something else it will be red.
if (detect == LOW) { // if if detects something do the following.
digitalWrite(irLedGreen, HIGH);
digitalWrite(irLedRed, LOW);
} else {
digitalWrite(irLedGreen, LOW);
digitalWrite(irLedRed, HIGH);
}
// button is pressed
if (buttonvalue != 0 ) {
digitalWrite(buttonLed, HIGH);
Serial.println("button");
} else if (detect == LOW) {
digitalWrite(buttonLed, LOW);
digitalWrite(latchPin, LOW); // prepare shift register for data
shiftOut(dataPin, clockPin, MSBFIRST, nummers[4]); // send data
digitalWrite(latchPin, HIGH); // update display
Serial.println("obstakel");
}
digitalWrite(latchPin, LOW); // prepare shift register for data
shiftOut(dataPin, clockPin, MSBFIRST, nummers[0]); // send data
digitalWrite(latchPin, HIGH); // update display
}
答案 0 :(得分:0)
打开任何网页搜索。输入“ arduino i2c”
点击第一个链接
https://www.arduino.cc/en/Reference/Wire
阅读文字。在“示例”下找到
主读取器/从写入器:对两个Arduino板进行编程以进行通信 在主读/从发件人配置中通过 I2C。
打开链接
https://www.arduino.cc/en/Tutorial/MasterReader
阅读文字。
将示例代码上传到您的arduinos
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop() {
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(500);
}
// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
}
理解代码,然后玩。然后将获得的知识应用于您的项目。