Pittsburgh
Offline
Newbie
Karma: 0
Posts: 20
I built a Segway...
|
 |
« on: January 14, 2008, 05:59:23 pm » |
Hello, I have a question for you guys. I need some way to change a variable on the arduino at least, or reprogram it entirely on some type of portable device. The easiest option (I believe) would be to use two buttons and a Binary display, most complicated some sort of Pocket PC with a serial terminal and a serial prompt on the arduino. Does anyone have a simpler idea or an implementation example? Here is my code: (Dists is the variable that I need to modify) int pin = 9; volatile int state = LOW; volatile int Count = 0; volatile int Dists = 1024;
void setup() { pinMode(pin, OUTPUT); attachInterrupt(1, Dist, FALLING); }
void loop() { digitalWrite(pin, HIGH); }
void Dist() { Count++; if (Count >= Dists) { digitalWrite(pin, LOW); } }
PS. Does this code look Ok? It is meant to be a very basic odometer, to make a car travel a certain distance and then stop.
|
|
|
|
|
Logged
|
|
|
|
|
Brisbane, Australia
Offline
God Member
Karma: 0
Posts: 593
|
 |
« Reply #1 on: January 15, 2008, 03:28:13 am » |
How do you need to change it? Just to certain values or to any value?
Easiest way I can think of is storing it in the EEPROM and then getting something to talk to it serially or even just a button to change it.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh
Offline
Newbie
Karma: 0
Posts: 20
I built a Segway...
|
 |
« Reply #2 on: January 15, 2008, 07:02:57 am » |
Right now, I would need to adjust it to any value, but after I test and calibrate the robot, I could limit it down to ~10 specific numbers
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #3 on: January 15, 2008, 07:31:06 am » |
This thread http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1199810491has a discussion and code examples about sending integer values to the arduino using the serial port. If your portable device can connect to the arduino serial port this is one way to do it. If you want to permanently save these values, then you could call functions like these to save and restore you values // functions to read and write integer values into EEPROM // if you only have one value to save, Address is 0; // see http://www.arduino.cc/en/Reference/EEPROM for more info void WriteEEPromInt(int Address, int Value) { EEPROM.write(Address , (unsigned int)Value >> 8); EEPROM.write(Address +1, (unsigned int)Value & 0xff); }
int ReadEEPromInt(int Address ) { return (EEPROM.read(Address) << 8) + EEPROM.read(Address +1) ; }
|
|
|
|
« Last Edit: January 15, 2008, 07:32:57 am by mem »
|
Logged
|
|
|
|
|
Toronto, ON, Canada
Offline
Newbie
Karma: 0
Posts: 12
|
 |
« Reply #4 on: January 15, 2008, 12:43:31 pm » |
Assuming it isn't outside the scope of your project you could add a simple IR receiver circuit to your device that recognizes 0-9 (and something like # as a terminator) from a remote you have lying around.
|
|
|
|
|
Logged
|
Ven Pixel ----------- Wandering Samurai"Since light travels faster than sound, people appear bright until you hear them speak." - George Bernard Shaw
|
|
|
|
Pittsburgh
Offline
Newbie
Karma: 0
Posts: 20
I built a Segway...
|
 |
« Reply #5 on: January 15, 2008, 04:50:29 pm » |
I was considering using serial, but I don't have any easily accessible portable serial terminal devices. I know my PSP, ipod touch, and old Ipaq pda have serial ports, but none(except maybe the ipaq) have serial terminal software. Right now I am leaning towards the IR remote programming. I have many salvaged 38khz IR receivers, so I will be trying that tonight. Also, I have my revised code. It works, reading an IR wheel encoder and controlling the distance of the vehicle. I am building it for the http://soinc.org/events/sample/ElectricVehicle_C08_v13%20_1_.pdfElectric Vehicle event in the Science Olympiad. int pin = 9; int ledPin = 13; volatile int state = LOW; volatile int Count = 0; volatile int Dists = 65;
void setup() { pinMode(pin, OUTPUT); pinMode(ledPin, OUTPUT); // sets the digital pin as output attachInterrupt(1, Dist, FALLING); }
void loop() { digitalWrite(pin, HIGH); }
void Dist() { Count++; if (Count >= Dists) { digitalWrite(pin, LOW); digitalWrite(ledPin, HIGH); // sets the LED on delay(100000); } }
EDIT: I just changed my mind. Iam in the process of obtaining a cybiko classic device, which has a built in serial port and keyboard, and you can easily uase it as a portable serial terminal.
|
|
|
|
« Last Edit: January 16, 2008, 07:04:33 am by kersny »
|
Logged
|
|
|
|
|
Pittsburgh
Offline
Newbie
Karma: 0
Posts: 20
I built a Segway...
|
 |
« Reply #6 on: January 30, 2008, 08:24:39 pm » |
Sorry to bump/double post, but I have another question. I changed my mind yet again, and decided to us an HP 100lx as the terminal. The problem is that the device can only send one byte at a time. Here is my test code: 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) { Serial.println("Type in a Number"); // read the incoming byte: incomingByte = Serial.read();
// say what you got: Serial.print("I received: "); Serial.println(incomingByte, BYTE);
} }
Every time I press a key, the arduino returns with that value. But, If I use the serial monitor in the arduino software, It still sends the data back one byte at a time. I need to send a number, 4-5 characters long, and Have it save to a variable.
|
|
|
|
|
Logged
|
|
|
|
|
Brisbane, Australia
Offline
God Member
Karma: 0
Posts: 593
|
 |
« Reply #7 on: January 30, 2008, 09:38:11 pm » |
Every time I press a key, the arduino returns with that value. But, If I use the serial monitor in the arduino software, It still sends the data back one byte at a time. I need to send a number, 4-5 characters long, and Have it save to a variable. You need a buffer and a end byte. So when you get a byte you stick it in the buffer. If its the end byte (0x00?) then you process the instruction.
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #8 on: January 31, 2008, 02:15:14 am » |
... If I use the serial monitor in the arduino software, It still sends the data back one byte at a time. I need to send a number, 4-5 characters long, and Have it save to a variable. If you just want to receive integer values then you may want to look at the code linked in the beginning of reply#3.
|
|
|
|
« Last Edit: January 31, 2008, 02:17:00 am by mem »
|
Logged
|
|
|
|
|
Pittsburgh
Offline
Newbie
Karma: 0
Posts: 20
I built a Segway...
|
 |
« Reply #9 on: January 31, 2008, 06:59:22 am » |
I looked at that code, and will it compile and go right on the arduino, or is it just a function. It has no Setup or loop functions, so I wondered what else it would take to run it.
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #10 on: January 31, 2008, 08:31:32 am » |
it is a function to be included in a sketch. But here is the key functionality in a sketch similar to yours 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() >= 2 ){ uint8_t msb = Serial.read(); // this assumes the msb is sent first, uint8_t lsb = Serial.read(); incomingByte = lsb + (msb <<8);
Serial.println("Type in a Number"); // say what you got: Serial.print("I received: "); Serial.println(incomingByte, BYTE); } } Note that you need to send your integer as two bytes for this to work.
|
|
|
|
« Last Edit: January 31, 2008, 08:32:10 am by mem »
|
Logged
|
|
|
|
|
|