arduino渐变跑马灯的程序:
让7个LED通过模拟电压值进行交替的变化:
#define STEPS 100
int ledPin[] ={5,6,7,8,9,10,11}; // LED connected to digital pin 9
int ledcount=4;
void setup() {
for(int led=0;led<ledcount;led++) //定义5-11号的引脚为输出;
pinMode(ledPin[led],OUTPUT);
}
void loop() { //循环函数;
fading();
}
void fading()
{
for(int led=0; led<ledcount;led++)
{
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { //渐渐变亮;
// sets the value (range from 0 to 255):
analogWrite(ledPin[led], fadeValue); //向定义的引脚写入一个模拟电压值;
// wait for 30 milliseconds to see the dimming effect
delay(10);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {//由亮变暗;
// sets the value (range from 0 to 255):
analogWrite(ledPin[led], fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(10);
}
}
}
arduino步进电机操控代码:
控制步进电机的转速以及单步转动的夹角的设置:
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 100
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 4, 5, 6, 7);//ina ,inb ,inc,ind;); 设置步进电机的引脚接入引脚;
// the previous reading from the analog input
int previous = 0;
void setup()
{
// set the speed of the motor to 30 RPMs
stepper.setSpeed(120);//set speed; //设置电机的转速,但是次函数不会使电机转动;
}
void loop()
{
// get the sensor value
int val = analogRead(0);
// move a number of steps equal to the change in the
// sensor reading
stepper.step(val - previous);//此函数的作用是控制步进电机按照setspeed()设置的转速转动一定的步距角;
// remember the previous value of the sensor
previous = val;
}