week12

parent 20c9f0b4
Pipeline #83778 passed with stage
in 25 seconds
#include <avr/io.h>
#include <util/delay.h>
#define output(directions,pin) (directions |= pin) // set port direction for output
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
//led config
#define led_portB PORTB
#define led_portA PORTA
#define led_directionB DDRB
#define led_directionA DDRA
#define red (1 << PB2)
#define green (1 << PA6)
#define blue (1 << PA7)
//servo config
#define PWM_port PORTA
#define PWM_direction DDRA
#define PWM_pin_0 (1 << PA5)
#define loop_count 60
int main(void) {
uint8_t i;
unsigned char count, pwm;
// set clock divider to /1
CLKPR = (1 << CLKPCE);
CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
// set PWM pins to output
//
clear(PWM_port, PWM_pin_0);
output(PWM_direction, PWM_pin_0);
// initialize LED pins
set(led_portB, red);
output(led_directionB, red);
set(led_portA, green);
output(led_directionA, green);
set(led_portA, blue);
output(led_directionA, blue);
//LED RED ON/OFF
clear(led_portB,red);
_delay_ms(5000);
set(led_portB,red);
_delay_ms(500);
while (1) {
// _delay_ms(500);
//
//LED GREEN ON/OFF
clear(led_portA,green);
_delay_ms(1000);
set(led_portA,green);
//LED BLUE ON/OFF
clear(led_portA,blue);
for (i = 0; i < loop_count; ++i) {
set(PWM_port,PWM_pin_0);
_delay_us(2000);
clear(PWM_port,PWM_pin_0);
_delay_us(19000);
}
for (i = 0; i < loop_count; ++i) {
set(PWM_port,PWM_pin_0);
_delay_us(20);
clear(PWM_port,PWM_pin_0);
_delay_us(18500);
}
set(led_portA,blue);
//_delay_ms(500);
}
}
void setup() {
// put your setup code here, to run once:
pinMode(8,OUTPUT);//red
pinMode(7,OUTPUT);//blue
pinMode(6,OUTPUT);//green
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(8,LOW);
delay(1000);
digitalWrite(8,HIGH);
delay(2000);
digitalWrite(7,LOW);
delay(1000);
digitalWrite(7,HIGH);
delay(2000);
digitalWrite(6,LOW);
delay(1000);
digitalWrite(6,HIGH);
delay(2000);
//delay(2000);
}
int i;
void setup() {
// put your setup code here, to run once:
pinMode(8,OUTPUT);//red
pinMode(7,OUTPUT);//blue
pinMode(6,OUTPUT);//green
}
void loop() {
// put your main code here, to run repeatedly:
int r = 1;
digitalWrite(8,HIGH);
digitalWrite(7,HIGH);
digitalWrite(6,HIGH);
for (i=255;i>0; i--){
analogWrite(8,i);
delay(10);
}
for (i=0;i<255; i++){
analogWrite(8,i);
delay(10);
}
for (i=255;i>0; i--){
analogWrite(7,i);
delay(10);
}
for (i=0;i<255; i++){
analogWrite(7,i);
delay(10);
}
for (i=255;i>0; i--){
analogWrite(6,i);
delay(10);
}
for (i=0;i<255; i++){
analogWrite(6,i);
delay(10);
}
}
#include <avr/io.h>
#include <util/delay.h>
#define output(directions,pin) (directions |= pin) // set port direction for output
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define PWM_delay() _delay_us(25) // PWM delay
#define led_portB PORTB
#define led_portA PORTA
#define led_directionB DDRB
#define led_directionA DDRA
#define red (1 << PB2)
#define green (1 << PA6)
#define blue (1 << PA7)
int main(void) {
//
// main
//
unsigned char count, pwm;
//
// set clock divider to /1
//
CLKPR = (1 << CLKPCE);
CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
//
// initialize LED pins
//
set(led_portB, red);
output(led_directionB, red);
set(led_portA, green);
output(led_directionA, green);
set(led_portA, blue);
output(led_directionA, blue);
//
// main loop
//
while (1) {
//LED BLUE ON/OFF
clear(led_portA,blue);
_delay_ms(500);
set(led_portA,blue);
_delay_ms(500);
//LED GREEN ON/OFF
clear(led_portA,green);
_delay_ms(500);
set(led_portA,green);
_delay_ms(500);
//LED RED ON/OFF
clear(led_portB,red);
_delay_ms(500);
set(led_portB,red);
_delay_ms(500);
}
}
#include <Servo.h>
Servo myservo;
int pos = 0;
void setup() {
// put your setup code here, to run once:
myservo.attach(5);
}
void loop() {
// put your main code here, to run repeatedly:
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
#include <avr/io.h>
#include <util/delay.h>
#define output(directions,pin) (directions |= pin) // set port direction for output
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define position_delay() _delay_ms(1000)
#define PWM_port PORTA
#define PWM_direction DDRA
#define PWM_pin_0 (1 << PA5)
#define loop_count 60
int main(void) {
//
// main
//
uint8_t i;
//
// set clock divider to /1
//
CLKPR = (1 << CLKPCE);
CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
//
// set PWM pins to output
//
clear(PWM_port, PWM_pin_0);
output(PWM_direction, PWM_pin_0);
//
// main loop
//
while (1) {
//
// 1 ms on time, both
//
for (i = 0; i < loop_count; ++i) {
set(PWM_port,PWM_pin_0);
_delay_us(2000);
clear(PWM_port,PWM_pin_0);
_delay_us(19000);
}
//
// 1.5 ms on time, both
//
for (i = 0; i < loop_count; ++i) {
set(PWM_port,PWM_pin_0);
_delay_us(20);
clear(PWM_port,PWM_pin_0);
_delay_us(18500);
}
}
}
......@@ -135,68 +135,51 @@
<a title="Output board"><img class="image center" src="images/week12/output.jpg"></a>
<h2>Programming</h2>
The difference of the previous weeks, we only use the ISP to program the board, that is because there is a voltage regulator that can cause interference with the power supply of the FTDI, the final wiring is the one seen in the picture.
<a title="Conection"><img class="image center" src="images/week12/conection.jpg"></a>
<br><h4>LED RGB</h4>
I start to program the LED with a simple ignition of the three leds. The ignition code is the following:
<a title="Code on / off"><img class="image center" src="images/week12/codeonoff.jpg"></a>
The next code is the turning on of the RGB using the PWM, turning on the red led first, then the blue and finally the green one.
The code used is the following
<a title="Code PWM"><img class="image center" src="images/week12/rgbpwm.jpg"></a>
You can see a video of the functioning of the on and off of the LEDs, as well as the operation of the PWM
<iframe width="560" height="315" src="https://www.youtube.com/embed/ISkW8jgA_gU" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<br><h4>Servomotor</h4>
For the servomotor operation, I use a very basic code that works with an Arduino, the code is shown in the following picture
<a title="Code Servo Arduino"><img class="image center" src="images/week12/servo.jpg"></a>
As with the softwareserial library, the servo library does not work correctly, so it is decided to program in C, for this I modify the<a href="http://academy.cba.mit.edu/classes/output_devices/servo/hello.servo.44.2.c"> code provided by Neil</a> , my final code being the following:
<a title="Code Servo C"><img class="image center" src="images/week12/servoc.jpg"></a>
In the following video we can see the movement of servomotor.
<center><video width="680" height="400" src="images/week12/ServovideoC.mp4" autoplay muted loop></video> </center>
<br><h4>Integration</h4>
For the integration of servomotor and RGB, I make a small application of what would be the logic of my final project:<br>
The Red LED will light up until the water reaches its desired temperature, when it reaches that value, the green LED will light be indicating that the system is ready.
When the valve is activated, which will be the servomotor, a blue LED will light be indicating that it is in operation.<br>
Here is a video of its final operation.<br>
<center><video width="680" height="400" src="images/week12/integration.mp4" autoplay muted loop></video> </center>
<h2>Problems</h2>
As we have problems with the arduino language, we tried to carry out a C programming with the simple ignition of the LEDs, having the following code and its demonstration in a video
<a title="RGB C"><img class="image center" src="images/week12/rgbc.jpg"></a>
<center><video width="680" height="400" src="images/week12/videoRGBc.mp4" autoplay muted loop></video> </center>
<h2>Group</h2>
Working.
<!--
As described above, I have decided to use an analog and a digital sensor, as I will use these sensors for my final project. Therefore, I reread the attiny44's datasheet to know how to use the analog and digital input pins.<br>
The attiny44 has a total of 7 analog pins, internally it has a digital analog converter (ADC) which will help us to interpret the analog sensor in a better way.<br>
There is a table on page 180 of the<a href="http://ww1.microchip.com/downloads/en/DeviceDoc/doc8006.pdf"> data sheets</a> that indicates the maximum value that the ADC can have is 1023, which would be the analog value of the maximum input voltage, in this case it will be 5VDC.
In other words, it means that the analog pins will have values between 0 and 1023, which would be from 0 to 5VDC. With programming we must convert this measure into a value that we can understand using different formulas.<br>
On page 55 of the data sheet they mention that digital pins are bidirectional, that means they can serve as inputs and outputs. We remember the hello world board in which we use a led as a digital output and the button as a digital input, only its operation is configured on setup as input or output.<br>
Only in case analog pins work only as inputs, they cannot be outputs. In case of wanting to use an analog output, we must use the PWM pins of the microcontroller.<br>
With these previous concepts, we proceed to make the schematic of our inputs board, remaining as the next picture.
<a title="Schematic circuit"><img class="image center" src="images/week11/schmatic.JPG"></a>
We remember from week 7, the attiny44 has a small internal clock of 8MHZ, for that reason I save the resonator and take both pins for the ultrasonic sensor and make a smaller board.
The final board is shown in the following picture.
<a title="Board circuit"><img class="image center" src="images/week11/board.jpg"></a>
We start with the manufacturing and soldering process, having the following result.
<a title="Inputs board"><img class="image center" src="images/week11/inputsboard.jpg"></a>
<br><h2>Programming</h2>
As it was done in the week of the programming, it is necessary to make a connection of the manufactured board, the ISP to burn the bootloader and the FTDI to load the program to the microcontroller.
The final wiring with the included sensor is as follow picture:
<a title="Board connection"><img class="image center" src="images/week11/connection.jpg"></a>
Now, it is time to programming both sensors.<br>
<br><h4>HC-SR04</h4>
The first test is the distance sensor, I work with these one several times, for that reason it was the first one.
In my board I did not use a led for output, so I only use the software serial monitor to see my distance.
<a title="Distance code"><img class="image center" src="images/week11/distance.jpg"></a>
<br><h4>Thermistor</h4>
For this case, I use<a href="https://learn.adafruit.com/thermistor/using-a-thermistor"> this tutorial</a> to programming my temperature sensor. These was the formula:
<pre><code>reading = (1023.0 / value) - 2.0;
temp = reading*100.0;</code></pre>
<a title="Temperature code"><img class="image center" src="images/week11/temperature.jpg"></a>
<br><h4>Both sensors</h4>
I try to make an only program, but I have a critical problem. The software serial library uses a lot of the internal memory, my programs are too heavy for the attiny44.
<a title="Memory problem"><img class="image center" src="images/week11/fullmemory.jpg"></a>
For my final project I need another board, because the memory is very small for all the process, I try another atmega328 version.
<a title="New board"><img class="image center" src="images/week11/newboard.jpg"></a>
In this version I use some pins to Vcc and GND for some sensors. To upload a code to the atmega328, is necessary to use this configuration:
<ul>
<li>Board: Arduino uno.</li>
<li>Programmer: USBtinyISP.</li>
</ul>
<a title="IDE configuration"><img class="image center" src="images/week11/atmegaIDE.jpg"></a>
I connect my ISP and FTDI to upload a simple blink, but I have some problems. It’s possible to burn the bootloader.
<a title="Bootloader"><img class="image center" src="images/week11/bootloaderatmega.jpg"></a>
When I try to upload my code, I have an error:
<a title="Time error"><img class="image center" src="images/week11/atmegaerror.jpg"></a>
I will review my schematic one more time to find the problem, I try to use finally my atmega board to the output board for the next week.<br>
In the following video we can see the operation of both sensors separately.
<iframe width="560" height="315" src="https://www.youtube.com/embed/DvJmeJwwtBU" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<br><h2>Problems</h2>
In programming the temperature sensor, I used some work codes from years past of various fabacademy students, but when using the software, I always had problems with memory space, as seen in the picture.
<a title="Temperature error"><img class="image center" src="images/week11/temperror.jpg"></a>
That was the main reason to investigate various codes to get one that approximates the actual temperature. For my project I will use other types of more commercial sensors, which has a simpler measurement as well as a conversion formula easier to interpret and modify.
<p>
-->
<h2>Download files</h2>
You can download files Here: <br>
<!--
Code<br>
<a href="images/week11/thermistor.ino" download>Thermistor code</a><br>
<a href="images/week11/distance.ino" download>Distance code</a><br>
<a href="images/week11/inputs.ino" download>Distance and Thermistor code</a><br>
-->
<a href="images/week11/rgb_servoC.ino" download>Integration code</a><br>
<a href="images/week11/rgbarduino.ino" download>RGB Arduino</a><br>
<a href="images/week11/rgbarduinopwm.ino" download>RGB PWM Arduino</a><br>
<a href="images/week11/servo.ino" download>Servo Arduino</a><br>
<a href="images/week11/servocedwin.ino" download>Servo C</a><br>
<a href="images/week11/rgbc.ino" download>RGB C</a><br>
Board<br>
<a href="images/week12/rgb_servo.sch" download>Schematic design</a><br>
<a href="images/week11/rgb_servo.brd" download>Board design</a><br>
......
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