งานปฏิบัติ 16
งานปฏิบัติ 16
การควบคุมมอเตอร์
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);
}
#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);
}
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);
}