งานปฏิบัติที่ 17
wiring diagram
รูปที่1
Code 1
#include <Stepper.h>
#include "Keypad.h"
#define STEP_ANGLE_4STEP 32 //360/11.25 degree
#define STEP_OUT_WITH_GEAR 2048 //32*64
Stepper stepper(STEP_ANGLE_4STEP,8,10,9,11);
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};
int speedmotor = 400;
int dirmotor = 1;
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,4,4);
void setup()
{
}
void loop()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
if (key == '1')
speedmotor = 400;
if (key == '2')
speedmotor = 700;
if (key == '3')
speedmotor = 1000;
if (key == 'A')
dirmotor = 1;
if (key == 'B')
dirmotor = -1;
if (key == 'C')
dirmotor = 0;
}
stepper.setSpeed(speedmotor);
stepper.step(dirmotor);
}
wiring diagram
รูปที่ 2
Code 2
#include <Servo.h>
Servo servo;
int x,y;
void setup()
{
servo.attach(9);
}
void loop()
{
x = analogRead(A0);
y = map(x,0,1023,0,179);
servo.write(y);
delay(50);
}
รูปที่ 3
Code 3
const int SW1 = 2;const int SW2 = 3;const int SW3 = 4; const int SW4 = 5;
const int ledPin = 13;const int Relay1 = 12;const int Relay2 = 11;
const int Relay3 = 10; const int Relay4 = 9;
int checkSW1 = 0;int checkSW2 = 0;int checkSW3 = 0;int checkSW4 = 0;
void setup() {
//========กำหนด output================
pinMode(ledPin, OUTPUT);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
//========กำหนด input==================
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
pinMode(SW3, INPUT);
pinMode(SW4, INPUT);
//========กำหนดค่า output===================
digitalWrite(Relay1, LOW);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
}
void loop(){
//============ตรวจสอบการควบคุม SW================
checkSW1 = digitalRead(SW1);
checkSW2 = digitalRead(SW2);
checkSW3 = digitalRead(SW3);
checkSW4 = digitalRead(SW4);
//=============================================
if (checkSW1 == LOW) {
//UP();
digitalWrite(Relay1, HIGH);
}
if (checkSW2 == LOW) {
//DOWN();
digitalWrite(Relay2, HIGH);
}
if (checkSW3 == LOW) {
//RIGHT();
digitalWrite(Relay3, HIGH);
}
if (checkSW4 == LOW) {
//LEFT();
digitalWrite(Relay4, HIGH);
}
if (checkSW1 == HIGH && checkSW2 == HIGH && checkSW3 == HIGH && checkSW4 == HIGH) {
//STOP();
digitalWrite(Relay1, LOW);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
}
}
//=============function===================
void UP(){
// Serial.print('RRR');
digitalWrite(ledPin, HIGH);
digitalWrite(Relay1,LOW);
digitalWrite(Relay2,HIGH);
digitalWrite(Relay3,LOW);
digitalWrite(Relay4,HIGH);
}
void DOWN(){
// Serial.print('RRR');
digitalWrite(ledPin, HIGH);
digitalWrite(Relay1,HIGH);
digitalWrite(Relay2,LOW);
digitalWrite(Relay3,HIGH);
digitalWrite(Relay4,LOW);
}
void RIGHT(){
// Serial.print('RRR');
digitalWrite(ledPin, HIGH);
digitalWrite(Relay1,HIGH);
digitalWrite(Relay2,LOW);
digitalWrite(Relay3,LOW);
digitalWrite(Relay4,HIGH);
}
void LEFT(){
// Serial.print('RRR');
digitalWrite(ledPin, HIGH);
digitalWrite(Relay1,LOW);
digitalWrite(Relay2,HIGH);
digitalWrite(Relay3,HIGH);
digitalWrite(Relay4,LOW);
}
void STOP(){
// Serial.print('RRR');
digitalWrite(ledPin,LOW);
digitalWrite(Relay1,LOW);
digitalWrite(Relay2,LOW);
digitalWrite(Relay3,LOW);
digitalWrite(Relay4,LOW);
}
ความคิดเห็น
แสดงความคิดเห็น