Помогите найти ошибку в коде

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

Хочу написать код для управления светодиодами через интернет модуль. Через данный код можно управлять 4 светодиодами, но нужно 8! Работают только 4, остальные не активны, но просто горят.

#include <EtherCard.h>

static byte mymac[] = {0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A};

byte Ethernet::buffer[900];
BufferFiller bfill;

int LedPins[] = {2, 3, 4, 5, 6, 7, 9, 10};

boolean PinStatus[] = {1, 2, 3, 4, 5, 6, 7, 8};

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><br />"
                    "Relay 5: <a href=\"?ArduinoPIN5=$F\">$F</a><br />"
                    "Relay 6: <a href=\"?ArduinoPIN6=$F\">$F</a><br />"
                    "Relay 7: <a href=\"?ArduinoPIN7=$F\">$F</a><br />"
                    "Relay 8: <a href=\"?ArduinoPIN8=$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>"),
               PinStatus[5] ? PSTR("off") : PSTR("on"),
               PinStatus[5] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"),
               PinStatus[6] ? PSTR("off") : PSTR("on"),
               PinStatus[6] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"),
               PinStatus[7] ? PSTR("off") : PSTR("on"),
               PinStatus[7] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"),
               PinStatus[8] ? PSTR("off") : PSTR("on"),
               PinStatus[8] ? 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 <= 8; 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("?ArduinoPIN5=on ", data, 16) == 0) {
        PinStatus[5] = true;
        bfill.emit_p(http_Found);
      } else if (strncmp("?ArduinoPIN6=on ", data, 16) == 0) {
        PinStatus[6] = true;
        bfill.emit_p(http_Found);
      } else if (strncmp("?ArduinoPIN7=on ", data, 16) == 0) {
        PinStatus[7] = true;
        bfill.emit_p(http_Found);
      } else if (strncmp("?ArduinoPIN8=on ", data, 16) == 0) {
        PinStatus[8] = 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 if (strncmp("?ArduinoPIN5=off ", data, 17) == 0) {
        PinStatus[5] = false;
        bfill.emit_p(http_Found);
      } else if (strncmp("?ArduinoPIN6=off ", data, 17) == 0) {
        PinStatus[6] = false;
        bfill.emit_p(http_Found);
      } else if (strncmp("?ArduinoPIN7=off ", data, 17) == 0) {
        PinStatus[7] = false;
        bfill.emit_p(http_Found);
      } else if (strncmp("?ArduinoPIN8=off ", data, 17) == 0) {
        PinStatus[8] = false;
        bfill.emit_p(http_Found);
      } else {
        // Page not found
        bfill.emit_p(http_Unauthorized);
      }
    }
    ether.httpServerReply(bfill.position());    // send http response
  }
}

Буду благодарен любым исправлериям!

 

ven-til
Offline
Зарегистрирован: 13.02.2018

в 91 строке цикл до 3.

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

А сколько нужно поставить?

ven-til
Offline
Зарегистрирован: 13.02.2018

Martin Swift пишет:
А сколько нужно поставить?

Вот это поворот. У тебя идей совсем нет? Дам подсказку у тебя цикл от 0 до 3 включительно, то есть он срабатывает 4 раза.

Genri5
Offline
Зарегистрирован: 31.05.2016

Martin Swift пишет:
А сколько нужно поставить?

Полное отсутствие присутствия понимания программы. :-)

sadman41
Offline
Зарегистрирован: 19.10.2016

Где-то я этот код уже видел... В "Ищу исполнителя" что ли...

b707
Offline
Зарегистрирован: 26.05.2017

ven-til пишет:

в 91 строке цикл до 3.

а все равно работать не будет. Во всяком случае правильно. В коде во всех массивах и циклах индексы перпутаны.

 

ТС написал "Хочу написать код". Общество не против. Пусть пишет.

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

ven-til пишет:
Martin Swift пишет:
А сколько нужно поставить?
Вот это поворот. У тебя идей совсем нет? Дам подсказку у тебя цикл от 0 до 3 включительно, то есть он срабатывает 4 раза.

Спасибо, всё работает!