Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@lvidarte

lvidarte Oct 14, 2016

Python reader

import sys
from serial import Serial

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

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

lvidarte commented Oct 14, 2016

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