Pages: [1]   Go Down
Author Topic: stop reading from sensor when a value is reached?  (Read 504 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm attempting to display a tweet when a sharp IR sensor "sees" a person in front of the project. The problem I'm having: connecting to twitter, displaying the tweet, etc takes a while and the sensor starts reading again before the tweet is fully read from the API, resulting in no tweets displayed.

Code like this works fine:

Code:
#include <SPI.h>

void setup() {
  Serial.begin(9600);
}

void loop() {

    //while there is no one there, read the sensor until there is someone there
    while (analogRead(0) < 100) {
      Serial.println(analogRead(0));
    }
   
    //now that there is someone there, do something
    Serial.println("sensor is now greater than 100");
 
}

But code like this seems to move to fast for the twitter part

Code:
#include <SPI.h>

void setup() {
  Serial.begin(9600);
}

void loop() {

    //while there is no one there, read the sensor until there is someone there
    while (analogRead(0) < 100) {
      Serial.println(analogRead(0));
    }
   
    //now that there is someone there, do something that takes a while
    <pseudocode> connect to twitter API, get a tweet, display it on LCD </pseudocode>
 
}

Thoughts? Does the loop belong at the end? Thanks from a code newbie!
Logged

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

1.
it doesnt make a big difference if the loop is in the beginning or in the end,
because:
loop() is called again and again in an end-less loop from main()...
2.
u could send debug messages via Serial.print() during the twitter part...
Logged

-Arne

Offline Offline
Full Member
***
Karma: 0
Posts: 101
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What if you make a simple "switch"?

x = 0 > read from sensor
x = 1 > do not read from sensor
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why is it necessary to turn something like this off? Usually you can let the sensors run anyway and and on the reaction side you say when something should happen?

But if you need to just set yourself a stopvariable in the void setup like "stop=1;" , put your code in an if that its running as long stop is 1 and another if for your value goal sets the stop to another int.
Logged

Pages: [1]   Go Up
Jump to: