Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Zero Padding or Leading Zeros for integers on: February 09, 2013, 12:15:04 am
Here is example code for using sprintf() to print formatted integers and dtostrf() for floats that works:
(not very elegant, but then neither is C, esp for printing.)

Code:
/* Print padded (aligned) integers and floats from Arduino environment 
 *  sprintf() for integers          www.cplusplus.com/reference/cstdio/printf/
 *  and dtostrf() for floats        dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER); gist.github.com/2343665   www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#g6c140bdd3b9bd740a1490137317caa44
 */

int n;
int m;
byte i = 1;
float x;
char c[10];        // long enough to hold complete integer string
char f[10];        // long enough to hold complete floating string

void setup() {     
  randomSeed(1); 
  Serial.begin(57600);
}

void loop() {
  n = random(-1000,1000);                    // generate a random integer
  x = n/1000.0;                              // convert to a float <= |1.0|

  m = sprintf(c, " %3d:  %5d    ", i,n);    // build integer string using C integer formatters  (m is length, and not used in this code)
  Serial.print(c);   

  dtostrf(x,6,3,f);                          // -n.nnn Use this for a consistent float format
  Serial.println(f);
  i++;
  delay(1000);               
}



Output:
   1:   -193    -0.193
   2:    249     0.249
   3:   -927    -0.927
   4:    658     0.658
   5:    -70    -0.070
   6:    272     0.272
   7:    544     0.544
   8:   -122    -0.122
   9:    923     0.923
  10:    709     0.709
  11:   -560    -0.560
  12:   -835    -0.835

2  Using Arduino / Programming Questions / Re: implementing a getc() function; always needs a <return> in Serial Monitor? on: December 16, 2012, 07:23:19 pm
Discovered the answer:
Yes, using the Arduino's serial monitor, the Send btn or Return key are required.
However, a terminal program does work as expected.

How:
  Load sketch to Arduino
  Quit (to free up serial port)
  Lauch terminal app (CoolTerm on Mac)
  Set Serial port and baud rate.
  Type... it works. 
 
3  Using Arduino / Programming Questions / implementing a getc() function; always needs a <return> in Serial Monitor? on: December 16, 2012, 06:53:28 pm
I'm trying to read a single character on serial port (y/n), but there is no response until I hit the <return> key or the Send button in the Serial Monitor. Is this a restriction of the monitor only? (If the port were connected to hyperterm for example, would it behave as expected?)

code snippet
Code:
void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    if (inChar == 'n') goflag = 0;
    else goflag = 1;
  }
}
4  Using Arduino / Programming Questions / Re: direct port writing: value changes but LED doesn't on: December 11, 2012, 02:58:46 am
I finally sorted this out. The arduino Uno and the mega2560 define the "arduino Dxx" the same, but the boards do not control the same output pins with the same processor port pins; the Mega uses some of PORTB and some of PORTH to control D8-D13. Therefore controlling bits on PORTD would fail though digitalWrite() would work. See schematic snippets. Now that I know what's going on, I know what to do. Thanks for the comments. I learned several things from them.

5  Using Arduino / Programming Questions / Re: direct port writing: value changes but LED doesn't on: December 10, 2012, 04:01:13 am
Why?
This is a much larger project controlling multiple high-speed valves, ethernet data, etc, and I want to minimize latency and write ports simultaneously. This is extracted to the bare minimum. I tried adding another ext LED to another pin of PORTB. Still doesn't work unless I do the following:

   PORTB = B11111111;
 later
   PORTB = 0;

works, and enables both LEDs as expected (pin 11, 13 in this instance).
Any other value to PORTB turns on neither. (B00111111 for instance).

6  Using Arduino / Programming Questions / Re: direct port writing: value changes but LED doesn't on: December 10, 2012, 02:27:50 am
I'm using the Mega 2560. Thanks for the comments. I'll try a different output.
7  Using Arduino / Programming Questions / direct port writing: value changes but LED doesn't on: December 09, 2012, 11:17:25 pm
I've made a simple modification to the blink program in order to verify operation by writing the port directly. I've printed the port byte and it changes, the the LED never comes on. Using DDRB & DDRD to set ports up had no effect. I'm missing something obvious. The digitalWrite() works. Suggestions appreciated.

Code:
// Blink, with direct port changing

int led = 13;      // Pin 13 has an LED connected on most Arduino boards.

// the setup routine runs once when you press reset:
void setup() {
  Serial.begin(9600);      // open the serial port at 9600 bps:    
  pinMode(led, OUTPUT);    // initialize the digital pin as an output.
  Serial.println();        // get to a new line in the monitor
}

void loop() {
  //  digitalWrite(led, HIGH);   // turn the LED on   (THIS WORKS)
  PORTB |= B00100000 ;        //     (DOESN'T WORK)
  delay(300);  

  //  digitalWrite(led, LOW);   // turn the LED off
  PORTB &= B11011111 ;  //  
  delay(700);
}
8  Using Arduino / Programming Questions / Re: Serial Monitor first message Delay on: December 07, 2012, 09:18:51 pm
If you "run" from the arduino IDE, it compiles and re-uploads every time, which does take several seconds. Then the program starts.
If pressing reset gives an immediate response, then it's working as it should.
Pages: [1]