我实际上是在项目的最后一部分,它是使用数字电位控制POT。我有一个MCP4131数字电位器,但我似乎无法让它工作。我认为这是由于我连接的wifly屏蔽,因为它也使用SPI。我有一个连接到WIFFLY屏蔽的arduino mega,我还连接了蓝牙模块,并连接到另一个arduino的TFT。蓝牙模块和tft屏幕只是将数据发送到串口(2和3),所以我知道这不是问题。我会在下面发布我的代码,如果有人能帮我解决这一点,我会非常感激。
#include <SPI.h>
#include <WiFly.h>
#include <Wire.h>
WiFlyServer server(2000);
byte address = 0x11;
int CS= 10;
int led = 47;
String readString;
String readString2;
char c;
int i;
char d;
int photocellReading;
int number_4 = 255;
int number_5 = 111;
int number_6 = 93;
int number_7 = 75;
int number_8 = 56;
int number_9 = 38;
int number_10 = 18;
int number_11 = 0;
int pwmpin = 9;
void setup() {
WiFly.begin();
Serial.begin(9600);
Serial3.begin(9600);
Serial2.begin(9600);
server.begin();
pinMode (CS, OUTPUT);
SPI.begin();
pinMode(led, OUTPUT);
pinMode(pwmpin, OUTPUT);
}
void loop() {
//resetFunc();
WiFlyClient client = server.available();
if(Serial2.available()){
while(Serial2.available() > 0) {
char d = Serial2.read(); //gets one byte from serial buffer
readString2 += d; //makes the string readString
delay(5); //slow looping to allow buffer to fill with next character
}
if (readString2.length() >0){
int commaIndex2 = readString2.indexOf(',');
int secondCommaIndex2 = readString2.indexOf(',', commaIndex2+1);
String firstValue2 = readString2.substring(0, commaIndex2);
String secondValue2 = readString2.substring(commaIndex2+1, secondCommaIndex2);
String thirdValue2 = readString2.substring(secondCommaIndex2+1); // To the end of the string
int x2 = firstValue2.toInt();
int y2 = secondValue2.toInt();
int z2 = thirdValue2.toInt();
if(x2 == 200){
if(y2 == 4){
digitalWrite(led, LOW);
}
}
}
//empty for next input
}
if (client.connect()){
while (client.available()) {
c = client.read();
readString += c; //makes the string readString
delay(5); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0){
int commaIndex = readString.indexOf(',');
int secondCommaIndex = readString.indexOf(',', commaIndex+1);
String firstValue = readString.substring(0, commaIndex);
String secondValue = readString.substring(commaIndex+1, secondCommaIndex);
String thirdValue = readString.substring(secondCommaIndex+1); // To the end of the string
int x = firstValue.toInt();
int y = secondValue.toInt();
int z = thirdValue.toInt();
Serial.println(x);
if(x == 4){
digitalWrite(led, HIGH);
digitalWrite(CS, LOW);
SPI.transfer(address);
SPI.transfer(number_4);
digitalWrite(CS, HIGH);
}
}
}
if (!client.connected() || !server.available()){
while(SpiSerial.available() > 0) {
#if ARDUINO >= 100
Serial3.write(SpiSerial.read());
#else
Serial3.print(SpiSerial.read(), BYTE);
#endif
delay(100);
}
if(Serial3.available()) { // Outgoing data
#if ARDUINO >= 100
SpiSerial.write(Serial3.read());
#else
SpiSerial.print(Serial3.read(), BYTE);
#endif
}
}
readString=""; //empty for next input
readString2="";
}
正如你所看到的,蓝牙和tft屏幕只是为了发送数据,我唯一的问题是这个。如前所述,如果有人能帮我拿锅看,我将非常感激。