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.*
- Plug in the Arduino
- Open Device Manager (search for devmgmt.msc)
- Go to unknown devices. Select the Unknown Device.
- Right-click the unknown device, and choose Update Driver...
- Choose "Browse my computer for driver software".
- Choose "Let me pick from a list of device drivers on my computer".
- Choose "Modems", and click Next.
- For Manufacturer, choose "Compaq".
- Choose "Ricochet Wireless USB Modem" (should be last item on list)
- Click Yes on the warning.
- The driver installs. That modem uses the same USB-Serial chip as the Arduino.
- 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.