Pin control within void/loop ifstatement

Hi, using an arduino dueminalove with an ethernet shield reading from twitter.

I have the code below working well so far. I have two motors attached to two relays on pin 2 and 3 and an input (limit switch) on pin 4.

I currently have it so when the arduino reads the tweet 'feed' from twitter it runs the section within the IF statement:

if(tweet == ">feed"){
digitalWrite(ShakePin, HIGH);
Serial.println("SHAKING!");
delay (4000);
digitalWrite(ShakePin, LOW);
Serial.println("FEEDING!");
digitalWrite(MotorPin, HIGH);
justFed = 0;
}

It works and runs fine so far but...

I am trying to figure out how to get the motor (MotorPin) to run continuously within this IF statement until it hits the limit switch on pin 4 (StopPin), stop the motor, set 'justfed to 0' then continue with the loop, monitoring for changes on twitter.

i can't get my head around getting the code to halt with the motor running until it hits the switch then carry on. I am not sure whether to try a 'switch/case' within the IF statement or is there an easier way that I am not seeing?

Thanks

/*

 
 */
#include <SPI.h>
#include <Ethernet.h>


//variable to prevent overfeeding
boolean justFed = 1;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(192,168,0,3); //<<< ENTER YOUR IP ADDRESS HERE!!!

// initialize the library instance:
EthernetClient client;

const int requestInterval = 10000;  // delay between requests = 1min

char serverName[] = "api.twitter.com";  // twitter URL

boolean requested;                   // whether you've made a request since connecting
long lastAttemptTime = 0;            // last time you connected to the server, in milliseconds

String currentLine = "";            // string to hold the text from server
String tweet = "";                  // string to hold the tweet
boolean readingTweet = false;       // if you're currently reading the tweet
const int StopPin = 4;
const int MotorPin = 3;
const int ShakePin = 2;

void setup() {

//  SET UP PINS

  pinMode(MotorPin, OUTPUT);
  pinMode(ShakePin, OUTPUT);
  pinMode(StopPin, INPUT);  // Cutout Switch
  digitalWrite(StopPin, LOW);  // 
  
  // reserve space for the strings:
  currentLine.reserve(256);
  tweet.reserve(150);

// initialize serial:
  Serial.begin(9600);
  // attempt a DHCP connection:
  if (!Ethernet.begin(mac)) {
    // if DHCP fails, start with a hard-coded address:
    Ethernet.begin(mac, ip);
  }
  // connect to Twitter:
  connectToServer();

  
  Serial.println("Starting up Feedatron4000");

}



void loop()
{
  if (justFed){
    if (client.connected()) {
      if (client.available()) {
        // read incoming bytes:
        char inChar = client.read();

        // add incoming byte to end of line:
        currentLine += inChar; 
  
        // if you get a newline, clear the line:
        if (inChar == '\n') {
          currentLine = "";
        } 
        // if the current line ends with <text>, it will
        // be followed by the tweet:
        if ( currentLine.endsWith("<text>")) {
          // tweet is beginning. Clear the tweet string:
          readingTweet = true; 
          tweet = "";
        }
        // if you're currently reading the bytes of a tweet,
        // add them to the tweet String:
        if (readingTweet) {
          if (inChar != '<') {
            tweet += inChar;
          } 
          else {
            // if you got a "<" character,
            // you've reached the end of the tweet:
            readingTweet = false;
            Serial.println(tweet);
            
        // END OF READING TWEET   
        
            if(tweet == ">feed"){
             digitalWrite(ShakePin, HIGH);
//              Serial.println("SHAKING!");
             delay (4000);
             digitalWrite(ShakePin, LOW);
             Serial.println("FEEDING!");
             digitalWrite(MotorPin, HIGH);
             justFed = 0;
               }
            }
//            if(tweet == ">reset"){
//            digitalWrite(ShakePin, HIGH);
//            Serial.println("CLEARING");
//             delay (6000);
//             digitalWrite(ShakePin, LOW);
//            }
          
            // close the connection to the server:
            client.stop(); 
          }
        }
      }   
    }
    else if (millis() - lastAttemptTime > requestInterval) {
      // if you're not connected, and two minutes have passed since
      // your last connection, then attempt to connect again:
      connectToServer();
    }
  }
  //else if (millis() - lastAttemptTime > 14400000){//if four hours has passed since last feeding
  //  justFed = 1;
//  }
  else if (millis() - lastAttemptTime > 30000){//if three mins has passed since last feeding
    justFed = 1;
 }
}

  

void connectToServer() {
  // attempt to connect, and wait a millisecond:
  Serial.println("connecting to server...");
  if (client.connect(serverName, 80)) {
    Serial.println("making HTTP request...");
  // make HTTP GET request to twitter:
    client.println("GET /1/statuses/user_timeline.xml?screen_name=fnordfeeder&count=1 HTTP/1.1");
    client.println("HOST: api.twitter.com");
    client.println();
  }
  // note the time of this connect attempt:
  lastAttemptTime = millis();
}

I am trying to figure out how to get the motor (MotorPin) to run continuously within this IF statement until it hits the limit switch on pin 4 (StopPin), stop the motor, set 'justfed to 0' then continue with the loop, monitoring for changes on twitter.

Do you want to run the motor until it hits the limit switch, doing nothing else until that happens? Or, do you want to start the motor running, and then elsewhere in loop() read the limit switch and look for more tweeting?

Yes, I basically want it to do nothing apart from run the motor until it hits the switch, then change the value of 'justfed' to 0, then go back to monitoring twitter until it is triggered again

Yes, I basically want it to do nothing apart from run the motor until it hits the switch, then change the value of 'justfed' to 0, then go back to monitoring twitter until it is triggered again

So, use a while loop:

digitalWrite(motorPin, HIGH);
while(digitalRead(limitSwitchPin) != LOW) // Or whatever pressed means for your switch
{
  // Do nothing but wait
}

Thanks Paul, that worked great however now it does not seem to go back to monitoring for tweets now. The code prints 'stopping' to the serial monitor then does nothing, even after the timed (lastattempttime) has passed, here is my code currently.

Thanks again for your help

/*

 
 */
#include <SPI.h>
#include <Ethernet.h>


//variable to prevent overfeeding
boolean justFed = 1;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(192,168,0,3); //<<< ENTER YOUR IP ADDRESS HERE!!!

// initialize the library instance:
EthernetClient client;

const int requestInterval = 30000;  // delay between requests = 1min

char serverName[] = "api.twitter.com";  // twitter URL

boolean requested;                   // whether you've made a request since connecting
long lastAttemptTime = 0;            // last time you connected to the server, in milliseconds

String currentLine = "";            // string to hold the text from server
String tweet = "";                  // string to hold the tweet
boolean readingTweet = false;       // if you're currently reading the tweet
const int StopPin = 4;
const int MotorPin = 3;
const int ShakePin = 2;

void setup() {

//  SET UP PINS

  pinMode(MotorPin, OUTPUT);
  pinMode(ShakePin, OUTPUT);
  pinMode(StopPin, INPUT);  // Cutout Switch
  digitalWrite(StopPin, LOW);  // 
  
  // reserve space for the strings:
  currentLine.reserve(256);
  tweet.reserve(150);

// initialize serial:
  Serial.begin(9600);
  // attempt a DHCP connection:
  if (!Ethernet.begin(mac)) {
    // if DHCP fails, start with a hard-coded address:
    Ethernet.begin(mac, ip);
  }
  // connect to Twitter:
  connectToServer();

  
  Serial.println("Starting up Feedatron4000");

}



void loop()
{
  if (justFed){
    if (client.connected()) {
      if (client.available()) {
        // read incoming bytes:
        char inChar = client.read();

        // add incoming byte to end of line:
        currentLine += inChar; 
  
        // if you get a newline, clear the line:
        if (inChar == '\n') {
          currentLine = "";
        } 
        // if the current line ends with <text>, it will
        // be followed by the tweet:
        if ( currentLine.endsWith("<text>")) {
          // tweet is beginning. Clear the tweet string:
          readingTweet = true; 
          tweet = "";
        }
        // if you're currently reading the bytes of a tweet,
        // add them to the tweet String:
        if (readingTweet) {
          if (inChar != '<') {
            tweet += inChar;
          } 
          else {
            // if you got a "<" character,
            // you've reached the end of the tweet:
            readingTweet = false;
            Serial.println(tweet);
          
        // END OF READING TWEET   
        
            if(tweet == ">feed"){
             digitalWrite(ShakePin, HIGH);
/             Serial.println("SHAKING!");
             delay (4000);
             digitalWrite(ShakePin, LOW);
             digitalWrite(MotorPin, HIGH);
             delay (4500);
             Serial.println("FEEDING!");
             digitalWrite(MotorPin, HIGH);
             while(digitalRead(StopPin) != HIGH) // Or whatever pressed means for your switch
             {
                 // Do nothing but wait
             }
             Serial.println("STOPPING!");
             digitalWrite(MotorPin, LOW);
             justFed = 0;
            
            client.stop(); 
          }
        }
      }   
    
    else if (millis() - lastAttemptTime > requestInterval) {
      // if you're not connected, and two minutes have passed since
      // your last connection, then attempt to connect again:
      connectToServer();
    }
  }
    }
  else if (millis() - lastAttemptTime > 60000){//if three mins has passed since last feeding 14400000(4hrs)
    justFed = 1;
 
 }
}

  
}
void connectToServer() {
  // attempt to connect, and wait a millisecond:
  Serial.println("connecting to server...");
  if (client.connect(serverName, 80)) {
    Serial.println("making HTTP request...");
  // make HTTP GET request to twitter:
    client.println("GET /1/statuses/user_timeline.xml?screen_name=fnordfeeder&count=1 HTTP/1.1");
    client.println("HOST: api.twitter.com");
    client.println();
  }
  // note the time of this connect attempt:
  lastAttemptTime = millis();
}

close bracket in wrong place?

everything is inside the if(justFed) routine.

Yes, that was it! working like a dream now, thanks all