They where two assignments for this week. The first one was to read one microcontroller's datasheet. The datasheet can be found here. Since I was using merely ATtiny44 microcontrollers I had focused only on this kind of microcontrollers during the reading. Basically I have learned the difference between analog and digital pin outputs, the importance of the power consumption, internal memory and frequency of these kind of microcontrollers. The overall information was to get familiarized with every technical details it may be required to use and program these microcontrollers, no matter of the programming environment.
The second assignment was to program the HelloWorld board to do something. Since I don't have programming skills, but I wanned to create something original I had a really hard week in figuring out everything and setting everything up.
Firstly I have installed the Arduino 1.6.1, ATtiny board files and the FDTI connector driver. Secondly, and to make sure that everything is soldered well on the board, I have burned the bootloader and uploaded the Blink program from the existing basic examples. The result was a blinking LED, as expected.
From this point onward things started to get complicated. I have read many examples of Arduino programs to see how they work, how they are programmed and all the implemented logic. I wanned to do something simple, smart and "useful". With a blinking LED you can do many things, including a Morse code. And this was my target: program the board to Morse code the fablab word.
The programming part was quite easy (relaying heavily on examples), but I didn't liked the outcome. I have used delay() command and while delay() was on the board wasn't checking for any other information. If I would press the button during a delay(), the board would simply just miss that information. It is like when I would drive on the highway and will shut my eyes for 10 minutes. I will miss many important information and surely won't reach my destination! Therefore, it was inacceptable to happen the same for the board, I wanned him to reach his destination and do his job correctly.
I have found the millis() command what was doing exactly what I needed: counting time and not blocking the communication ways. From here, I had to reset the millis() at every action take and introduce a new variable for the programs progress. Without this, the program would be on the same buckle forever. Having a button variable created already, it is possible to stop or restart the blinking at any time. Finally, I have edited a video with the blinking and his translation.
The program can be seen below. It is not really a nice coding, but it makes his duty of emitting the morse code of the fablab word. Many things have learned and many to come...
/*
Szilard Kados
- fablab in blinking morse code -
- first press the program starts runing
- second press the program stops
- third press the program runs from the beginning
*/
//declaring all the needed variables and define the pin numbers
int prevButton = HIGH;
bool buttonState = false;
long startTime;
int fablab = 0;
int dot = 500;
int dash = 1000;
int off = 500;
int pause = 1000;
const int LEDpin = 3;
const int BUTTONpin = 7;
//initializing the pins for input and output
void setup() {
pinMode(LEDpin, OUTPUT);
pinMode(BUTTONpin, INPUT);
}
//the actual program loop
void loop() {
if (digitalRead (BUTTONpin) == LOW) {
if (prevButton == HIGH) {
prevButton = LOW;
if (buttonState == false) {
buttonState = true;
digitalWrite(LEDpin, HIGH);
startTime = millis();
fablab = 0;
} else {
buttonState = false;
digitalWrite(LEDpin, LOW);
prevButton = LOW;
fablab = 0;
}
}
} else {
prevButton = HIGH;
}
if (digitalRead(LEDpin) == HIGH && fablab == 0) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 1) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 2) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 3) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 4) {
if ((millis() - startTime) > dash) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 5) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 6) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 7) {
if ((millis() - startTime) > pause) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 8) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 9) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 10) {
if ((millis() - startTime) > dash) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 11) {
if ((millis() - startTime) > pause) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 12) {
if ((millis() - startTime) > dash) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 13) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 14) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 15) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 16) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 17) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 18) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 19) {
if ((millis() - startTime) > pause) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 20) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 21) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 22) {
if ((millis() - startTime) > dash) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 23) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 24) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 25) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 26) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 27) {
if ((millis() - startTime) > pause) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 28) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 29) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 30) {
if ((millis() - startTime) > dash) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 31) {
if ((millis() - startTime) > pause) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 32) {
if ((millis() - startTime) > dash) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 33) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 34) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 35) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 36) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == LOW && fablab == 37) {
if ((millis() - startTime) > off) {
digitalWrite(LEDpin, HIGH);
startTime = millis();
prevButton = HIGH;
buttonState = true;
++fablab;
}
} else if (digitalRead(LEDpin) == HIGH && fablab == 38) {
if ((millis() - startTime) > dot) {
digitalWrite(LEDpin, LOW);
startTime = millis();
prevButton = HIGH;
buttonState = false;
fablab = 0;
}
}
}