Arduino下跑马灯和步进电机控制代码实现

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;
}


  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值