Pages: [1]   Go Down
Author Topic: edit sketch  (Read 1270 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49433
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A variation on Pauls theme, is to have slower / faster commands that change the delay.
Code:
if (Serial.available())
{
  char ch = Serial.read();
  if (ch == 's')
  {
    delayPeriod += 100;
    Serial.println(delayPeriod);
  }
  elseif (ch == 'f')
  {
    delayPeriod -= 100;
    Serial.println(delayPeriod);
  }
}
Logged

--
My New Arduino Book: http://www.arduinobook.com

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int delayPeriod = 500;
In loop you would have:
Code:
delay(delayPeriod);

rather than:

Code:
delay(500);
Logged

--
My New Arduino Book: http://www.arduinobook.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26348
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Further student-proofing:
Code:
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);
  }
}
Logged

"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.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49433
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if(Serial.available() > 0)
{
   // There is some data to be read...
}
Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Sweden
Offline Offline
Jr. Member
**
Karma: 0
Posts: 84
Arduino System Go!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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:


 
Logged

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
hooking up a potentiometer to the Arduino

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

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

will probably be about right.
Logged

--
My New Arduino Book: http://www.arduinobook.com

Sweden
Offline Offline
Jr. Member
**
Karma: 0
Posts: 84
Arduino System Go!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

:-)

Logged

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

.. and for good measure do:
Code:
Serial.println(delayPeriod);

And open the Serial Monitor
Logged

--
My New Arduino Book: http://www.arduinobook.com

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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




Logged

Pages: [1]   Go Up
Jump to: