首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将I2C协议与arduinos结合使用

如何将I2C协议与arduinos结合使用
EN

Stack Overflow用户
提问于 2019-10-14 22:20:05
回答 1查看 441关注 0票数 0

我正试图用3个或更多的Arduino Uno's和1 Mega (作为主人)建造一个电梯/电梯。

Arduino Uno (奴隶)

我已经连接了一个红外传感器,一个按钮,一个LED和一个7段显示器到Uno.

如果我按下按钮,LED就会亮起来,一直这样,直到红外传感器检测到电梯笼。然后,发光二极管将关闭和7 -segment显示器将显示地板号码。

Arduino Mega (硕士)

该主机用于步进电机和键盘。

主人要做的是询问奴隶(在这种情况下,Uno's)是否按下按钮。

示例场景

保持架在2楼,由红外传感器检测。如果我按下2楼的按钮,梅加一定知道笼子已经在那里了。如果我按下3楼的按钮,Mega必须知道笼子在2楼,3楼的按钮被按下,它必须控制马达把笼子带到3楼,并显示在7段显示器上。

我必须使用i2c。

这是奴隶的代码:

代码语言:javascript
复制
#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
}

编辑

这是一个小项目,不是真正的电梯。我需要你的帮助,我怎样才能给大师编程。

EN

回答 1

Stack Overflow用户

发布于 2019-10-15 09:49:25

打开任何网络搜索。输入"arduino i2c“

单击第一个链接

https://www.arduino.cc/en/Reference/Wire

读课文。在“示例”下找到

主读取器/从作者:编程两个Arduino板,通过I2C在主阅读器/从发件人配置中相互通信。

打开链接

https://www.arduino.cc/en/Tutorial/MasterReader

读课文。

将示例代码上载到您的arduinos

主控

代码语言:javascript
复制
// 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);
}

从站

代码语言:javascript
复制
// 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
}

理解代码,玩它。然后将所获得的知识应用到您的项目中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58384743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档