Skip to content

Instantly share code, notes, and snippets.

@lvidarte
Last active October 15, 2016 22:36
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 lvidarte/a471cd8ba5acd47ff8899e841939a426 to your computer and use it in GitHub Desktop.
Save lvidarte/a471cd8ba5acd47ff8899e841939a426 to your computer and use it in GitHub Desktop.
Buttons controller for Minirobots Turtle
/**
* Author: Leo Vidarte <http://nerdlabs.com.ar>
*
* This is free software:
* you can redistribute it and/or modify it
* under the terms of the GPL version 3
* as published by the Free Software Foundation.
*/
const int pinBuzzer = 13;
const int soundButtons[] = {1760, 2093, 2637, 2794};
const int pinButtons[] = {2, 3, 4, 5};
int stateButtons[] = {0, 0, 0, 0};
int totalButtons()
{
return sizeof(pinButtons) / sizeof(int);
}
void setup()
{
Serial.begin(9600);
pinMode(pinBuzzer, OUTPUT);
for (int i = 0; i < totalButtons(); i++)
{
pinMode(pinButtons[i], INPUT);
}
}
void checkButton(int button)
{
if (digitalRead(pinButtons[button]) == HIGH)
{
if (stateButtons[button] == 0)
{
stateButtons[button] = 1;
Serial.write(button);
tone(pinBuzzer, soundButtons[button], 100);
}
}
else
{
stateButtons[button] = 0;
}
}
void loop()
{
for (int i = 0; i < totalButtons(); i++)
{
checkButton(i);
}
delay(100); // Delay a little bit to avoid bouncing
}
@lvidarte
Copy link
Author

Python reader

import sys
from serial import Serial

serial = Serial(sys.argv[1], 9600)

while True:
    print(ord(serial.read()))

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