Go Down

Topic: edit sketch (Read 1 time) previous topic - next topic

jmnijsse

Hi,

I'm new here. And I'm very enthusiastic about Arduino. Low price, huge opportunities.
I use Arduino at this moment to get one or more analog values in MSExcel (with PLX-DAQ).
In the sketch I set the samplefrequency (=delay) at p.e. 500, a half second.
So, in Excel comes the data each half second.
But when I will change this frequency, I have to open the sketch, change te right thing, upload, and back to excel.

Is there any solution to do this special thing (change delay time) without typing in the sketch? Perhaps in a small form with a input field and an upload-button, out of the whole program.

(In future I will pupils (12-14 years) let work with this. But they don't have to understand the C-code. They only have to change the frequency of the sample.)

Next step is: change sample-time. But thats an Excel question, I think.

Greetings,

Johan

TOA Natuurkunde
Calvijn College Goes (www.calvijncollege.nl)
The Netherlands

PaulS

You could have a default sample time. Then, add some code that looks for serial data to provide a new sample time. Open the Serial Monitor, and type in a new interval, with a terminator (250!, for instance).

The Arduino would read the data sent ("250!"), until it encountered the terminator ("!"), and convert the character data to an integer (atoi()), to define a new interval. Opening the Serial Monitor resets the Arduino, as does closing it, so, you might want to store the interval in EEPROM. Or not, so the students don't set intervals of every two weeks or something silly.

Si

A variation on Pauls theme, is to have slower / faster commands that change the delay.
Code: [Select]

if (Serial.available())
{
  char ch = Serial.read();
  if (ch == 's')
  {
    delayPeriod += 100;
    Serial.println(delayPeriod);
  }
  elseif (ch == 'f')
  {
    delayPeriod -= 100;
    Serial.println(delayPeriod);
  }
}
--
My New Arduino Book: http://www.arduinobook.com

jmnijsse

Thanks for answering.

This is my little code:

void setup(){
  Serial.begin(9600);
  Serial.println("CLEARDATA")
}

void loop() {
  int sensorvalue = analogRead(0) //potmeter at analog input 0
  Serial.print("DATA, ,");
  Serial.println(sensorvalue);
  delay (500);
}

Upload in Arduino.
Now I start PLX-DAQ, the Excel Macro, and 'connect' (http://www.parallax.com/tabid/393/Default.aspx).
Data comes up in Excel, row after row, each 500 ms.

This PLX-DAQ uses COM1.
But when I will change with SerialMonitor it uses COM1 too. And thats not possible, I mean.

And how will the sketch know when a serial command comes in and wherefor it is?

@Si,

Is 'delayPeriod' the same as 'delay'?

Sorry, i'm a very newbee

Gr.
Johan

Si

Quote
Is 'delayPeriod' the same as 'delay'?

Sorry, i'm a very newbee


No, its a variable I invented for you, to hold the time delay in milliseconds. At the top of your sketch, you would have:
Code: [Select]

int delayPeriod = 500;

In loop you would have:
Code: [Select]

delay(delayPeriod);


rather than:

Code: [Select]

delay(500);
--
My New Arduino Book: http://www.arduinobook.com

AWOL

Further student-proofing:
Code: [Select]
if (Serial.available())
{
  char ch = Serial.read();
  if (ch == 's')
  {
    delayPeriod += 100;
    delayPeriod = min (delayperiod, SOME_MAX_PERIOD);  //SOME_MAX_PERIOD tbd by you
    Serial.println(delayPeriod);
  }
  elseif (ch == 'f')
  {
    delayPeriod -= 100;
    delayPeriod = max (delayperiod, 100);
    Serial.println(delayPeriod);
  }
}
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
This PLX-DAQ uses COM1.
But when I will change with SerialMonitor it uses COM1 too. And thats not possible, I mean.

And how will the sketch know when a serial command comes in and wherefor it is?

So, you won't be able to dynamically change the delay time. Unless you use a different approach - maybe read a potentiometer setting, and map the result to the minimum and maximum range you define.

The Serial.available() function reports how much serial data is waiting to be read.
Code: [Select]
if(Serial.available() > 0)
{
   // There is some data to be read...
}

jmnijsse

Thanks.
I'll try it tomorrow.
My working day is ended...


Anders 2009


An completely alternative approach, which might be even simpler for the kids to use, is to consider hooking up a potentiometer to the Arduino and let that decide the interval. This way the kids can just turn a knob to set the interval and you just have to read for the device and never write to it.

Something like this:



Si

Quote
hooking up a potentiometer to the Arduino


Sounds like a good idea. Have a look at 'map' in the Adruino documentation.

Code: [Select]

delayPeriod = map(potReading, 0, 1023, 100, 10000);


will probably be about right.
--
My New Arduino Book: http://www.arduinobook.com

Anders 2009

Exactly!, and if you really want to go fancy, you could perhaps display the delayPeriod value on an LCD, bolt the pot and the LCD onto a nice box, stick the the Arduiono in it and you've got yourself a neat control panel.

:-)


Si

.. and for good measure do:
Code: [Select]

Serial.println(delayPeriod);


And open the Serial Monitor
--
My New Arduino Book: http://www.arduinobook.com

jmnijsse

All great idea's. Potentiometer, an LCD.
All of you: Thanks a lot!

If I had more questions, I'll be back here...





Go Up