How to off analogRead from reading value ? URGENT

I would like to know when button 's' if pressed, then the analogRead will stop reading value from sensor.

Read this for how to read a button

Then use an if statement to see if you need to perform an analogue read function.

Grumpy_Mike:
Read this for how to read a button
http://arduino.cc/en/Tutorial/DigitalReadSerial
Then use an if statement to see if you need to perform an analogue read function.

For my case, i mean when char 's' is detected not the digitalpin from arduino

int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

               if(incomingByte != 's') {
                // put here the code to read the analogue input and do what you want with it
             }
        }
}

Posting some code to show what you've done so far would help.

I assume you mean an 's' from the computer keyboard via usb from say the serial monitor?- if so have you got Serial.read() working to monitor what's coming in? Then use that result to control what you do using something like a while or an if

EDIT.... just like G_M said 8)

JimboZA:
Posting some code to show what you've done so far would help.

I assume you mean an 's' from the computer keyboard via usb from say the serial monitor?- if so have you got Serial.read() working to monitor what's coming in? Then use that result to control what you do using something like a while or an if

EDIT.... just like G_M said 8)

Ya... you get what i mean. I know when coming data 's'/ 'S', then i wan know the code on how to stop the sensor from sensing any values? or switch off the sensor.

then i wan know the code on how to stop the sensor from sensing any values? or switch off the sensor.

You are not clear in what you are asking.
You do not write code to stop a sensor sensing, you write code to read the value from a sensor.
What sort of sensor do you have, it is not normal to turn off a sensor your code should simply not read it.

You need to specify what you want to do with the readings from your sensor.

Is this homework?

Grumpy_Mike:

then i wan know the code on how to stop the sensor from sensing any values? or switch off the sensor.

You are not clear in what you are asking.
You do not write code to stop a sensor sensing, you write code to read the value from a sensor.
What sort of sensor do you have, it is not normal to turn off a sensor your code should simply not read it.

You need to specify what you want to do with the readings from your sensor.

Is this homework?

I am using Hall effect sensor.

#define NOFIELD 505L // Analog output with no applied field, calibrate this
// This is used to convert the analog voltage reading to milliGauss
#define TOMILLIGAUSS 1953L // For A1301: 2.5mV = 1Gauss, and 1024 analog steps = 5V, so 1 step = 1953mG
void setup()
{
Serial.begin(9600);

}
void DoMeasurement()
{
char btn;
btn=(char)Serial.read();
int flag=0;
if(btn==‘a’){//activate sensor
flag =1;
while(flag==1)
{
// measure magnetic field
int raw = analogRead(A0); // Range : 0…1024
long compensated = raw - NOFIELD; // adjust relative to no applied field
long gauss = compensated * TOMILLIGAUSS / 1000; // adjust scale to Gauss
if (gauss <0)
{
Serial.print(gauss*-1);
Serial.println();
}
else{
Serial.print(gauss);
Serial.println();
if(btn==‘s’){
//here i dont know what to write
}
}}}}
void loop()
{
delay(5);
DoMeasurement();
}

I think you may need to re-structure a bit, perhaps put G_M's suggestion in the loop

 if(incomingByte != 's') {
                DoMeasurement();
             }

JimboZA:
I think you may need to re-structure a bit, perhaps put G_M's suggestion in the loop

 if(incomingByte != 's') {

DoMeasurement();
             }

Cannot work, the system will hang.

Why does a function called DoMeasurement do anything at all to the serial port?

you need a small state machine , OK a gift because it is Christmas

// see also - http://playground.arduino.cc/Code/HallEffect

#define NOFIELD 505L    // Analog output with no applied field, calibrate this
// Uncomment one of the lines below according to device in use A1301 or A1302
// This is used to convert the analog voltage reading to milliGauss
#define TOMILLIGAUSS 1953L  // For A1301: 2.5mV = 1Gauss, and 1024 analog steps = 5V, so 1 step = 1953mG
// #define TOMILLIGAUSS 3756L  // For A1302: 1.3mV = 1Gauss, and 1024 analog steps = 5V, so 1 step = 3756mG


int incomingByte = 0;   // for incoming serial data

int state = 0;   // 0 == off,    1 == start

void setup() 
{
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop()
{
  if (Serial.available() > 0) 
  {
    incomingByte = Serial.read();
    if (incomingByte == 's') state = 1;     // s = start
    if (incomingByte == 'q') state = 0;   // q = quit
  }

  if (state == 1) DoMeasurement();

}

void DoMeasurement()
{
  int raw = analogRead(A0);   // Range : 0..1023
  long compensated = raw - NOFIELD;                 // adjust relative to no applied field 
  long gauss = compensated * TOMILLIGAUSS / 1000;   // adjust scale to Gauss

  if (gauss < 0) Serial.println(gauss * -1);
  else Serial.println(gauss);
}

Ok Rob, I have to ask.... how did you get the "not tested" into the code tag?

you can edit the code tags, but its a secret, don't tell anyone :wink:

[ code=secret]

off course without the space ..

We live and learn...

check - http://docs.simplemachines.org/index.php?topic=735.0 -

Is your HTML study going well?

robtillaart:
check - Alphabetical list of all bulletin board codes - Online Manual -

Is your HTML study going well?

Yes thanks, busy with forms and cgi as we speak......