我的步进电机如代码里,为什么在void setup里的就可以执行,而在loop中的无法执行呢?
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>
byte buff[2];
//for yeelink api
#define APIKEY "1bb83b3dd6b237608b0132d60901dac0"
#define DEVICEID 9340//led 13
#include"SR04.h"
#define TRIG_PIN 6
#define ECHO_PIN 7
SR04 sr04=SR04(ECHO_PIN,TRIG_PIN);
long a;
#define SENSORID1 15009//photoresistence LDR
#define ldrpin A5
int ldrbuffer=0;
#define SENSORID2 //LM35 temprature sensor
#define lm35 A0
int lm35val=0;
float temp=0;
#define SENSORID3 //stepper motor
#define a1 2
#define b1 3
#define c1 4
#define d1 5
int n; //calculating the stepper's angle
//assign a MAC address for the ethernet controller.
byte mac[]={0x00,0x1D,0x72,0x82,0x35,0x9D};
//initialize the library instance
EthernetClient client ;
char server[]="api.yeelink.net";
unsigned long lastConnectionTime=0;
boolean lastConnected=false;
const unsigned long postingInterval=3*1000;
String returnValue="";
boolean ResponseBegin=false;
void setup()
{
pinMode(a1,OUTPUT);
pinMode(b1,OUTPUT);
pinMode(c1,OUTPUT);
pinMode(d1,OUTPUT);
ldrbuffer= analogRead(ldrpin);
for(n=0;n<513;n++)//从此开始为步进电机驱动,513为电机转一圈
{
if(ldrpin!=0)//此处为一判断条件,不影响电机工作,电机为28BYJ-48 步进电机,
{
Phase_A();
delay(3);
Phase_B();
delay(3);
Phase_C();
delay(3);
Phase_D();
delay(3);
}
else
{
break;
} //步进电机驱动程序结束,表现正常,驱动板上边A+A-B+B-亮度一致
}
Wire.begin();
Serial.begin(57600);
if(Ethernet.begin(mac)==0)
{
Serial.println("Failed to configure Ethernet using DHCP");
for(;;)
;
}
else
{
Serial.println("Ethernet configuration OK");
}
}
void loop()
{
if(client.available())
{
char c=client.read();
if(c=='{')
{
ResponseBegin=true;
}
else if(c=='}')
{
ResponseBegin=false;
}
if(ResponseBegin)
returnValue+=c;
}
if(returnValue.length()!=0&&(ResponseBegin==false))
{
Serial.println(returnValue);
if(returnValue.charAt(returnValue.length()-1)=='1')
{
Serial.println("turn on the Radar");
void Radar();
{
lm35val=analogRead(lm35);
temp = lm35val * 0.48876;
int n;
for(n=0;n<1026;n++) //步进电机驱动开始
{
if(n<513)
{
a=sr04.Distance(); //超声波测距模块
Phase_D();
delay(3);
Phase_C();
delay(3);
Phase_B();
delay(3);
Phase_A();
delay(3);
Serial.println(a/100); //测距输出
}
else
{
a=sr04.Distance();
Phase_A();
delay(3);
Phase_B();
delay(3);
Phase_C();
delay(3);
Phase_D();
delay(3);
Serial.println(a/100);//步进电机驱动程序结束,此段程序步进电机不转,驱动板上边指示灯显示不正常,A+A-灯光暗,B+B-亮
}
}
}
}
else if(returnValue.charAt(returnValue.length()-1)=='0')
{
Serial.println("turn off the Radar");
}
returnValue="";
}
if (!client.connected() && lastConnected)
{
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
// read sensor data, replace with your code
//int sensorReading = readLightSensor();
Serial.print("yeelink:");
//get data from server
getData();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
void Phase_C()
{
digitalWrite(a1,LOW);
digitalWrite(b1,LOW);
digitalWrite(c1,HIGH);
digitalWrite(d1,LOW);
}
void Phase_A()
{
digitalWrite(a1,HIGH);
digitalWrite(b1,LOW);
digitalWrite(c1,LOW);
digitalWrite(d1,LOW);
}
void Phase_B()
{
digitalWrite(a1,LOW);
digitalWrite(b1,HIGH);
digitalWrite(c1,LOW);
digitalWrite(d1,LOW);
}
void Phase_D()
{
digitalWrite(a1,LOW);
digitalWrite(b1,LOW);
digitalWrite(c1,LOW);
digitalWrite(d1,HIGH);
}
void getData(void) {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP GET request:
client.print("GET /v1.0/device/");
client.print(DEVICEID);
client.print("/sensor/");
client.print(SENSORID1);
client.print("/datapoints");
client.println(" HTTP/1.1");
client.println("Host: api.yeelink.net");
client.print("Accept: *");
client.print("/");
client.println("*");
client.print("U-ApiKey: ");
client.println(APIKEY);
client.println("Content-Length: 0");
client.println("Connection: close");
client.println();
Serial.println("print get done.");
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// note the time that the connection was made or attempted:
lastConnectionTime = millis();
}
这是为什么啊?求大神帮忙!
|