C Wifi屏蔽和电机屏蔽一起工作

C Wifi屏蔽和电机屏蔽一起工作,c,arduino,wifi,led,C,Arduino,Wifi,Led,我目前参与的一个项目需要有4个led和一个步进电机通过wifi独立打开/关闭 只需提及Im使用Uno r3、官方Wifi屏蔽和官方汽车屏蔽 我的4个led在引脚A2-A5或代码引脚(16-19)上工作,留下所有数字引脚,并将步进电机设置为引脚9、8、6、5。当我进入ip地址时,我可以独立地关闭/打开每个LED,但使用步进电机,它要么抖动,不移动,要么什么都不动 我已经在下面发布了我的代码,非常感谢任何帮助或建议。谢谢 /* WiFi Web Server LED Blink A simp

我目前参与的一个项目需要有4个led和一个步进电机通过wifi独立打开/关闭

只需提及Im使用Uno r3、官方Wifi屏蔽和官方汽车屏蔽

我的4个led在引脚A2-A5或代码引脚(16-19)上工作,留下所有数字引脚,并将步进电机设置为引脚9、8、6、5。当我进入ip地址时,我可以独立地关闭/打开每个LED,但使用步进电机,它要么抖动,不移动,要么什么都不动

我已经在下面发布了我的代码,非常感谢任何帮助或建议。谢谢

/*
  WiFi Web Server LED Blink

 A simple web server that lets you blink an LED via the web.
 This sketch will print the IP address of your WiFi Shield (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn on and off the LED on pin 9.

 This example is written for a network using WPA encryption. For 
 WEP or WPA, change the Wifi.begin() call accordingly.

 Circuit:
 * WiFi shield attached
 * LED attached to pin 9

 created 25 Nov 2012
 by Tom Igoe
 */

#include <SPI.h>
#include <WiFi.h>
#include <Stepper.h>

char ssid[] = "username";      //  your network SSID (name) 
char pass[] = "password";   // your network password
int keyIndex = 0;  
int delaylegnth = 7;
int state = 0;
int x = 0;
// your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  Serial.begin(9600);      // initialize serial communication
  pinMode(16, OUTPUT);      // set the LED pin mode
  pinMode(17, OUTPUT);      // set the LED pin mode
  pinMode(18, OUTPUT);      // set the LED pin mode
  pinMode(19, OUTPUT);      // set the LED pin mode

    //establish motor direction toggle pins
  pinMode(5, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(6, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???

  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    while(true);        // don't continue
  } 

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  } 
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status

}


void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";  // make a String to hold incoming data from the client
    String currentLine2 = ""; 
    String currentLine3 = "";
    String currentLine4 = "";  
    String currentLine5 = ""; 
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {  
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:    
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> turn the LED on pin 4 on<br>");
            client.print("Click <a href=\"/L\">here</a> turn the LED on pin 4 off<br>");
            client.print("Click <a href=\"/A\">here</a> turn the LED on pin 5 on<br>");
            client.print("Click <a href=\"/B\">here</a> turn the LED on pin 5 off<br>");
            client.print("Click <a href=\"/C\">here</a> turn the LED on pin 6 on<br>");
            client.print("Click <a href=\"/D\">here</a> turn the LED on pin 6 off<br>");
            client.print("Click <a href=\"/E\">here</a> turn the LED on pin 2 on<br>");
            client.print("Click <a href=\"/F\">here</a> turn the LED on pin 2 off<br>");
            client.print("Click <a href=\"/G\">here</a> turn the Stepper<br>");
            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;         
          } 
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
            currentLine2 = "";
            currentLine3 = "";
            currentLine4 = "";
            currentLine5 = "";
          }
        }     
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;
          currentLine2 += c;
          currentLine3 += c;
          currentLine4 += c;
          currentLine5 += c;
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(16, HIGH);               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(16, LOW);                // GET /L turns the LED off
        }
        if (currentLine2.endsWith("GET /A")) {
          digitalWrite(17, HIGH);               // GET /A turns the LED on
        }
        if (currentLine2.endsWith("GET /B")) {
          digitalWrite(17, LOW);                // GET /B turns the LED off
        }
          if (currentLine3.endsWith("GET /C")) {
          digitalWrite(18, HIGH);               // GET /A turns the LED on
        }
        if (currentLine3.endsWith("GET /D")) {
          digitalWrite(18, LOW);                // GET /B turns the LED off
        }
          if (currentLine4.endsWith("GET /E")) {
          digitalWrite(19, HIGH);               // GET /A turns the LED on
        }
        if (currentLine4.endsWith("GET /F")) {
          digitalWrite(19, LOW);                // GET /B turns the LED off
        }
           }

          if (currentLine5.endsWith("GET /G")) { //stepper motor
//     for(int x; x < 500; x++) {

//  state=digitalRead(0);
//  if (state == LOW){

  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(5, HIGH);   //Sets direction of CH A
  analogWrite(A0, 255);   //Moves CH A

//  }

//  else{

  delay(delaylegnth);

  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(6, LOW);   //Sets direction of CH B
  analogWrite(A1, 255);   //Moves CH B

  delay(delaylegnth);

  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(5, LOW);   //Sets direction of CH A
  analogWrite(A0, 255);   //Moves CH A

  delay(delaylegnth);

  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(6, HIGH);   //Sets direction of CH B
  analogWrite(A1, 255);   //Moves CH B

  delay(delaylegnth);

//     }
//     }

      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}
/*
WiFi网络服务器LED闪烁
一个简单的网络服务器,允许您通过网络闪烁LED。
此草图将打印WiFi屏蔽的IP地址(一旦连接)
连接到串行监视器。从那里,您可以在web浏览器中打开该地址
打开和关闭针脚9上的LED。
此示例是为使用WPA加密的网络编写的。对于
WEP或WPA,相应地更改Wifi.begin()调用。
线路:
*已连接WiFi屏蔽
*连接到引脚9的LED
创建于2012年11月25日
汤姆·伊戈
*/
#包括
#包括
#包括
char ssid[]=“用户名”//您的网络SSID(名称)
字符传递[]=“密码”;//您的网络密码
int-keyIndex=0;
int delaylegnth=7;
int state=0;
int x=0;
//您的网络密钥索引号(仅WEP需要)
int状态=WL\U空闲\U状态;
WiFiServer服务器(80);
无效设置(){
Serial.begin(9600);//初始化串行通信
引脚模式(16,输出);//设置LED引脚模式
引脚模式(17,输出);//设置LED引脚模式
引脚模式(18,输出);//设置LED引脚模式
引脚模式(19,输出);//设置LED引脚模式
//建立电机方向拨动销
pinMode(5,输出);//通道A——高=向前,低=向后???
pinMode(6,输出);//通道B——高=向前,低=向后???
//建立电机制动销
引脚模式(9,输出);//制动(禁用)通道A
引脚模式(8,输出);//制动(禁用)通道B
//检查是否存在屏蔽:
如果(WiFi.status()=无线无屏蔽){
Serial.println(“不存在WiFi屏蔽”);
while(true);//不要继续
} 
//尝试连接到Wifi网络:
当(状态!=WL_已连接){
Serial.print(“正在尝试连接到名为:”)的网络;
Serial.println(ssid);//打印网络名称(ssid);
//连接到WPA/WPA2网络。如果使用open或WEP网络,请更改此行:
状态=WiFi.begin(ssid,通过);
//等待10秒钟以进行连接:
延迟(10 000);
} 
server.begin();//在端口80上启动web服务器
printWifiStatus();//您现在已连接,请打印状态
}
void循环(){
WiFiClient client=server.available();//侦听传入的客户端
如果(客户){//如果你有客户,
Serial.println(“新客户端”);//从串行端口打印消息
String currentLine=“;//生成一个字符串以保存来自客户端的传入数据
字符串currentLine2=“”;
字符串currentLine3=“”;
字符串currentLine4=“”;
字符串currentLine5=“”;
while(client.connected()){//在连接客户端时循环
如果(client.available()){//如果要从客户端读取字节,
char c=client.read();//读取一个字节,然后
Serial.write(c);//在串行监视器上打印出来
如果(c=='\n'){//如果字节是换行符
//如果当前行为空,则一行中有两个换行符。
//客户端HTTP请求到此结束,请发送响应:
如果(currentLine.length()==0){
//HTTP标头始终以响应代码开头(例如HTTP/1.1 200 OK)
//和一个内容类型,以便客户端知道接下来会发生什么,然后是一个空行:
client.println(“HTTP/1.1200ok”);
client.println(“内容类型:text/html”);
client.println();
//HTTP响应的内容如下所示:
client.print(“单击打开针脚4上的LED”
”; 打印(“单击关闭针脚4上的LED
”; client.print(“单击打开针脚5上的LED”
”; 打印(“单击关闭针脚5上的LED
”; client.print(“单击打开针脚6上的LED”
”; 打印(“单击关闭针脚6上的LED
”; client.print(“单击将针脚2上的LED灯打开
”; 打印(“单击关闭针脚2上的LED
”; 打印(“单击旋转步进器
”; //HTTP响应以另一个空行结束: client.println(); //打破while循环: 打破 } 否则{//如果有换行符,请清除currentLine: currentLine=“”; currentLine2=“”; currentLine3=“”; currentLine4=“”; currentLine5=“”; } } else如果(c!='\r'){//如果您得到的不是回车字符,而是其他字符, 电流线+=c; currentLine2+=c; currentLine3+=c; currentLine4+=c; currentLine5+=c; } //检查客户端请求是“GET/H”还是“GET/L”: if(currentLine.endsWith(“GET/H”)){ digitalWrite(16,高);//GET/H打开LED } if(currentLine.endsWith(“GET/L”)){ digitalWrite(16,低);//GET/L关闭LED } 如果(currentLine2.endsWith(“GET/A”)){ digitalWrite(17,高);//GET/A打开LED