0% found this document useful (0 votes)
56 views

Arduino 程式碼

The document describes code for controlling stepper motors and LEDs using Arduino. It defines functions for: 1) Setting up pin modes and initializing stepper motor objects for clockwise and counterclockwise rotation. 2) Creating threads to control motor rotation speed and direction by reading button inputs and calling stepper motor methods. 3) Driving the stepper motors in different patterns by toggling which coils are enabled through boolean arrays and counters.

Uploaded by

beefong7680
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Arduino 程式碼

The document describes code for controlling stepper motors and LEDs using Arduino. It defines functions for: 1) Setting up pin modes and initializing stepper motor objects for clockwise and counterclockwise rotation. 2) Creating threads to control motor rotation speed and direction by reading button inputs and calling stepper motor methods. 3) Driving the stepper motors in different patterns by toggling which coils are enabled through boolean arrays and counters.

Uploaded by

beefong7680
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Void setup(){

//(13 )!
pinMode(13,output);
}

Void loop(){
digitalWrite(13,HIGH);
delay(1000); //1000
digitalWrite(13,Low);
delay(1000);
}



#include <Stepper.h>
#include <Thread.h>
#include <ThreadController.h>

// number of steps on your motor
#define STEPS 200
#define RotateSpeedMax 150
#define RotateSpeedMin 10

char cmd;
int RotateSpeed = 50;
int AcpreviousButtonState = HIGH;
int DepreviousButtonState = HIGH;

ThreadController controll = ThreadController();
Thread Thread1 = Thread();
Thread Thread2 = Thread();

Stepper cw(STEPS, 8, 9, 11, 12);//clockwise
Stepper ccw(STEPS, 12, 11, 9, 8);//counterclockwise

void RotationSpeed(){
//Press 4 to increase the RotatingSpeed
int AcbuttonState = digitalRead(4);
if ((AcbuttonState != AcpreviousButtonState)
// and it's currently pressed:
&& (AcbuttonState == HIGH)&& RotateSpeed < RotateSpeedMax) {
// increment the button counter
RotateSpeed = RotateSpeed+5;
cw.setSpeed(RotateSpeed);
ccw.setSpeed(RotateSpeed);
}
AcpreviousButtonState = AcbuttonState;

//Press 5 to decrease the RotatingSpeed
int DebuttonState = digitalRead(5);
if ((DebuttonState != DepreviousButtonState)
// and it's currently pressed:
&& (DebuttonState == HIGH)&& RotateSpeed > RotateSpeedMin) {
// increment the button counter
RotateSpeed = RotateSpeed-5;
cw.setSpeed(RotateSpeed);
ccw.setSpeed(RotateSpeed);
}
DepreviousButtonState = DebuttonState;
}

void RotationDirection(){
//press 2 for clockwise
if(digitalRead(2) == HIGH && digitalRead(3) == LOW)
{
cw.step(10);
}
//press 3 for counterclockwise
else if(digitalRead(2) == LOW && digitalRead(3) == HIGH)
{
ccw.step(10);
}
//press both for sweep
else if(digitalRead(2) == HIGH && digitalRead(3) == HIGH)
{
cw.step(75);
ccw.step(150);
cw.step(75);
}
}

void setup()
{
//set motor speed
cw.setSpeed(RotateSpeed);
ccw.setSpeed(RotateSpeed);

//Configure Thread1
Thread1.onRun(RotationSpeed);
Thread1.setInterval(10);

//Configure Thread2
Thread2.onRun(RotationDirection);
Thread2.setInterval(10);

controll.add(&Thread1);
controll.add(&Thread2);
}

void loop()
{
controll.run();
}











int motorPin1 = 9;
int motorPin2 = 10;
int motorPin3 = 11;
int motorPin4 = 12;
int ledPin = 13;
boolean motorX[4]={true,true,true,true};
boolean turn_R_or_L=true;
boolean tempBool=false;
int count=0;
int potPin = 5;
int val = 0;
int speed=10;
int motorStartType=1;
void setup()
{
Serial.begin(9600);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(ledPin, OUTPUT);
}

void loop()
{
tempBool = !tempBool;
digitalWrite(ledPin,tempBool);
putAnalog();
oneHIGT();
motorGo(turn_R_or_L);

delay(speed); //delyMicroseconds(speed*0.001);
}
void putAnalog()
{
val = analogRead(potPin);
if(val>512) turn_R_or_L=true;
else turn_R_or_L=false;
}
void oneHIGT()
{
for(int x=0;x<=3;x++)
{
if(x==count) motorX[x]=true;
else motorX[x]=false;
}
if(count==3) count=0;
else count++;
}
void twoHIGT()
{
switch (count)
{
case 0:
motorX[0]=true;
motorX[1]=true;
motorX[2]=false;
motorX[3]=false;
break;
case 1:
motorX[0]=false;
motorX[1]=true;
motorX[2]=true;
motorX[3]=false;
break;
case 2:
motorX[0]=false;
motorX[1]=false;
motorX[2]=true;
motorX[3]=true;
break;
case 3:
motorX[0]=true;
motorX[1]=false;
motorX[2]=false;
motorX[3]=true;
break;
} if(count==3) count=0;
else count++;
}
void bothHIGT(int who)
{}
void motorGo(boolean RL)
{
if(RL){
digitalWrite(motorPin1, motorX[0]);
digitalWrite(motorPin2, motorX[1]);
digitalWrite(motorPin3, motorX[2]);
digitalWrite(motorPin4, motorX[3]);
}
else{
digitalWrite(motorPin4, motorX[0]);
digitalWrite(motorPin3, motorX[1]);
digitalWrite(motorPin2, motorX[2]);
digitalWrite(motorPin1, motorX[3]);
}
}




http://ming-shian.blogspot.tw/2013/05/arduinouln2003.html

You might also like