Rich A. Marino  |  Arduino Stuff

These are my notes related to Arduino.

Using Arduino on Windows 8 (Driver Signing Issue)

Due to the mandated driver signing on Windows 8, the inbuilt Arduino drivers will not work. A solution is to install an alternative driver written by Microsoft for a similar device. I use this with an Arduino Uno.*
  1. Plug in the Arduino
  2. Open Device Manager (search for devmgmt.msc)
  3. Go to unknown devices. Select the Unknown Device.
  4. Right-click the unknown device, and choose Update Driver...
  5. Choose "Browse my computer for driver software".
  6. Choose "Let me pick from a list of device drivers on my computer".
  7. Choose "Modems", and click Next.
  8. For Manufacturer, choose "Compaq".
  9. Choose "Ricochet Wireless USB Modem" (should be last item on list)
  10. Click Yes on the warning.
  11. The driver installs. That modem uses the same USB-Serial chip as the Arduino.
  12. Open the Arduino software. Choose Tools > Serial Port and select appropriately.
*This works for me. I take no liability if it doesn't work for you, or causes damage. USE AT YOUR OWN RISK!

Blinking an LED

This is an elementary code to blink an LED. Note the use of constants (boardLED), the setup function, and the loop function that will run indefinately.
int boardLED = 13;

void setup()
{
pinMode(boardLED, OUTPUT);
}

void loop()
{
digitalWrite(boardLED, HIGH);
delay(270);
digitalWrite(boardLED, LOW);
delay(100);
}

Using a Button

One should be familiar with the concepts of pull-up or pull-down resistors. Run a wire from one side of the switch to Vcc. Put a resistor on the other side, and connect to the ground. Put a connection from the side with the resistor into a numbered pin on the Arduino. This code runs a toggle switch
int boardLED = 13;
int button = 2;
boolean state = LOW;

void setup()
{
pinMode(boardLED, OUTPUT);
pinMode(button, INPUT);
}

void loop()
{
if(digitalRead(button) == HIGH && state == LOW)
{
state = HIGH;
}
else if(digitalRead(button) == HIGH && state == HIGH)
{
state= LOW;
}
digitalWrite(boardLED, state);
}

Debouncing

The Arduino is imperfect. When the state of a button changes, there is some fluctuation for some while, which makes getting an accurate reading troublesome. One solution is to put a capacitor in series with the button, but this can also be solved in software:
int boardLED = 13;
int button = 2;
boolean led_state = LOW;
boolean last_state = LOW;
boolean current_state = LOW;
void setup()
{
pinMode(boardLED, OUTPUT);
pinMode(button, INPUT);
}

boolean debounce (boolean button_state)
{
boolean current = digitalRead(button);
if(button_state != current)
{
delay(5); //5 ms delay
current = digitalRead(button); //debounced value
}
}

void loop()
{
current_state = debounce(last_state);
if(last_state == LOW && current_state == HIGH)
{
led_state = !led_state;
}
digitalWrite(boardLED, led_state);
last_state = current_state;
}
It is worth noting that the delay must be enough time for the switch to finish bouncing, but short enough time so that the press is responsive. The Arduino runs at 400,000 fps on an empty loop. A digitalWrite takes 25 microseconds. A digitalRead takes 3 microseconds.