CÓDIGOS
Imagem para avaliação de Métodos Computacionais
Salvar como engmec.jpg no diretório atual do Octave em seu computador.
ARDUINO
Arduino e sensor ultrassônico HC-SR04
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with Arduino UNO R3");
}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
Arduino e leitura de sensor analógico
const int sensorPin = A0;
int sensorValue = 0;
void setup() {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
}
BRICX NXC LEGO NXT MINDSTORMS
Escrever no display e mover robô
#define motores OUT_AB
#define velocidade 60
task main()
{
TextOut(0,LCD_LINE1,"iniciando...");
Wait(500);
ClearLine(LCD_LINE1);
TextOut(0,LCD_LINE4,"MOVER!");
Wait(500);
ClearScreen();
OnFwdSync(motores, 60,0); // frente
Wait(1000);
Off(motores);
OnRevSync(motores, 60,0); // trás
Wait(1000);
Off(motores);
OnFwdSync(motores, 60,-100); // esquerda
Wait(600);
Off(motores);
OnRevSync(motores, 60,100); // direita
Wait(600);
Off(motores);
}
Ler e escrever valores dos sensores - luz, toque e ultrassom
task main()
{
///////// SENSOR DE LUZ NA PORTA 1
SetSensorLight(S1);
int valorSensorLuz;
valorSensorLuz = Sensor(S1);
TextOut(0,LCD_LINE1,"reflexao:");
NumOut(65,LCD_LINE1, valorSensorLuz);
Wait(3000);
ClearScreen();
///////// SENSOR DE TOQUE NA PORTA 2
SetSensorTouch(S2);
int valorSensorToque ;
valorSensorToque = Sensor(S2);
TextOut(0,LCD_LINE2,"Estado:");
NumOut(65,LCD_LINE2, valorSensorToque);
Wait(2000);
ClearScreen();
///////// SENSOR ULTRASSÔNICO NA PORTA 4
SetSensorLowspeed(IN_4);
int valorSensorUS;
valorSensorUS = SensorUS(IN_4);
TextOut(0,LCD_LINE3,"distancia:");
NumOut(0,LCD_LINE4,valorSensorUS);
Wait(5000);
}
LINGUAGEM C
Ler 2 valores e escrever na tela - SCANF e PRINTF
#include <stdio.h>
int main() {
int A, B;
printf("\nDigite o valor de A:");
scanf("%i",&A);
printf("\nDigite o valor de B:");
scanf("%i",&B);
printf("O valor de A = %i e o valor de B = %i", A, B);
return 0;
}
Cáculo da velocidade (v = vo + at)
#include <stdio.h>
int main(){
float v, vo, a, t;
printf("\nInforme a velocidade inicial:");
scanf("%f",&vo);
printf("\nInforme a aceleracao:");
scanf("%f",&a);
printf("\nInforme o tempo:");
scanf("%f",&t);
v = vo + a*t;
printf("%.2f eh a velocidade e a aceleracao eh %.2f",v,a);
return 0;
}
Média de 10 números informados pelo usuário usando WHILE
#include<stdio.h>
int main()
{
int i=1, soma = 0, N;
while(i<=10)
{
scanf("%i",&N);
soma = soma + N;
i=i+1;
}
float media;
media = soma/10;
printf("a media foi = %.2f",media);
return 0;
}
Relógio com 3 laços de repetição aninhados - FOR
#include <stdio.h>
#include <windows.h>
int main() {
int hora, min, seg;
while(1)
{
for(hora=0;hora<24;hora++)
{
for(min=0;min<60;min++)
{
for(seg=0;seg<60;seg++)
{
printf("%.2d:%.2d:%.2d",hora,min,seg);
Sleep(1000);
system("cls");
}
}
}
}// FIM WHILE
return 0;
}
OCTAVE
Arquivos TXT de dados - AULA PID
Plotagem de uma função do primeiro grau y = x + 2
x = [2,2.5,2.8,3.3,3.7,4.6];
y = x+2;
plot(x,y);
axis([0 8 0 10]);
Gráfico com um ponto na tela - PLOT
x = 20;
y = 4;
plot(x,y,'bo','markerfacecolor','k');
text(x+0.02,y+0.02,'Um ponto na tela');
title('Grafico XY - ponto');
Gráfico com duas funções - POLYVAL
x = [-5.5:0.1:2];
c_1 = [2 10.1 0 6];
c_2 = [2 10.1 -10.1 6];
f_1 = polyval(c_1, x);
f_2=polyval(c_2, x);
plot(x, f_1, "linewidth", 5, x, f_2,"linewidth", 5, "color", "red")
Repetição usando o comando FOR
clear all, clc;
printf("**** USANDO O FOR ***** \n\n");
for i=1:10
printf("%i EngMEc \n", i);
endfor
printf("\n**** FIM DO SCRIPT *****\n");
Gráfico de uma superfície 3D formato de trombeta
clear all, clc;
x = linspace(1,4,25) ; % definição do domínio
f = @(x) x .^2 - 4*x + 5 ; % definição da função inline
t = linspace ( 0 , 2*pi , 25) ; % definição dos parâmetros
[X T] = meshgrid(x,t) ; % (x , t )mesh
Y = f(X).*cos(T); % calcula o Y
Z = f(X).*sin(T) ; % calcula o Z
surf(X, Y, Z) % gráfico da superfície
Processamento de imagem
pkg load image;
clear all;clc;
clear all; clc;
imagem = imread("gear.jpg");
tamanho = size(imagem);
largura = tamanho(1,1);
altura = tamanho(1,2);
cinza = rgb2gray(imagem);
pb1 = im2bw(cinza);
P = cinza;
subplot(1,3,1);
imshow(cinza);
title("CINZA");
subplot(1,3,2);
imshow(pb1);
title("PRETO E BRANCO AUTO");