Skip to content

Instantly share code, notes, and snippets.

View alistairjevans's full-sized avatar

Alistair Evans alistairjevans

View GitHub Profile
@alistairjevans
alistairjevans / app.ino
Last active May 11, 2019 10:26
Hello World for Arduino
void setup()
{
// Initialise my serial port with 9600 baud
Serial.begin(9600);
}
void loop()
{
// Wait a second
delay(1000);
@alistairjevans
alistairjevans / app.ino
Created May 19, 2019 12:31
Reading an analog voltage
void setup()
{
// Initialise my serial port with 9600 baud
Serial.begin(9600);
}
void loop()
{
// Analog values are read as integer values between 1 and 1023.
// Each 1 is a fraction of 5V. So 0 = 0V, 1023 = 5V, and the scale
@alistairjevans
alistairjevans / digitalspeed1.ino
Created May 21, 2019 20:08
Basic digital speed sampler.
const int SPEEDPIN = 2;
// Sample every 500ms
const int SAMPLEFREQ = 500;
// Variables for storing state.
int switchState = 0;
int lastSwitchState = 0;
// Variable for remembering when we last sampled.
@alistairjevans
alistairjevans / program.cs
Created May 25, 2019 09:30
Value-Tuple basics
(string value1, string value2) SomeFunc()
{
return ("val1", "val2");
}
void CallerFunction()
{
var result = SomeFunc();
Console.WriteLine(result.value1); // val1
@alistairjevans
alistairjevans / program.cs
Created May 25, 2019 09:30
Value-Tuple basics
(string value1, string value2) SomeFunc()
{
return ("val1", "val2");
}
void CallerFunction()
{
var result = SomeFunc();
Console.WriteLine(result.value1); // val1
@alistairjevans
alistairjevans / program.cs
Last active May 25, 2019 09:48
Old painful list of kvp
void SomeMethod(List<KeyValuePair<string, string>> pairs)
{
// Use the values
}
void Caller() {
// Yuck!
SomeMethod(new List<KeyValuePair<string, string>>
@alistairjevans
alistairjevans / program.cs
Last active May 25, 2019 09:48
Still painful, but getting better
// Forgive me for this function name
KeyValuePair<string, string> KVP(string key, string val)
{
return new KeyValuePair<string, string>(key, val);
}
void Caller()
{
// Still pretty bad
SomeMethod(new List<KeyValuePair<string, string>> {
@alistairjevans
alistairjevans / program.cs
Last active May 25, 2019 09:49
Array KVP - steady improvement
void SomeMethod(KeyValuePair<string, string>[] pairs)
{
}
void Caller()
{
// Ok, so I hate myself a little less
SomeMethod(new[]
{
KVP("key1", "val1"),
@alistairjevans
alistairjevans / program.cs
Last active May 25, 2019 09:50
KVP with params
void SomeMethod(params KeyValuePair<string, string>[] pairs)
{
}
void Caller()
{
// params are great, but I still don't love it.
SomeMethod(
KVP("key1", "val1"),
KVP("key2", "val2")
@alistairjevans
alistairjevans / program.cs
Last active May 25, 2019 09:51
Value Tuples to pass lists
void SomeMethod(params (string key, string value)[] pairs)
{
}
void Caller()
{
// Now that's more like it!
SomeMethod(
("key1", "val1"),
("key2", "val2")