Sunteți pe pagina 1din 6

Nama : Chika Vevita Novelia

Kelas : 3 D4 Teknik Telekomunikasi A


NRP : 1210171018
SOAL UTS
Tugas Mikrokontroller
1. Rangkaian

Source Code :
Arduino
float temp;
int tempPin = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
temp = analogRead(tempPin);
temp = temp * 0.48828125;
Serial.println(temp);
delay(1000);
}
Processing
import processing.serial.*; fill(255,255,255);
Serial commPort; rect(410,10, 100, 300);
float tempC; }
float tempF; else if (num <= 25 && num >= 21 ){
String myString = null; fill(255,255,255);
float num; rect(10,10, 100, 300);
fill(255,255,0);
void setup() rect(210,10, 100, 300);
{ fill(255,255,255);
size(600,400); rect(410,10, 100, 300);
commPort = new Serial(this, "COM10", }
9600); else if (num < 21){
} fill(255,255,255);
rect(10,10, 100, 300);
void draw(){ fill(255,255,255);
while (commPort.available() > 0) { rect(210,10, 100, 300);
myString = fill(0,255,0);
commPort.readStringUntil(10);
rect(410,10, 100, 300);
if (myString != null) {
num=float(myString);
}
if (num > 25){
}
fill(255,0,0);
}
rect(10,10, 100, 300);
}
fill(255,255,255);
rect(210,10, 100, 300);

Output

\
2. Rangkaian

File notepad :
Source Code

Arduino
void setup() {
pinMode(2, OUTPUT); //Turn off all LEDs if the byte Read = 0
pinMode(3, OUTPUT); if(byteRead==0){
pinMode(4, OUTPUT); //Turn off all LEDS
pinMode(5, OUTPUT); digitalWrite(2, LOW);
pinMode(6, OUTPUT); digitalWrite(3, LOW);
pinMode(7, OUTPUT); digitalWrite(4, LOW);
pinMode(8, OUTPUT); digitalWrite(5, LOW);
pinMode(9, OUTPUT); digitalWrite(6, LOW);
pinMode(10, OUTPUT); digitalWrite(7, LOW);
Serial.begin(9600); digitalWrite(8, LOW);
} digitalWrite(9, LOW);
digitalWrite(10, LOW);
void loop() { }
byte byteRead;
if(byteRead>0){
if (Serial.available()) { digitalWrite((byteRead+1), HIGH); // set
the LED on
byteRead = Serial.read();
}
//You have to subtract '0' from the read Byte
to convert from text to a number. }
byteRead=byteRead-'0'; }

Processing
import processing.serial.*; //This is turned off when mySwitch = 0
import java.io.*; mySwitch=1;
int mySwitch=0;
int counter=0; //Open the serial port for communication
with the Arduino
String [] subtext;
//Make sure the COM port is correct
Serial myPort;
myPort = new Serial(this, "COM10", 9600);
myPort.bufferUntil('\n');
}
void setup(){
//Create a switch that will control the
frequency of text file reads. void draw() {
//When mySwitch=1, the program is setup if (mySwitch>0){
to read the text file.
/*The readData function can be found later
in the code. /* The following function will read from a
This is the call to read a CSV file on the CSV or TXT file */
computer hard-drive. */ void readData(String myFileName){
readData("D:lampu.txt");

File file=new File(myFileName);


/*The following switch prevents continuous BufferedReader br=null;
reading of the text file, until
we are ready to read the file again. */
try{
mySwitch=0;
br=new BufferedReader(new
}
FileReader(file));
/*Only send new data. This IF statement String text=null;
will allow new data to be sent to
the arduino. */
/* keep reading each line until you get to
if(counter<subtext.length){ the end of the file */
/* Write the next number to the Serial port while((text=br.readLine())!=null){
and send it to the Arduino
/* Spilt each line up into bits and pieces
There will be a delay of half a second using a comma as a separator */
before the command is
subtext = splitTokens(text,",");
sent to turn the LED off : myPort.write('0');
*/ }
myPort.write(subtext[counter]); }catch(FileNotFoundException e){
delay(500); e.printStackTrace();
myPort.write('0'); }catch(IOException e){
delay(100); e.printStackTrace();
//Increment the counter so that the next }finally{
number is sent to the arduino. try {
counter++; if (br != null){
} else{ br.close();
//If the text file has run out of numbers, then }
read the text file again in 5 seconds.
} catch (IOException e) {
delay(5000);
e.printStackTrace();
mySwitch=1;
}
}
}
}
}
3. Rangkaian

Source Code:
Arduino
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {

// put your main code here, to run repeatedly:


for(int a=0; a<=100; a++)
{ Output:
int b=0;
Serial.println(b);
delay(1);
}
for(int a=0; a<=100; a++)
{
int b=100;
Serial.println(b);
delay(1);
}

S-ar putea să vă placă și