Showing posts with label fundamental. Show all posts
Showing posts with label fundamental. Show all posts

Saturday, March 22, 2014

Convert int to String

This example show how to convert int to String in various number base, BIN, HEX, DEC.
void setup() {
  
}

void loop() {
  int i = 1023;
  Serial.println("http://arduino-er.blogspot.com/");
  Serial.println("Test String: convert int to String");
  Serial.println();
  
  Serial.println("String(i) = " + String(i));
  Serial.println();
  
  Serial.println("String(i, BIN) = " 
      + String(i, BIN));
  Serial.println("String(i, HEX) = " 
      + String(i, HEX));
  Serial.println("String(i, DEC) = " 
      + String(i, DEC));
  Serial.println();
  
  Serial.println("String(i+i, BIN) = " 
      + String(i+i, BIN));
  Serial.println("String(i+i, HEX) = " 
      + String(i+i, HEX));
  Serial.println("String(i+i, DEC) = " 
      + String(i+i, DEC));
  Serial.println();
  
  Serial.println("String(i, BIN)+String(i, BIN) = " 
      + String(i, BIN)+String(i, BIN));
  Serial.println("String(i, HEX)+String(i, HEX) = " 
      + String(i, HEX)+String(i, HEX));
  Serial.println("String(i, DEC)+String(i, DEC) = " 
      + String(i, DEC)+String(i, DEC));
  Serial.println();
  
  delay(1000);
}

Convert int to String
Convert int to String

Tuesday, July 16, 2013

Generate pseudo-random numbers in Arduino


In Arduino, call random() function to generate pseudo-random numbers.
  • random(max)
  • random(min, max)
min - lower bound of the random value, inclusive (optional)
max - upper bound of the random value, exclusive

Returns
a random number between min and max-1 (long)

Monday, February 25, 2013

Hello World on Arduino Due, with message printed in Serial port

There are no display on Arduino board (Arduino Due in my case). We can display message on Arduino IDE's Tools of Serial Monitor, by print on Serial.

Hello World on Arduino Due, with message printed in Serial port.
Hello World on Arduino Due, with message printed in Serial port.



void setup() {
  //Setup Serial Port with baud rate of 115200
  Serial.begin(115200);
  print("Hello World!");
}

void loop() {
  
}

void print(String s){
  Serial.print(s);
}


Arduino Due code example: Serial communication - Rx

Simple example run on Arduino Due to read data from Serial, then send back to Serial.

Arduino Due code example: Serial communication - Rx



void setup() {
  //Setup Serial Port with baud rate of 115200
  Serial.begin(115200);

}

void loop() {
  
  if(Serial.available() > 0){
    Serial.println((char)(Serial.read()));
  }

}


Sunday, February 24, 2013

Arduino Due code example: Serial communication - Tx

Simple example run on Arduino Due to send data via Serial, with baud rate of 115200.

#define MSG_SIZE 17

char hellomsg[MSG_SIZE] = {'H', 'e', 'l', 'l', 'o', ' ', 
  'A', 'r', 'd', 'u', 'i', 'n', 'o', '-', 'e', 'r', '!'};

void setup() {
  //Setup Serial Port with baud rate of 115200
  Serial.begin(115200);

}

void loop() {
  for(int i = 0; i < MSG_SIZE; i++){
    Serial.print(hellomsg[i]);
    delay[500];
  }
  
  Serial.println();
  delay(1000);
}