วันเสาร์ที่ 21 กรกฎาคม พ.ศ. 2561
งานที่ 7
#include "LedControl.h"
#define C4 262
#define D4 294
#define E4 330
#define F4 349
#define G4 392
#define A4 440
#define B4 494
#define C5 523
int melody[] = {C4,D4,E4,F4,G4,A4,B4,C5};
float beats[] ={1,1,1,1,1,1,1,1};
int buzzerpin = 11;
int timestop = 70;
LedControl lc=LedControl(8,10,9,1);
// Pin 8->DIN, 10->CLK, 9->CS(LOAD), 1 = No.of devices
void setup()
{
lc.shutdown(0,false);
lc.setIntensity(0,5);
lc.clearDisplay(0);
int dl = 500;
pinMode(buzzerpin,OUTPUT);
int numnote;
numnote = sizeof(melody)/2;
for (int i=0;i<numnote;i++)
{
lc.setChar(0,7-i,'-',false);
tone(buzzerpin, melody[i],dl*beats[i]);
delay(dl*beats[i]);
digitalWrite(buzzerpin,HIGH);
delay(timestop);
}
}
void loop()
{
}
งานที่ 6
#include "LedConyrol.h"
#include "Keypad.h"
char keys[4][4]={
{'7','8','9','A'},
{'4','5','6','B'},
{'1','2','3','C'},
{'*','0','#','D'}};
byte rowPins[] = {7,6,5,4};
byte colPins[] = {3,2,1,0};
Keypad keypad = keypad(makeKeymap(keys),rowPins,colPins,4,4);
LedControl lc=LedControl(8,10,9,1);
int m=7;
void setup()
{
lc.shutdown(0,false);
lc.setIntensity(0,5);
lc.clearDisplay(0);
}
void loop]()
{
char key = keypad.getKey();
if (key !=NO_KEY)
{
if (m==7)
lc.clearDisplay(0);
lc.setChar(0,m,key,false);
m = m-1;
if (m < 0)
m = 7;
}
}
งานที่ 5
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 4, d5 = 3, d6 = 2, d7 = 1;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int led1 = 9;
int led2 = 8;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("PIN YUTTASIN!");
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
}
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
delay(2000);
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
delay(2000);
digitalWrite(led1,LOW);
digitalWrite(led2,HIGH);
delay(2000);
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
delay(2000);
}
งานที่ 3
int led1=13;
int led2=12;
int led3=11;
int led4=10;
int button1=9;
int buttonStata =0;
void setup() {
pinMode(led1 , OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(button1,INPUT);
}
void loop()
{
buttonStata=digitalRead(button1);
if(buttonStata == HIGH){
digitalWrite(led1,HIGH);
delay(500);
digitalWrite(led2,HIGH);
digitalWrite(led1,LOW);
delay(500);
digitalWrite(led2,HIGH);
delay(500);
digitalWrite(led3,HIGH);
digitalWrite(led2,LOW);
delay(500);
digitalWrite(led3,HIGH);
delay(500);
digitalWrite(led4,HIGH);
digitalWrite(led3,LOW);
delay(500);
digitalWrite(led4,HIGH);
delay(500);
digitalWrite(led4,LOW);
delay(500);
buttonStata =0;
}else{
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
}
}
งานที่ 2
int led1=13;
int led2=12;
int button1=9;
int button2=10;
int buttonStata =0;
void setup() {
pinMode(led1 , OUTPUT);
pinMode(led2, OUTPUT);
pinMode(button1,INPUT);
pinMode(button2,INPUT);
}
void loop()
{
buttonStata=digitalRead(button1);
if(buttonStata == HIGH){
digitalWrite(led1,HIGH);
delay(2000);
digitalWrite(led2,HIGH);
digitalWrite(led1,LOW);
delay(2000);
buttonStata =0;
}else{
digitalWrite(led1,LOW);
}
buttonStata=digitalRead(button2);
if(buttonStata == HIGH){
digitalWrite(led2,HIGH);
delay(2000);
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
delay(2000);
buttonStata =0;
}else{
digitalWrite(led2,LOW);
}
}
งานที่ 1
int motorpin =2;
void setup() {
pinMode(motorpin,OUTPUT);
Serial.begin(9600);
while(! Serial);
Serial.println ("Speed 0 to 255");
}
void loop(){
if(Serial.available())
{
int speed = Serial.parseInt();
if(speed >=0 && speed <=255)
{
analogWrite(motorpin,speed);
}
}
}
วันอาทิตย์ที่ 15 กรกฎาคม พ.ศ. 2561
ส่งงานครั้งที่5 ตัวอย่าง หุ่นยนต์เดินตามเส้น 2 เซ็นเซอร์ Arduino
หุ่นยนต์เดินตามเส้น 2 เซ็นเซอร์ Arduino
อุปกรณ์ที่ต้องใช้ก็คือ
12. แผ่นอะคริลิค
ตัวอย่างโค้ด Arduino
/*-------definning Inputs------*/
int LS = 2; // left sensor
int RS = 3; // right sensor
/*-------definning Outputs------*/
int LM1 = 4; // left motor
int LM2 = 5; // left motor
int RM1 = 6; // right motor
int RM2 = 7; // right motor
/*-------PWM ควบคุมความเร็วมอเตอร์-----*/
int PWM1 = 10; // PWM left motor
int PWM2 = 11; // PWM right motor
int SPEED = 90; // Speed PWM สามารถปรับความเร็วได้ถึง 255
void setup()
{
pinMode(LS, INPUT);
pinMode(RS, INPUT);
pinMode(LM1, OUTPUT);
pinMode(LM2, OUTPUT);
pinMode(RM1, OUTPUT);
pinMode(RM2, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(PWM2, OUTPUT);
}
void loop()
{
if (!(digitalRead(LS)) && !(digitalRead(RS))) // Move Forward
{
analogWrite(PWM1, SPEED);
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
analogWrite(PWM2, SPEED);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
if (!(digitalRead(LS)) && digitalRead(RS)) // Turn right
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
analogWrite(PWM2, SPEED);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
if (digitalRead(LS) && !(digitalRead(RS))) // turn left
{
analogWrite(PWM1, SPEED);
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}
if (digitalRead(LS) && digitalRead(RS)) // stop
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}
}
int LS = 2; // left sensor
int RS = 3; // right sensor
/*-------definning Outputs------*/
int LM1 = 4; // left motor
int LM2 = 5; // left motor
int RM1 = 6; // right motor
int RM2 = 7; // right motor
/*-------PWM ควบคุมความเร็วมอเตอร์-----*/
int PWM1 = 10; // PWM left motor
int PWM2 = 11; // PWM right motor
int SPEED = 90; // Speed PWM สามารถปรับความเร็วได้ถึง 255
void setup()
{
pinMode(LS, INPUT);
pinMode(RS, INPUT);
pinMode(LM1, OUTPUT);
pinMode(LM2, OUTPUT);
pinMode(RM1, OUTPUT);
pinMode(RM2, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(PWM2, OUTPUT);
}
void loop()
{
if (!(digitalRead(LS)) && !(digitalRead(RS))) // Move Forward
{
analogWrite(PWM1, SPEED);
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
analogWrite(PWM2, SPEED);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
if (!(digitalRead(LS)) && digitalRead(RS)) // Turn right
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
analogWrite(PWM2, SPEED);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
if (digitalRead(LS) && !(digitalRead(RS))) // turn left
{
analogWrite(PWM1, SPEED);
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}
if (digitalRead(LS) && digitalRead(RS)) // stop
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}
}
สมัครสมาชิก:
บทความ (Atom)