My project is a keyboard, so I will need buttons as inputs. I already made a prototype on week 10 using an arduino, so, for this assignment my goal is to design my own prototype and continue my project research with this board.
The ATtiny45 has too few pins to work, so I decided to use the ATtiny44, with which I can connect the 8 buttons that I need for the keyboard.
Using an I2C lcd it's possible to control an lcd with only two pins of the microcontroller, I tried to make and lcd work with the ATtiny44 but I still have no success with that, so I will use serial monitor from arduino to read the inputs.
Another problem I will need to solve is the clock. In past weeks I noticed the delay times are not accurate. For this I will use an oscilloscope to measure timing and adjust the software accordingly.
On past arduino projects, I have read about switch bounce. Because the buttons don't go from opened to closed position cleanly, there is often some noise voltage that might cause false signals for the microcontroller, causing it to read several pushes when the user intends to input a signal with the button. So I looked for a debounce circuit to implement in my circuit. There are various types of debounce circuits, I chose one with one resistor and one capacitor:
With this information, I started designing my board using kiCAD.
First, the microcontroller pins needed for programming it were connected to the 2x3 pin header. These pins are gnd, vcc, miso, mosi, sck and rst:
Then the resonator was connected to xtal1, xatl2 and gnd:
For the 8 input buttons I used all the pins from adc0 through adc7:
And for each button I implemented the debounce circuit. I used the four pins of the button to be able to trace routes unter the button when designing the pcb:
It was complicated to design the pcb without bridges for some routes:
The buttons were used as bridges and the gnd route goes under all the buttons:
The components were arranged according to the keys distribution of the projet. It doesn't have the final size but it has the general shape required:
From kiCAD, the circuit is exported to a gerber file. And this file is loaded into flatCAM:
The main settings are:
Then, two cnc files are created, one for isolation (blue line) and one for cutout (red line):
This is the milled and the final board:
Although this board worked well, I didn't think beforehand about the communication, so that I could see what the board was doing. I tried implementing serial communication for the ATtimy44, but I was unable to make it work.
I then proceeded to design a new button board with pin headers, so it could be used with Arduino, and later with my Fabduino.
This is the design and milling process:
When my fabduino was ready (week 15) I connected the buttons board and made the tests. I also connected an ultrasonic sensore and measured distance with it.
The HC-SR04 ultrasonic sensor can measure distance, and it is simple to connect. It need only two digital pins from the microcontroller. I used pins 2 and 3 (and vcc and gnd) and wrote the code based on this information.
This is the code:
#define trigPin 3
#define echoPin 2
int led = 13;
void setup() {
Serial.begin (9600);
pinMode(led, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0)
{
digitalWrite(led, HIGH);
Serial.println("Out of range");
}
else
{
digitalWrite(led, LOW);
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
This is the output:
Testing
-
I tested the keyboard with my fabduino. For this assignment, I want to register the button or buttons being pressed. At first, the microcontroller was only looping a for statement sensing each input, and displaying trhough serial wich buttons were pressed.
int buttonArray[] = {3,4,5,6,7,8,9,10,11}; // this array will hold buttons pins
const int ledPin = 13; // the number of the LED pin
void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
for (int thisPin = 0; thisPin < 7; thisPin++)
{ pinMode(buttonArray[thisPin], INPUT); }
}
void loop() {
// read the state of the pushbuttons value:
// buttonState = digitalRead(buttonPin);
String mensaje = "button ";
for (int thisPin = 0; thisPin < 7; thisPin++)
{ int reading = digitalRead(buttonArray[thisPin]);
if (reading == LOW)
{
mensaje = mensaje + thisPin;
//mensaje = mensaje + reading;
}
}
Serial.println(mensaje);
Serial.println("------");
delay(500);
}
For the second version of the code, I set a While statement to stop the loop while there are no buttons pressed, and when a button is pressed it enters another While structure. This second while executes as long as there is a button or buttons pressed. Again, the microcontroller communicates through serial the information about wich buttons are pressed.
int buttonArray[] = {2, 3, 4, 7, 8, 9, 5, 6}; // this array will hold the pin value for each button
String stringArray[] = {"a","a","a","a","a","a","a","a","a"}; // this array will hold the state of each button
const int ledPin = 13; // the number of the LED pin
void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
for (int thisPin = 0; thisPin < 8; thisPin++)
{ pinMode(buttonArray[thisPin], INPUT); }
}
void loop()
{
// read the state of the pushbuttons value:
// buttonState = digitalRead(buttonPin);
String mensaje = "";
for (int thisPin = 0; thisPin < 8 ; thisPin++)
{
stringArray[thisPin] = "0";
mensaje = mensaje + stringArray[thisPin];
}
Serial.println("**********");
mensaje = "";
while ( digitalRead(buttonArray[0]) == HIGH &&
digitalRead(buttonArray[1]) == HIGH &&
digitalRead(buttonArray[2]) == HIGH &&
digitalRead(buttonArray[3]) == HIGH &&
digitalRead(buttonArray[4]) == HIGH &&
digitalRead(buttonArray[5]) == HIGH &&
digitalRead(buttonArray[6]) == HIGH &&
digitalRead(buttonArray[7]) == HIGH &&
digitalRead(buttonArray[8]) == HIGH )
{digitalWrite(ledPin, HIGH);}
digitalWrite(ledPin, LOW);
while ( digitalRead(buttonArray[0]) == LOW ||
digitalRead(buttonArray[1]) == LOW ||
digitalRead(buttonArray[2]) == LOW ||
digitalRead(buttonArray[3]) == LOW ||
digitalRead(buttonArray[4]) == LOW ||
digitalRead(buttonArray[5]) == LOW ||
digitalRead(buttonArray[6]) == LOW ||
digitalRead(buttonArray[7]) == LOW ||
digitalRead(buttonArray[8]) == LOW )
{
if(digitalRead(buttonArray[0] == LOW)) {stringArray[0] = "1";}
if(digitalRead(buttonArray[1]) == LOW) {stringArray[1] = "1";}
if(digitalRead(buttonArray[2]) == LOW) {stringArray[2] = "1";}
if(digitalRead(buttonArray[3]) == LOW) {stringArray[3] = "1";}
if(digitalRead(buttonArray[4]) == LOW) {stringArray[4] = "1";}
if(digitalRead(buttonArray[5]) == LOW) {stringArray[5] = "1";}
if(digitalRead(buttonArray[6]) == LOW) {stringArray[6] = "1";}
if(digitalRead(buttonArray[7]) == LOW) {stringArray[7] = "1";}
if(digitalRead(buttonArray[8]) == LOW) {stringArray[8] = "1";}
}
for (int thisPin = 0; thisPin < 8 ; thisPin++)
{
mensaje = mensaje + stringArray[thisPin];
}
Serial.println(mensaje);
mensaje = "";
}
This is the output:
Back