Требуется доделать скетч

Martin Swift
Offline
Зарегистрирован: 02.03.2018

Добрый вечер, мне нужно сделать скетч для управления четырьмя двигателями через шильд L293D и интернет модуль  HR911105A. Имеется два скетча (они представлены ниже). Нужно объеденить их + добавить управление двумя светодиодами через интернет. Все предложения на martin7swift@gmail.com

#include <EtherCard.h>
 
static byte mymac[] = {0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A};
 
byte Ethernet::buffer[900];
BufferFiller bfill;
 
int LedPins[] = {2, 3, 4, 5};
 
boolean PinStatus[] = {1, 2, 3, 4};
 
const char http_OK[] PROGMEM =
  "HTTP/1.0 200 OK\r\n"
  "Content-Type: text/html\r\n"
  "Pragma: no-cache\r\n\r\n";
 
const char http_Found[] PROGMEM =
  "HTTP/1.0 302 Found\r\n"
  "Location: /\r\n\r\n";
 
const char http_Unauthorized[] PROGMEM =
  "HTTP/1.0 401 Unauthorized\r\n"
  "Content-Type: text/html\r\n\r\n"
  "<h1>401 Unauthorized</h1>";
 
void homePage()
{
  bfill.emit_p(PSTR("$F"
                    "<title>ArduinoPIN Webserver</title>"
                    "Relay 1: <a href=\"?ArduinoPIN1=$F\">$F</a><br />"
                    "Relay 2: <a href=\"?ArduinoPIN2=$F\">$F</a><br />"
                    "Relay 3: <a href=\"?ArduinoPIN3=$F\">$F</a><br />"
                    "Relay 4: <a href=\"?ArduinoPIN4=$F\">$F</a>"),
 
               http_OK,
               PinStatus[1] ? PSTR("off") : PSTR("on"),
               PinStatus[1] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"),
               PinStatus[2] ? PSTR("off") : PSTR("on"),
               PinStatus[2] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"),
               PinStatus[3] ? PSTR("off") : PSTR("on"),
               PinStatus[3] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"),
               PinStatus[4] ? PSTR("off") : PSTR("on"),
               PinStatus[4] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"));
}
 
void setup()
{
  Serial.begin(9600);
 
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0);
 
  if (!ether.dhcpSetup());
 
  ether.printIp("My Router IP: ", ether.myip);
 
  for (int i = 0; i <= 4; i++)
  {
    pinMode(LedPins[i], OUTPUT);
    digitalWrite (LedPins[i], HIGH);
    PinStatus[i] = false;
  }
}
 
void loop()
{
  delay(1);
  word len = ether.packetReceive();   // check for ethernet packet
  word pos = ether.packetLoop(len);   // check for tcp packet
 
  if (pos) {
    bfill = ether.tcpOffset();
    char *data = (char *) Ethernet::buffer + pos;
    if (strncmp("GET /", data, 5) != 0) {
      bfill.emit_p(http_Unauthorized);
    } else {
      data += 5;
      if (data[0] == ' ') {
        homePage(); // Return home page
        for (int i = 0; i <= 3; i++) {
          digitalWrite(LedPins[i], !PinStatus[i + 1]);
        }
      } else if (strncmp("?ArduinoPIN1=on ", data, 16) == 0) {
        PinStatus[1] = true;
        bfill.emit_p(http_Found);
      } else if (strncmp("?ArduinoPIN2=on ", data, 16) == 0) {
        PinStatus[2] = true;
        bfill.emit_p(http_Found);
      } else if (strncmp("?ArduinoPIN3=on ", data, 16) == 0) {
        PinStatus[3] = true;
        bfill.emit_p(http_Found);
      } else if (strncmp("?ArduinoPIN4=on ", data, 16) == 0) {
        PinStatus[4] = true;
        bfill.emit_p(http_Found);
      } else if (strncmp("?ArduinoPIN1=off ", data, 17) == 0) {
        PinStatus[1] = false;
        bfill.emit_p(http_Found);
      } else if (strncmp("?ArduinoPIN2=off ", data, 17) == 0) {
        PinStatus[2] = false;
        bfill.emit_p(http_Found);
      } else if (strncmp("?ArduinoPIN3=off ", data, 17) == 0) {
        PinStatus[3] = false;
        bfill.emit_p(http_Found);
      } else if (strncmp("?ArduinoPIN4=off ", data, 17) == 0) {
        PinStatus[4] = false;
        bfill.emit_p(http_Found);
      } else {
        // Page not found
        bfill.emit_p(http_Unauthorized);
      }
    }
    ether.httpServerReply(bfill.position());    // send http response
  }
}
#include <AFMotor.h>  // Подключаем библиотеку для работы с шилдом 
#include <Servo.h>  // Подключаем библиотеку для работы с сервоприводами, можно не подключать

// Подключаем моторы к клеммникам M1, M2, M3, M4
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

void setup() {
  // Задаем максимальную скорость вращения моторов (аналог работы PWM) 
  motor1.setSpeed(255);
  motor1.run(RELEASE);
  motor2.setSpeed(255);
  motor2.run(RELEASE);
  motor3.setSpeed(255);
  motor3.run(RELEASE);
  motor4.setSpeed(255);
  motor4.run(RELEASE);
}

int i;

void loop() {
  // Двигаемся условно вперед одну секунду 
  motor1.run(FORWARD); // Задаем движение вперед
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
  motor1.setSpeed(255); // Задаем скорость движения
  motor2.setSpeed(255); 
  motor3.setSpeed(255); 
  motor4.setSpeed(255); 
  delay(1000);
  
  // Останавливаем двигатели
  /* Очень не рекомендуем резко переключать направление вращения двигателей.
  Лучше дать небольшой промежуток времени.*/
  
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  delay(500);
  
  // Двигаемся в обратном направлении
  motor1.run(BACKWARD);  // Задаем движение назад
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
  motor1.setSpeed(255);  // Задаем скорость движения 
  motor2.setSpeed(255); 
  motor3.setSpeed(255); 
  motor4.setSpeed(255); 
  delay(1000);
  
  // Останавливаем двигатели  
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  delay(500);
  
  // Разгоняем двигатели в одном направлении
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
  for (i=0; i<255; i++) {
    motor1.setSpeed(i); 
    motor2.setSpeed(i); 
    motor3.setSpeed(i); 
    motor4.setSpeed(i); 
    delay(10);
 }
 
 // Останавливаем двигатели  
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  delay(500);
  

 

semen-pro
Offline
Зарегистрирован: 16.02.2014

[quote = Martin Swift] интернет модуль HR911105A [/ quote]

Хочу вас разочаровать, этот модуль на самом деле ENC28J60, а он не сможет нормально работать с шаговыми двигателями и все зависнет.

Приобретите нормальный модуль типа W5100