Setup running more than once

Hello everyone.

I wrote this code to control a robotic arm by sending a query through the browser. I was testing out sending an int array. It works, but everytime I send a query like ?ints=1,2,3,4 after getting the input, it seems to run setup again.

#include <SPI.h>
#include <Ethernet.h>
#include "SoftwareSerial.h"
#include "LobotServoController.h"

#define LED 13
#define TOUCH 12  //Touch sensor
#define RxPin 8   //Define soft serial port
#define TxPin 9

int posStep = 5;

SoftwareSerial mySerial(RxPin, TxPin);        //Set an example of SoftwareSerial
LobotServoController myController(mySerial);  //Set an example of LobotSerialController

byte mac[] = {
  0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX
};

// Create an EthernetServer object
EthernetServer server(80);

void setup() {
  // Start the serial communication
  mySerial.begin(9600);
  Serial.begin(9600);
  digitalWrite(LED, HIGH);
  myController.moveServo(1, 500, 1000);
  myController.moveServo(2, 500, 1000);
  myController.moveServo(3, 260, 1000);
  myController.moveServo(4, 750, 1000);
  myController.moveServo(5, 445, 1000);
  myController.moveServo(6, 500, 1000);
  delay(1500);
  // Initialize the Ethernet shield
  Ethernet.begin(mac);

  // Start the server
  server.begin();
  Serial.println(Ethernet.localIP());
  Serial.println("test");
}

void loop() {
  EthernetClient client = server.available();
  if (client) {
    String request = "";
    while (client.available()) {
      char c = client.read();
      request += c;
    }

    if (request.indexOf("?ints=") != -1) {
      String intsStr = request.substring(request.indexOf("?ints=") + 6);
      int ints[10];
      int i = 0;
      while (intsStr.indexOf(",") != -1) {
        ints[i++] = intsStr.substring(0, intsStr.indexOf(",")).toInt();
        intsStr = intsStr.substring(intsStr.indexOf(",") + 1);
      }
      ints[i++] = intsStr.toInt();
      }
        //Run the servo motor to input value
      myController.moveServo(1, ints[0], 1000);
      myController.moveServo(2, ints[1], 1000);
      myController.moveServo(3, ints[2], 1000);
      myController.moveServo(4, ints[3], 1000);
      myController.moveServo(5, ints[4], 1000);
      myController.moveServo(6, ints[5], 1000);
      delay(1500);
    }

    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println();
    client.println("<html><body>Received int array</body></html>");
    delay(1000);
    //client.stop();
  }
}

Can someone help find the issue?

I did not read your code.
But your mcu might be resetting due to low power.
How have you connected your actuators?
What type of power supply do you have?
What type of actuators do you have? Datasheet!
Add a printout from your SerialMonitor

After reading your code: either it never reaches end of setup (then you have a power issue) or you do reach loop and something else is happening.
What type of mcu do you use?

Check for power supply

I am connecting it directly to my computer using USB, is that not enough power?
Also, i am using the arduino Mega

I am using Arduino Mega for this project, and connected it directly to my computer via USB. The robotic arm is being powered by its own power source.

The code is able to get into loop and is able to send the query to the robotic arm. However, it does not send an OK query and it just runs setup again.

Hello

Disconnect all servos and try again.

Using Strings might cause a problem. But I would not expect that problem immediately.
In order to prevent holes in heap, you could reserve a String buffer large enough to hold the largest thing.
By the way, how long is the largest thing? Mega does not have that much memory and you do a lot of runtime allocation.
Add Serial.prints in your code to see how far it gets.
Serial.println("waymark 1");
Add Serial.prints of your substrings in the for loop...

Usb should be sufficient for mega with not much connected.
Does your robo share a ground line?
Can you share a picture of your setup?

That code does not compile.
It has an extra closing backet at the end.

Most likely, your power supply failed to supply the current required to move eight servos simultaneously, and despite having a separate supply, your system wasn't immune to the transient produced.
To test this, simply insert a delay(200) between each of your servo commands at the end of loop(). If it then doesn't reset, decide how to proceed. If it still resets something else is a problem as well.
I'd also be suspicious that your servo power supply ground isn't well connected to your Arduino's ground.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.