Arduino basics

The basics

Programming Arduino

Debugging

  • just do serial output, Serial.println() Links to an external site.  (don't forget Serial.begin() Links to an external site. in setup() )
    • but this means that you can't do both debugging and serial communication at the same time unless the other party is instructed to ignore
  • the serial communication can be read with any other program, not just the Arduino environment. You can use the Windows Hyperterminal or the Mac Terminal
    • just make sure you use the same baud rate in Serial.begin() and in your terminal program
    • in the environment you can set the baud rate from a chooser
    • on the Mac Terminal, you can use the screen utility  (Ctrl-a k to stop)
      • screen /dev/tty.usbmodemfa131 9600

Digital output

Digital input

Analog input

Analog output (PWM= Pulse Width Modulation)

Driving servos

Interrupt-based programming

  • you can define digital events on 2 pins (2 and 3) via interrupts
    • not analog (give me an interrupt when value is under X)
  • source code example (will open in this tab)
  • attachInterrupt Links to an external site.(pin-2, function, mode)
  • 'function' is a function name (should actually be &function, the address of the function in strict C language)
  • 'mode' can be LOW, CHANGE, RISING, FALLING
    • if mode is LOW, interrupts keep coming as long as the pins stays on LOW voltage
  • pin-2: pin 2 -> 0, pin 3->1. Only values 0 and 1 (pins 2 and 3) work for interrupts
  • detachInterrupt() Links to an external site.
  • still, there is no event queue, no event details, no analog events, etc.

Other Arduino functions

Debouncing

If you are reading this, it is probably because you have written code to run when you press a button, and you noticed that it is triggered multiple times even if you pressed only once. Or perhaps you did not realise that it is triggered several times, but you saw your actuators acting weirdly (and especially your LEDs!). This problem is called bouncing and it has a solution called... you guessed: Debouncing.

The Arduino website has a very nice explanation (Links to an external site.) on how to implement this solution.

Does this mechanism remind you of something? (Hint: Something from lab 1?)