Using LDR as a alert at anytime during program

Hi,
I am doing this project on home sensing project that 3 sensors that measure out values and send it to Xively. I was wondering about whether the LDR could trigger twitter alert (when LDR did not detect any light, it will send a twitter message) at any duration of the program. Here's my codng

#include <Twitter.h>

#include <CountingStream.h>
#include <Xively.h>
#include <XivelyClient.h>
#include <XivelyDatastream.h>
#include <XivelyFeed.h>



#include <b64.h>
#include <HttpClient.h>


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

#include <Xively.h>

#define API_KEY "zuivpbOAGm6DnvmbWzKifFhcAaLwxozmw9GRu4saiFfjNHsT" // your Xively API key goes here
#define FEED_ID 388783128 // your Xively feed ID goes here


byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x9D, 0x98 };
Twitter twitter("2652700802-OGD14O7hWL6OI0sNFkVUGEUsaWyxPXtK4mXYrnT");


const int lightSensorPin=2;
const int tempPin1=3;
const int tempPin2=4;
char tweetText[140];
unsigned long lastConnectionTime = 0;                // last time we connected to Xively
const unsigned long connectionInterval = 15000;      // delay between connecting to Xively in milliseconds
int count = 4;



char sensorId1[] = "light";  // Light level (LDR)
char sensorId2[] = "temp1";  // Temperature sensor 1  (TMP36)
char sensorId3[] = "temp2";  // Temperature sensor 2  (TMP36)
 

XivelyDatastream datastreams[] = {
  XivelyDatastream(sensorId1, strlen(sensorId1), DATASTREAM_FLOAT),
  XivelyDatastream(sensorId2, strlen(sensorId2), DATASTREAM_FLOAT),
  XivelyDatastream(sensorId3, strlen(sensorId3), DATASTREAM_FLOAT),
};


XivelyFeed feed(FEED_ID, datastreams, 3 );


EthernetClient client;
XivelyClient xivelyclient(client);

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing network");
  while (Ethernet.begin(mac) != 1) {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(15000);
  }
  Serial.println("Network initialized");
  Serial.println("Ready.");
}

void loop() {

  if (millis() - lastConnectionTime > connectionInterval) {
    
    
    float lightLevel = map(analogRead(lightSensorPin),0,1023,0,100);
    if(lightLevel < 30)
    {
      sprintf(tweetText, "Alert. %d", count);
      tweet(tweetText);
      count++;
    }
    
      
    sendData(0, lightLevel);  
    
    
    getData(0); 
    
    float temperature1 = ((getVoltage(tempPin1) -.5) * 100L);
    
    
    
    sendData(1, temperature1);
    
    
    getData(1);
    
    
    float temperature2 = ((getVoltage(tempPin2) -.5) * 100L);
    
   
    sendData(2, temperature2); 
    
    
    getData(2);
    
    lastConnectionTime = millis();
 
    Serial.println("Waiting for next reading");
    Serial.println("========================");
    
    
  }
}

// send the supplied value to Xively, printing some debug information as we go
void sendData(int streamIndexNumber, float sensorValue) {
  datastreams[streamIndexNumber].setFloat(sensorValue);~~~

  Serial.print("Sensor value is: ");
  Serial.println(datastreams[streamIndexNumber].getFloat());

  Serial.println("Uploading to Xively");
  int ret = xivelyclient.put(feed, API_KEY);
  //Serial.print("PUT return code: ");
  //Serial.println(ret);
}

// get the value of the datastream from Xively, printing out the values we received
void getData(int stream) {
  Serial.println("Reading the data back from Xively");

  int request = xivelyclient.get(feed, API_KEY);
  //Serial.print("GET return code: ");
  //Serial.println(request);

  if (request > 0) {
    Serial.print("Datastream: ");
    Serial.println(feed[stream]);

    Serial.print("Sensor value: ");
    Serial.println(feed[stream].getFloat());
    Serial.println("========================");
  }
}

float getVoltage(int pin){
//Converts from a 0 to 1024 digital reading from 0 to 5 volts
//in ~ 5 millivolt increments
return (analogRead(pin) * .004882814);
}
void tweet(char msg[])
{
  Serial.println("connecting ...");
  if (twitter.post(msg))
  {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200)
    {
      Serial.println("OK.");
    } 
    else
    {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } 
  else
  {
    Serial.println("connection failed.");
  }

}

Sorry if it was a tedious code (P.S Most of the coding is not by me, its from the Dummies book on Arduino Projects)

float lightLevel = map(analogRead(lightSensorPin),0,1023,0,100);
    if(lightLevel < 30)
    {
      sprintf(tweetText, "Alert. %d", count);
      tweet(tweetText);
      count++;
    }

Don't you do it here? It's not working? If you dont know what this piece of code does, it reads LDR's value at Analog Pin 2 then maps its value between 0 and 100, if the value is less than "30" it tweets "tweetText".

omersiar:

float lightLevel = map(analogRead(lightSensorPin),0,1023,0,100);

if(lightLevel < 30)
    {
      sprintf(tweetText, "Alert. %d", count);
      tweet(tweetText);
      count++;
    }




Don't you do it here? It's not working? If you dont know what this piece of code does, it reads LDR's value at Analog Pin 2 then maps its value between 0 and 100, if the value is less than "30" it tweets "tweetText".

I know what it does...but that's not what i ask...what i ask is whether is there a way to send a tweet in any time during the program...i can only send a tweet from there before they send the data to xively, after that i can't send a tweet anymore...

if (millis() - lastConnectionTime > connectionInterval)

This code after "void loop" makes loop runs at specific time which is "time since arduino running" minus "lastConnectionTime" if it is greater than "connectionInterval" loop runs else waits and you can't send tweets

I encourage you to be more aggressive about manipulating codes, there is nothing you can not roll back

Hi, if you use an "interupt" set to trigger whenever the value drops below say 30 (or whatever value you choose), and have that act as the trigger to send your tweet.
While I'm not very good at coding, I had a similar problem with a project last year of a camera triggered flash control box not allways triggering which was due to the program only checking periodically and had to modify so the input from the camera interupted the program to trigger things.

Interrupts are best avoided if possible, and when required used only reluctantly and to the minimum extent necessary. I don't think this problem requires interrupts.

PeterH:
Interrupts are best avoided if possible, and when required used only reluctantly and to the minimum extent necessary. I don't think this problem requires interrupts.

So what do you think i should do? i am out of ideas...

I don't unde and how you mean you can send it once?

Why not call xively before the twitter post? The code I understand but the system ie how to post tweets, what are the steps involved!
Xively