Skip to content

Instantly share code, notes, and snippets.

@Timmmm
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Timmmm/7f57c09132514d674181 to your computer and use it in GitHub Desktop.
Save Timmmm/7f57c09132514d674181 to your computer and use it in GitHub Desktop.
Arduino vs mBed

Arduino vs mBed

ArduinomBed
Basic GPIO
void foo()
{
	pinMode(4, INPUT_PULLUP);
	pinMode(5, OUTPUT);
	digitalWrite(5, !digitalRead(4));
}
void foo()
{
	DigitalIn pin4(D4, PullUp);
	DigitalOut pin5(D5);
	pin5 = !pin4;
}
Attach interrupt to pin 3
void callback() {}
void setup()
{
// Replace 1 with 3 for Arduino Due. 1 means pin 3 on Uno.
attachInterrupt(1, callback, FALLING);
}

void callback() {}
InterruptIn button(D1);
void main()
{
button.fall(callback);
}

Waiting
void foo()
{
	// Wait 60 seconds. Note that the L suffix is required because
	// the Uno boards are 16 bit, and int can only go up to 32767.
	delay(60 * 1000L);
// Wait 500 milliseconds.
delay(500);

// Wait 500 microseconds.
delayMicroseconds(500);

}

void foo()
{
	// Wait 60 seconds.
	wait(60);
	// Or using integers (faster on processors without FPU). mBed boards
	// are 32-bit so no need for an L suffix.
	wait_ms(60 * 1000);
// Wait 500 milliseconds.
wait_ms(500);
// Or using floats:
wait(0.5);

// Wait 500 microseconds.
wait_us(500);

}

Get system time in milliseconds
unsigned long t = millis();
// clock() returns microseconds since boot in a 64-bit integer.
unsigned int t = clock() / 1000;
Periodically call function
void callback() { }
Ticker f;
void main()
{
// Call callback every 0.5 seconds.
f.attach(callback, 0.5f);
Time a function call
Use PWM
Write to serial port
Quickly read/write multiple GPIOs
Use SPI
Use I2C
Analogue Input
void setup()
{
	pinMode(A0, INPUT);
}
void foo()
{
	int x = analogRead(A0);
AnalogIn ain(A0);
void foo()
{
float x = ain;

Analogue Output
Not possible
AnalogOut aout(A0);
void foo()
{
aout = 0.5f;

SD cards
Run multithreaded code
Print to serial
void setup()
{
	Serial.begin(115200);
}
void foo(int a, float b, const char* c)
{
Serial.print("a: ");
Serial.print(a);
Serial.print(" b: ");
Serial.print(b);
Serial.print(" c: ");
Serial.println(c);
}

Serial serial(USBTX, USBRX);
int main()
{
serial.baud(115200);
// ...
}
void foo(int a, float b, const char* c)
{
serial.printf("a: %d b: %f c: %s\n", a, b, c);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment