简体   繁体   English

我如何获得引脚 4 的值也被发送到从设备并使用 I2C 显示?

[英]How do i get values of pin 4 to also get sent over to the slave and be displayed using I2C?

I am trying to get voltage values from my master to my slave but only the voltage from Pin3 is being sent over and not my values from pin 4. I am new to coding so please make your solution very "jargon-less."我试图从我的主站到我的从站获取电压值,但只有来自 Pin3 的电压被发送,而不是来自 pin 4 的电压。我是编码新手,所以请让你的解决方案非常“行话少”。 If you could provide me with an example then it will be greatly appreciated.如果你能给我提供一个例子,那么将不胜感激。

MASTER CODE:主码:

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();
} 

SLAVE CODE:奴隶代码:

#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());

As Edd said you should show us some output.正如 Edd 所说,您应该向我们展示一些输出。

But assuming you're trying to compare the incoming data to 3.20 as a numerical value , the following code will do the job.但假设您尝试将传入数据与 3.20作为数值进行比较,以下代码将完成这项工作。

Master掌握

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))));
} 

Slave奴隶

#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 = "";

}

But again you should consider your way of controlling your GPIO outputs.但是,您应该再次考虑控制 GPIO 输出的方式。

You've done quite a lot of mistakes.你已经犯了很多错误。 Check this page to get more idea about Strings.查看此页面以了解有关字符串的更多信息。 And this page for Wire transmission.此页面用于有线传输。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM