How to read something in once?

In my home project, I'd like to set the delay of the arduino through a c# app. The tricky side of the thing is, that I only want to send and read the value once. The send side is not a problem. I convert the value to a string and serialport1.write() programmed to a button press. I press the button and the the string is sent to the port. How can I read it and convert it only once?

In my home project, I'd like to set the delay of the arduino through a c# app.

Do you mean change a delay in arduino side? What is the reason for this?

As it will be sent only once it can only be read only once from the serial port.

So you can wait for the value to arrive in setup() , capture the chars until a \n and then convert the string with atoi() or so

Yes, I meant on the arduino side. The reason behind it, is that I want to monitor the voltage of a solar panel and I want to make measurment with the ardu like one per minute. It’s needless to make measurements in every second, sunlight intensity doesn’t change that rapidly.

How should I capture it in the void setup() ?

You read it in the function "setup()" in almost the same way you would read it in the function "loop()", except you must put in you own loop to wait for the data to come in.

Ok... I made a code like this: char rc[10]; int c;

void setup() { Serial.begin(9600); void loop() { if(Serial.available) { rc=Serial.read(); c=atoi(rc) } } } void loop() { int sensorvalue = analogRead(A0); double voltage = sensorvalue * (5.0 / 1023.0); Serial.print(voltage); delay(c); }

But it's not working... could you point out my mistakes? I would be greatly in your debt! :)

Mark5217:
Yes, I meant on the arduino side. The reason behind it, is that I want to monitor the voltage of a solar panel and I want to make measurment with the ardu like one per minute. It’s needless to make measurements in every second, sunlight intensity doesn’t change that rapidly.

How should I capture it in the void setup() ?

Why not sample as often as possible and do averaging. Solar fluctuations can be quite steep when clouds obstruct direct sunlight.

Because I want to give the user a chance to set the measuring rate. I know that more often is more accurate, but I want my program to be user friendly as much as it's possible.

could you point out my mistakes?

The idea is good but: - can't believe this code works. Please post the actual code - function declaration of loop() inside setup() is not allowed - assignment to rc array is incorrect - delay value can be converted once in setup instead of every time in loop - variable names are too cryptic (warning, no error;) - current application is blocking, use the delay time in a construct like in the "blink without delay" example then you can do more - no code tags ;)

...

Mark5217: Because I want to give the user a chance to set the measuring rate. I know that more often is more accurate, but I want my program to be user friendly as much as it's possible.

But you can do the averaging for {delay} time and then print the average.

The user can still configure the time between he sees results...

Ok... I made a code like this: char rc[10]; int c;

void setup() { Serial.begin(9600); void loop() { if(Serial.available) { rc=Serial.read(); c=atoi(rc) } } } void loop() { int sensorvalue = analogRead(A0); double voltage = sensorvalue * (5.0 / 1023.0); Serial.print(voltage); delay(c); }

But it's now working... could you point out my mistakes?

Let's assume that was supposed to say "not working".

You wait for (good) a single character (maybe not so good) and try to put it into a char pointer (very bad).

I'm planning to do avarageing as well. But first I'd like to sort out this.

Yes the code doesn't work. That's right. :) Then where should I declare the loop. I already declared it once. Can it be done multiple times? I already corrected some of the mistakes you mentioned as soon as I posted the code. Here it is now: char rc[10]; int c;

void setup() { Serial.begin(9600); } void loop() { if(Serial.available()) { rc=Serial.read(); c=atoi(rc); } int sensorvalue = analogRead(A0); double voltage = sensorvalue * (5.0 / 1023.0); Serial.print(voltage); delay(c); }

Still now working. It says this is the problem: incompatible types in assignment of 'int' to 'char [10]' Oh I realized I wrote now instead of not in one of my comments. Sorry about that. :P But the serial.available() part will repeat too. Will it not give the c a 0 value if it won't receive any data at every turn of the loop?

try this

char tmp[10];
int idx = 0;
int c = 0;

unsigned long sum = 0;
unsigned long measurements = 0;
unsigned long waitTime = 0;
unsigned long lastTime = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("Please enter number");

  // IDE terminal does not sent an \n  that's why I took z :(
  while (c != 'z') 
  {
    if(Serial.available() )
    {
      c = Serial.read();  // TODO check for digits only?
      tmp[idx++] = c;
      // TODO overflow of the array
    }
  }
  tmp[idx] = 0;
  waitTime = atoi(tmp);
  lastTime = millis();
  Serial.println("Starting measurements...");
}

void loop() 
{
  sum += analogRead(A0);
  measurements++;

  if (millis() - lastTime > waitTime)
  {
    lastTime += waitTime;
    double voltage = sum * 5.0 / 1023.0 / measurements;  // TODO merge to one constant
    // TODO appropriate output layout
    Serial.print(measurements);
    Serial.print("\t");
    Serial.println(voltage);
    // reset 
    sum = 0;
    measurements = 0;
  }

  // TODO you can do additional things here in same way e.g. light and temperature sensor
}

@Mark5217

Read this before posting a programming question

How to use this forum

Code tags, thanks.

http://www.gammon.com.au/serial

robtillaart I get the idea, but what do you need last time for?

robtillaart:  while (c != 'z')

What is the 'z' ?

'z' is a character, with the value z -

I explained in the code : // IDE terminal does not sent an \n that's why I took z :( didn't you read that?