Show Posts
Pages: [1] 2 3 4
1  Using Arduino / Programming Questions / Re: Pin control within void/loop ifstatement on: April 01, 2013, 02:21:23 pm
Yes, that was it! working like a dream now, thanks all
2  Using Arduino / Programming Questions / Re: Pin control within void/loop ifstatement on: April 01, 2013, 01:14:31 pm
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

Code:
/*

 
 */
#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();
}   
 

3  Using Arduino / Programming Questions / Re: Pin control within void/loop ifstatement on: April 01, 2013, 12:17:43 pm
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
4  Using Arduino / Programming Questions / Pin control within void/loop ifstatement on: April 01, 2013, 11:40:50 am
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


Code:
/*

 
 */
#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();
}   
 




5  Community / Exhibition / Gallery / Arduino controlled animatronic ED-209 on: February 01, 2013, 05:01:23 am
I am in the process of turning a 15" high Hot-Toys model of ED-209 from Robocop into an animatronic robit,  controlled by Arduino.

http://fnordcorps.com/project-209

I am currently at the CNC stage of the build but thought I would post this in case anyone wants to keep up with my progress.

In total there will be five stepper motors,  all controlled by an arduino. I plan to have an arduino in the base communicting wirelessly via Xbee to a board in the main body of ED.  This is mainly to keep the amount of wires I need to pass to the head at a minimum.

I hope to sync the the motion to the sound effects of the '20 seconds to comply'  sequence from the film.  I also hope to get some motion tracking in there If I have not lost the will to live by the end of it (and if I have any space left)

Checkout my link above for more details. 

6  Using Arduino / Programming Questions / Re: digital read/write not working within switch/case serial loop on: December 10, 2012, 03:45:33 pm
Ok,
    solved this, thanks for your help PaulS but I think part of my problem was explaining my problem. After many an hour head scratching
I have solved it.

My problem was having the switch/case inside the if (serial.available(){ so monitoring of the input could only happen whilst it was receiving serial data.

I moved it outside of the IF statement so that all the above did was write the values to key and val and it worked a charm

void loop() {

if( Serial.available() >= 2){
    key = Serial.read();
    val = Serial.read(); 
}

    if (key == 4 && val == 1){ mode = 1;}
    if (key == 5 && val == 1){ mode = 2;}
    if (key == 13 && val == 1){ mode = 3;}
   

    switch (mode){

Thanks again for help
7  Using Arduino / Programming Questions / Re: digital read/write not working within switch/case serial loop on: December 10, 2012, 02:44:21 pm
Sorry, more information. I am using a Bluetooth module attached to the arduino. I am using an android app which when you set up a touchbutton and press it, it sends two bytes of information (key,val) over bluetooth to the serial port. This is all working great and triggers the right pins when the right touch button is pressed.

Because the app does not have a serial monitor I cannot see what is going on, the only way I know is by using LEd's on the pins to monitor the mode changes.  Using the PC serial monitor as well as the BT module does not work as I think the BT module is the serial input/output port. (I am a beginner so I am still working all this out)

I know that there is no problem with monitoring the switch input because It does switch the mode if I am already making the StopPin HIGH when the mode starts (if I hold down the StopPin button whilst pressing the button on the app).  

I know this is because I have the mode changes within the IF serial.available statement and changes can only be made at the same time as serial information is being recieved. I cannot work out a way to have the StopPin change the mode, within the Switch/case block whilst Mode 6 is already running, without having to recieve serial data at the same time.

Sorry if this is confusing as hell
8  Using Arduino / Programming Questions / Re: digital read/write not working within switch/case serial loop on: December 10, 2012, 02:26:44 pm
The way I understand it my code is operating as the following at the moment:

If Serial bytes received, read bytes and change mode based on the byte value. Only read the button value if serial data is being received.

What I need it to do is:

If serial bytes received, read bytes and change mode based on the byte value. when mode 6 is running, monitor StopPin and if value goes high change mode to 7.

I am having trouble finding exactly where in my code I need to put the monitor to allow me to switch the mode without simultaneously receiving data from the serial port.
9  Using Arduino / Programming Questions / Re: digital read/write not working within switch/case serial loop on: December 10, 2012, 02:10:25 pm
Quote
You need to add a Serial.print() statement to loop, to print out the value in mode.

Thanks, can you explain to me where in the loop and what this would achieve?

If it is just to monitor the state of the button I can't actually view the serial port output as the app does not support it. Or is this to output the button state to the serial port to allow it to be read by the program?

I know the switch works because If I hold it whilst the key is pressed the mode does change (as indicated by the test LED lighting up after 3 seconds)

Sorry but I am trying to understand why this would work rather than 'it works'

Thanks
10  Using Arduino / Programming Questions / Re: digital read/write not working within switch/case serial loop on: December 10, 2012, 01:28:27 pm
Quote
Anytime you are in mode 6, you can read the switch state, and change to a different mode if the state is to your liking.

I have tried adding the following to within the (mode = 6) code:

Code:
case 6:  // Run Motor until StopPin pressed
      digitalWrite (FeedPin, HIGH);
      buttonState = digitalRead(StopPin);
      if (buttonState == HIGH) { mode = 7;}
      break;
     
    case 7:  // Reset all variables and go to wait mode
      digitalWrite (TestPin,HIGH);
      digitalWrite (FeedPin, LOW);
      key = 0;
      val = 0;
      mode = 0;
      break;


    }
  }
}

The TestPin does not go HIGH and the FeedPin does not go LOW when the StopPin switch is pressed whilst mode = 6 is running.

I am trying to find out where in my code I need to monitor the StopPin to allow me to interrupt (sorry, change mode) whilst mode
6 is running. It currently does not. Can anyone suggest where I need to monitor the switch to allow the mode to be changed






11  Using Arduino / Programming Questions / Re: digital read/write not working within switch/case serial loop on: December 10, 2012, 12:49:49 pm
Thanks, Can I change the mode within another IF statement from outside the IF statement? I need to be able to interupt {mode = 6} with
the switch, making the program stop? I thought that is what I had done already, unsuccesfully
12  Using Arduino / Programming Questions / Re: digital read/write not working within switch/case serial loop on: December 10, 2012, 12:15:57 pm
Hi,
 no there is only serial data when I press a button on the andorid app that is communicating with the serial port, I see the problem now as it can only read the button state when the serial data is being sent, due to it being within the serials IF statement.

Any idea how I can implement reading the pin state and changing the mode whilst also monitoring the serial input?
13  Using Arduino / Programming Questions / Re: digital read/write not working within switch/case serial loop on: December 10, 2012, 11:43:59 am
ok, reformatted - anyway, here is the problem -

when the (buttonState == HIGH)  it does not switch the case to (mode = 7), turning the pin to low and returning the mode to 0.

I have an LED attached to the FeedPin at the moment. When I trigger [key 5] the LED lights up for three seconds, off for a second then
back on, which indicates that it is succesfully running mode-2 then switching to mode-6 with no problems, however the LED does not turn off when I press the switch attached to the StopPin, which indicates to me that it is not switching to Mode-7.

(changed slightly from original posted code)

Code:
void loop() {


  if( Serial.available() >= 2){

    buttonState = digitalRead(StopPin); //Is the stop pin high

    int key = Serial.read();
    int val = Serial.read();


if ( mode == 0 && val == 1)
    {
       if (key == 4)
          { mode = 1;}
       if (key == 5)
          { mode = 2;}
       if (key == 13)
          { mode = 3;}
       if (buttonState == HIGH)
          { mode = 7;}
    }

    switch (mode){

    case 0:  // do nothing and wait
      break;

    case 1:  // Shake
      digitalWrite (ShakePin, HIGH);
      delay (5000);
      digitalWrite (ShakePin, LOW);
      mode = 5;
      break;



    case 2:  // Feed
      digitalWrite (FeedPin, HIGH);
      delay (3000);
      digitalWrite (FeedPin, LOW);
      delay (1000);
      mode = 6;
      break;

    case 3:   //LED Test

      digitalWrite (TestPin,HIGH);
      delay (3000);
      digitalWrite (TestPin,LOW);
      mode = 5;
      break;

    case 5:  // Reset all variables and go to wait mode
      key = 0;
      val = 0;
      mode = 0;
      break;

    case 6:  // Run Motor until Interupt from StopPin
      digitalWrite (FeedPin, HIGH);
      break;
     
    case 7:  // Reset all variables and go to wait mode
      digitalWrite (FeedPin, LOW);
      key = 0;
      val = 0;
      mode = 0;
      break;


    }
  }
}

14  Using Arduino / Programming Questions / digital read/write not working within switch/case serial loop on: December 10, 2012, 11:11:04 am
Hi,
   I am trying to get a digitalRead/Write statement to switch the mode within my Switch/Case loop.

I want to be able to switch the mode to (mode = 5) when the button attached to 'StopPin' is pressed.
Preferably only whilst (mode = 6) is running.

I know the wiring to the button/Test LED is right as I have tested it with the bit of code greyed out -

//    buttonState = digitalRead(StopPin);   
//    if (buttonState == HIGH) {digitalWrite(TestPin, HIGH);}

so I know the switch works fine. My problem is as soon as I move this to within the IF variables in the code
it stops working. I have tried to get it to light up the Test LED and switch the mode with no luck. 

I am assuming it is something to do with the fact it is working alongside the serial input (which all works great)
but I cannot see why. I have tried moving the read/write to different parts of the loop but cant seem to get it
to do anything. Can anyone see why?

Thanks in advance

Code:
const int ShakePin = 4;
const int FeedPin = 5;
const int StopPin = 8;
const int TestPin = 13;
byte mode;
int buttonState = 0;


void setup()
{
  pinMode(ShakePin, OUTPUT);  //Shaker Relay Pin
  digitalWrite(ShakePin, LOW);  //
 
  pinMode(FeedPin, OUTPUT);  // Feeder Relay Pin
  digitalWrite(FeedPin, LOW);  //
 
  pinMode(StopPin, INPUT);  // Cutout Switch
  digitalWrite(StopPin, LOW);  //
 
  pinMode(TestPin, OUTPUT);  // pin 12 shakermotor as OUTPUT
  digitalWrite(TestPin, LOW);  // pin 12 shakermotor as OUTPUT
 
  Serial.begin(115200);       // start serial communication at 115200bps

}
 
void loop() {
//    buttonState = digitalRead(StopPin);   
//    if (buttonState == HIGH) {digitalWrite(TestPin, HIGH);}
 
  if( Serial.available() >= 2){
   
    buttonState = digitalRead(StopPin); //Is the stop pin high
   
    int key = Serial.read();
    int val = Serial.read();
   
   
    if ( mode == 0 && key == 4 && val == 1){ mode = 1;}
    if ( mode == 0 && key == 5 && val == 1){ mode = 2;}
    if ( mode == 0 && key == 13 && val == 1){ mode = 3;}
    if (buttonState == HIGH) {mode = 5;}   // If Pin is high light the stopPin
 
 //   if ( mode == 6 && buttonState == HIGH){ mode = 5;}
 //   if (buttonState == HIGH) {digitalWrite(TestPin, HIGH);}   // If Pin is high light the TestLED
   
    switch (mode){
   
case 0:  // do nothing and wait
break;

case 1:  // Shake
    digitalWrite (ShakePin, HIGH);
    delay (5000);
    digitalWrite (ShakePin, LOW);
    mode = 5;
break;


   
case 2:  // Feed
    digitalWrite (FeedPin, HIGH);
    delay (3000);
    digitalWrite (FeedPin, LOW);
    delay (1000);
    mode = 6;
break;

case 3:   //LED Test
     
     digitalWrite (TestPin,HIGH);
     delay (3000);
     digitalWrite (TestPin,LOW);
     mode = 5;
break;

case 5:  // Reset all variables and go to wait mode
     key = 0;
     val = 0;
     mode = 0;
break;

case 6:  // Run Motor until Interupt from StopPin
     digitalWrite (FeedPin, HIGH);
break;

     
  }
  }
}
15  Using Arduino / Programming Questions / Re: Serial Read with Switch/Case problem on: December 05, 2012, 06:07:06 pm
Ahh, I have been staring at this code for two hours and missed that every time. Damn it.

Changed the relevant lines to digitalWrite and it now works great.  Thanks again Paul! I
Can now get on with the mechanical side of things.

Thanks
Pages: [1] 2 3 4