Commit d4a28b2d authored by Philip Hozier's avatar Philip Hozier
Browse files

week8 update #2 2/5

parent 8078da15
Pipeline #228232 passed with stages
in 34 seconds
......@@ -312,20 +312,20 @@ We hooked up the Arduino Uno board to a MAC computer with the IDE already instal
We began programming a 'sketch' to blink the LED. We checked which pin the LED was connected and picked a value of 100ms as a reasonable length of time for the LED to be on.
int LED_pin = 9;
int OnTime = 100;
int OffTime = 100;
int LED_pin = 9;
int OnTime = 100;
int OffTime = 100;
void setup() {
pinMode(LED_pin, OUTPUT);
}
void setup() {
pinMode(LED_pin, OUTPUT);
}
void loop() {
digitalWrite(LED_pin, HIGH);
delay(OnTime);
digitalWrite(LED_pin, LOW);
delay(OffTime);
}
void loop() {
digitalWrite(LED_pin, HIGH);
delay(OnTime);
digitalWrite(LED_pin, LOW);
delay(OffTime);
}
With my background in film and TV I knew that the human eye and brain actually take snapshots of i mages what we see and not a continuous input (you could call it a 'digital' signal). This is called the persistence of vision, which has a time value of approximately 1/25 of a second (equivalent to 25 frames a second for TV i mage signals). We were also taught that the electronic signal to an LED's are digital and are actually pulsing, causing it to flash at a rate that we can't notice.
......@@ -333,20 +333,20 @@ I suggested we try and find out what would be the minimum value we would need be
I suggested that we try to change the brightness by changing the ratio of time between the LED being on and off within that cycle. This I thought would be able to change the perceived brightness of the LED. A bit like people walking across a long exposure photograph. It turns out this has a name, Pulse Modulation Waveform (PWM).
int LED_pin = 9;
int Frequency = 1000/50;
int DutyCycle = 10;
int LED_pin = 9;
int Frequency = 1000/50;
int DutyCycle = 10;
void setup() {
pinMode(LED_pin, OUTPUT);
}
void setup() {
pinMode(LED_pin, OUTPUT);
}
void loop() {
digitalWrite(LED_pin, HIGH);
delay(Frequency * DutyCycle/100);
digitalWrite(LED_pin, LOW);
delay(Frequency * (100 - DutyCycle)/100);
}
void loop() {
digitalWrite(LED_pin, HIGH);
delay(Frequency * DutyCycle/100);
digitalWrite(LED_pin, LOW);
delay(Frequency * (100 - DutyCycle)/100);
}
Next spiral development was to add an input to our board.
......@@ -374,30 +374,30 @@ upon closure of the button. Due to voltage fluctuation noise in the circuit, it
The last spiral was to get an analogue input to affect the brightness of the LED. We accidentally left in the digital button so t only we could only change the brightness when the button was also pressed.
int LED_pin = 9;
int BUTTON_pin = 3;
int ANALOG_pin = A0;
int Frequency = 1000/50; // 1000 milliseconds / 50 Hertz = 20 ms wait time
int DutyCycle = 10; // the percentage "on"
int AnalogValue = 0;
void setup() {
pinMode(LED_pin, OUTPUT);
pinMode(BUTTON_pin, INPUT_PULLUP);
pinMode(ANALOG_pin, INPUT);
}
void loop() {
i f (digitalRead(BUTTON_pin) == LOW) {
AnalogValue = analogRead(ANALOG_pin);
DutyCycle = map(AnalogValue, 0, 1023, 0, 99);
digitalWrite(LED_pin, HIGH);
delay(Frequency * DutyCycle/100);
digitalWrite(LED_pin, LOW);
delay(Frequency * (100 - DutyCycle)/100);
int LED_pin = 9;
int BUTTON_pin = 3;
int ANALOG_pin = A0;
int Frequency = 1000/50; // 1000 milliseconds / 50 Hertz = 20 ms wait time
int DutyCycle = 10; // the percentage "on"
int AnalogValue = 0;
void setup() {
pinMode(LED_pin, OUTPUT);
pinMode(BUTTON_pin, INPUT_PULLUP);
pinMode(ANALOG_pin, INPUT);
}
void loop() {
i f (digitalRead(BUTTON_pin) == LOW) {
AnalogValue = analogRead(ANALOG_pin);
DutyCycle = map(AnalogValue, 0, 1023, 0, 99);
digitalWrite(LED_pin, HIGH);
delay(Frequency * DutyCycle/100);
digitalWrite(LED_pin, LOW);
delay(Frequency * (100 - DutyCycle)/100);
}
}
}
---
......@@ -460,83 +460,93 @@ I started off by programming a simple button = LED flash of a certain duration.
The 'dashes' were 2 'dots' together, and the delay between each letter was also double that of in between each letter. This ended up a pretty rapid execution of the morse code, and too little delays for me to easily distinguish between the letters. I increased the delays by a factor of 2, and increased the amount of time the LED should be on to 100ms.
// Program to flash 'HELLO' in morse code.
// DIGITAL
#define LED_pin 0
#define BUTTON_pin A3
// LED variables
int LED_duty = 1000/10; // length of LED cycle
int LED_delay = LED_duty*2; // delay between cycles
int LED_morseDelay = LED_delay*4; // delay between morse code
void setup() {
// put your setup code here, to run once:
pinMode(LED_pin, OUTPUT);
pinMode(BUTTON_pin, INPUT_PULLUP); // make sure it is open and not sending a LOW signal
}
void loop() {
// put your main code here, to run repeatedly:
i f(digitalRead(BUTTON_pin) == LOW){
Hello();
}else{
digitalWrite(LED_pin, LOW);
}
}
void LED_dot(){
digitalWrite(LED_pin,HIGH);
delay(LED_duty);
digitalWrite(LED_pin,LOW);
}
void LED_dash(){
digitalWrite(LED_pin,HIGH);
delay(LED_duty*4);
digitalWrite(LED_pin,LOW);
}
void Hello(){
////////////// h ....
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_morseDelay);
////////////// e .
LED_dot();
delay(LED_morseDelay);
////////////// l .-..
LED_dot();
delay(LED_delay);
LED_dash();
delay(LED_delay);
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_morseDelay);
////////////// l .-..
LED_dot();
delay(LED_delay);
LED_dash();
delay(LED_delay);
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_morseDelay);
////////////// o ---
LED_dash();
delay(LED_delay);
LED_dash();
delay(LED_delay);
LED_dash();
delay(LED_morseDelay);
}
// Program to flash 'HELLO' in morse code.
// DIGITAL
#define LED_pin 0
#define BUTTON_pin A3
// LED variables
int LED_duty = 1000/10; // length of LED cycle
int LED_delay = LED_duty*2; // delay between cycles
int LED_morseDelay = LED_delay*4; // delay between morse code
void setup() {
// put your setup code here, to run once:
pinMode(LED_pin, OUTPUT);
pinMode(BUTTON_pin, INPUT_PULLUP); // make sure it is open and not sending a LOW signal
}
void loop() {
// put your main code here, to run repeatedly:
i f(digitalRead(BUTTON_pin) == LOW){
Hello();
}else{
digitalWrite(LED_pin, LOW);
}
}
void LED_dot(){
digitalWrite(LED_pin,HIGH);
delay(LED_duty);
digitalWrite(LED_pin,LOW);
}
void LED_dash(){
digitalWrite(LED_pin,HIGH);
delay(LED_duty*4);
digitalWrite(LED_pin,LOW);
}
void Hello(){
////////////// h ....
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_morseDelay);
////////////// e .
LED_dot();
delay(LED_morseDelay);
////////////// l .-..
LED_dot();
delay(LED_delay);
LED_dash();
delay(LED_delay);
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_morseDelay);
////////////// l .-..
LED_dot();
delay(LED_delay);
LED_dash();
delay(LED_delay);
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_morseDelay);
////////////// o ---
LED_dash();
delay(LED_delay);
LED_dash();
delay(LED_delay);
LED_dash();
delay(LED_morseDelay);
}
**Serial output**
To view the serial output you need to turn to Terminal (Arduino's serial monitor function wants to look at the same UPDI port). Type in this command:
```screen /dev/cu.usbserial-D30A3T7Y 9600```
Where 'cu.usbserial-D30A3T7Y' is the port your FTDI is using.
![Individual Assignment](../week8/wk8-serial-output.jpg "Serial output")
---
## Further Development
......@@ -545,7 +555,7 @@ To hone my program so that I can use this board as a one-stop-shop control to te
A side project would be 'create' a morse-code 'library' that would echo what you type in morse code through the LED.
Further-further development would pLace another 'Hello' board next to i t, and allow that to receive the flashes through the phototransistor and therefore have another way of computers to talk between each other :).
Further-further development would pLace another 'Hello' board next to it, and allow that to receive the flashes through the phototransistor and therefore have another way of computers to talk between each other :).
Scale this up and take over the world! Muah ahahahahah.
---
......@@ -566,14 +576,20 @@ When learning to program the board I stumbled over where all the different pin n
[ATtiny412 Datasheet](https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny212-214-412-414-416-DataSheet-DS40002287A.pdf)
**UPDI upside down**
In one of the sessions trying to test the programs on the 'Hello' board I kept getting this error when uploading the program to the board.:
```PycuprogError: UPDI Initialisation failed.```
Google searches came up with almost nothing, and then I realised, it was upside down. Meaning the 2 pins were swapped around - a school boy error! I had made a nice case on my 3D printer for the UPDI , and each side looked very similar.
Google searches came up with almost nothing, and then I realised, it was upside down. Meaning the 2 pins were swapped around - a school boy error! I had made a nice case on my 3D printer for the UPDI, and each side looked very similar.
**Serial communications problems**
When trying the serial communication test program I came up against these problems. I had borrowed a commercially available FTDI board to test my programming (while I would re-make an Fablab FTDI at another time). This had 'male' serial header pins so wouldn't fit directly onto my 'Hello' board. I looked at the 'Hello' board schematic and connected my 1 and 3 pins to the GND and Vcc of the FTDI . I was confident of the Serial programming I had done , but whilst wondering why I was receiving no serial communication from the board, I realised that the FTDI should have the Tx and Rx pins connect to have a fighting chance of working. Back to the schematic and connected to pins 4 + 5. The ```Serial.swap()``` to activate the alternative function needed on these pins for my board was in the program. But that still didn't work. I tried checking that the different boards were recognised by the System Profile and scanned the USB ports n Terminal like above, and both boards were there as expected. I must be missing something simple but I don't know were to look. I 'll wait till i've made my own FTDI to take the 'non-standard' temporary FTDI board out of the equation to test again.
When trying the serial communication test program I came up against these problems. I had borrowed a commercially available FTDI board to test my programming (while I would re-make an Fablab FTDI at another time). This had 'male' serial header pins so wouldn't fit directly onto my 'Hello' board. I looked at the 'Hello' board schematic and connected my 1 and 3 pins to the GND and Vcc of the FTDI . I was confident of the Serial programming I had done , but whilst wondering why I was receiving no serial communication from the board, I realised that the FTDI should have the Tx and Rx pins connect to have a fighting chance of working. Back to the schematic and connected to pins 4 + 5. The ```Serial.swap()``` to activate the alternative function needed on these pins for my board was in the program. But that still didn't work. I tried checking that the different boards were recognised by the System Profile and scanned the USB ports in Terminal like above, and both boards were there as expected. I must be missing something simple but I don't know were to look. I 'll wait till i've made my own FTDI to take the 'non-standard' temporary FTDI board out of the equation to test again.
*UPDATE*
I made an FTDI and tried out the program and instantly got some serial output! check out the morse code serial output image above.
![Temporary setup for testing](../week8/temp-setup.jpg "Temporary testing setup")
......@@ -585,4 +601,9 @@ This is probably the most crucial subject to learn for my final project. One tha
---
## FILES
----
\ No newline at end of file
----
| **FILE** | **DESCRIPTION** |
|---|----|
| [ATtiny412 datasheet](../week10/Attiny212-412_datasheet.pdf "ATtiny412 datasheet") | ATtiny412 datasheet. |
|[LED_SERIAL_HELLO.ino](../week8/LED_SERIAL_HELLO.ino "LED_SERIAL_HELLO code") | The Arduino code for LED and Serial morse code 'Hello'.|
\ No newline at end of file
// DIGITAL
int LED_pin = 0;
int BUTTON_pin = A3;
// LED variables
int LED_cycle = 1000/10; // length of LED cycle
float LED_duty = LED_cycle;//0.9; // Brightness of LED
float LED_off = LED_cycle;//1-LED_duty; //
int LED_delay = LED_cycle*2; // delay between cycles
int LED_morseDelay = LED_delay*4; // delay between morse code
void setup() {
// put your setup code here, to run once:
pinMode(LED_pin, OUTPUT);// PULLUP?
pinMode(BUTTON_pin, INPUT_PULLUP);
// Initialize serial communications at 9600 bps
// Only used for debug
Serial.swap(1);
Serial.begin(9600);
while(!Serial);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(BUTTON_pin) == LOW){
Hello();
}else{
digitalWrite(LED_pin, LOW);
}
}
void LED_dot(){
digitalWrite(LED_pin,HIGH);
delay(LED_duty);
digitalWrite(LED_pin,LOW);
//delay(LED_off);
//serial
Serial.print(" . ");
}
void LED_dash(){
digitalWrite(LED_pin,HIGH);
delay(LED_duty*4);
digitalWrite(LED_pin,LOW);
//serial
Serial.print(" - ");
}
void Hello(){
////////////// h ....
Serial.print("H ");
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_morseDelay);
//serial
Serial.print(" ");
////////////// e .
Serial.print("e ");
LED_dot();
delay(LED_morseDelay);
Serial.print(" ");
////////////// l .-..
Serial.print("l ");
LED_dot();
delay(LED_delay);
LED_dash();
delay(LED_delay);
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_morseDelay);
Serial.print(" ");
////////////// l .-..
Serial.print("l ");
LED_dot();
delay(LED_delay);
LED_dash();
delay(LED_delay);
LED_dot();
delay(LED_delay);
LED_dot();
delay(LED_morseDelay);
Serial.print(" ");
////////////// o ---
Serial.print("o ");
LED_dash();
delay(LED_delay);
LED_dash();
delay(LED_delay);
LED_dash();
delay(LED_morseDelay);
Serial.print(" ");
Serial.println("");
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment