作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试获取从主设备到从设备的电压值,但只有 Pin3 的电压被发送过来,而不是 Pin 4 的电压值。我是编码新手,所以请让您的解决方案非常“简洁”。 ”如果您能为我提供一个示例,我将不胜感激。
主代码:
void pin3() {
Wire.beginTransmission(0x08);
int val2 = analogRead(twosensorpin);
float volts2 = (val2 / 1023.0) * refvoltage;
voltage = String(volts2);
char volt2[5];
voltage.toCharArray(volt2, 5);
Serial.print("The voltage are pin 2 is ");
Serial.println(voltage);
Wire.write(volt2);
Wire.endTransmission();
}
void pin4() {
Wire.beginTransmission(0x08);
int val3 = analogRead(threesensorpin);
float volts3 = (val3 / 1023.0) * refvoltage;
voltage2 = String(volts3);
char volt3[5];
voltage.toCharArray(volt3, 5);
Serial.print("The voltage are pin 3 is ");
Serial.println(voltage2);
Wire.write(volt3);
Wire.endTransmission();
}
从属代码:
#include <Wire.h>
#define SLAVE_ADDRESS 0X08
String q;
String r = "3.20";
// name the motor control pins
#define PWMa 7
#define PWMb 5
#define PWMc 8
#define PWMd 6
void setup() {
// configure the motor control pins as outputs
pinMode(PWMa, OUTPUT);
pinMode(PWMb, OUTPUT);
pinMode(PWMc, OUTPUT);
pinMode(PWMd, OUTPUT);
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveEvent);
//Wire.onRequest(requestEvent);
Serial.begin(9600);
}
void loop() {}
void receiveEvent() {
q = "";
while (Wire.available()) {
char c = Wire.read();
q += c;
}
do {
if (q < r) {
Serial.print("The value coming from pin 2 is ");
Serial.println(q);
digitalWrite(PWMa, LOW);
digitalWrite(PWMb, HIGH);
digitalWrite(PWMc, LOW);
digitalWrite(PWMd, HIGH);
//delay(500);
}
} while (Wire.available());
do {
if (q > r) {
digitalWrite(PWMa, HIGH);
digitalWrite(PWMb, LOW);
digitalWrite(PWMc, HIGH);
digitalWrite(PWMd, LOW);
}
} while (Wire.available());
最佳答案
正如 Edd 所说,你应该向我们展示一些输出。
但假设您尝试将传入数据与 3.20 进行比较作为数值,以下代码将完成这项工作。
大师
float send_i2c(int val){
float volts = (val / 1023.0) * refvoltage;
char v[5];
String(volts).toCharArray(v, 5);
Wire.beginTransmission(0x08);
Wire.write(v);
Wire.endTransmission();
return volts;
}
void pin3(){
Serial.println("Pin3 val : " + String(send_i2c(analogRead(twosensorpin))));
}
void pin4(){
Serial.println("Pin4 val : " + String(send_i2c(analogRead(threesensorpin))));
}
从属
#include <Wire.h>
#define SLAVE_ADDRESS 0X08
#define R 3.20
String q;
// name the motor control pins
#define PWMa 7
#define PWMb 5
#define PWMc 8
#define PWMd 6
void setup() {
// configure the motor control pins as outputs
pinMode(PWMa, OUTPUT);
pinMode(PWMb, OUTPUT);
pinMode(PWMc, OUTPUT);
pinMode(PWMd, OUTPUT);
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void loop() {}
void receiveEvent(int a) {
int num = 0;
while (Wire.available())
q += Wire.read();
num = q.toInt();
Serial.print("The value: ");
Serial.println(q);
digitalWrite(PWMa, (num < R) ? LOW : HIGH);
digitalWrite(PWMb, (num < R) ? HIGH : LOW);
digitalWrite(PWMc, (num < R) ? LOW : HIGH);
digitalWrite(PWMd, (num < R) ? HIGH : LOW);
q = "";
}
但是您应该再次考虑控制 GPIO 输出的方式。
关于c++ - 如何获取引脚 4 的值并将其发送到从机并使用 I2C 显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59825840/
我是一名优秀的程序员,十分优秀!