arduino uno PWM的for语句执行中按按钮怎么立即停止

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了arduino uno PWM的for语句执行中按按钮怎么立即停止相关的知识,希望对你有一定的参考价值。

我需要PWM控制LED亮度变化,按一下按钮LED灯开始亮,但亮度变化途中按按钮没有反应,必须按住按钮等FOR整个执行完才会灭,这样应该怎么改

int i=0;
int val=0;
int oldval=0;
int state=0;
boolean buttonUp= true;

void setup()
pinMode(7,INPUT);
digitalWrite(7,HIGH);
pinMode(10,OUTPUT);
digitalWrite(10,LOW);
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
pinMode(3,OUTPUT);
digitalWrite(3,LOW);
pinMode(4,OUTPUT);
digitalWrite(4,LOW);


void loop()
val=digitalRead(7);
if(val !=HIGH && oldval==LOW && buttonUp == true)
state=1-state;
delay(100);
buttonUp = false;

else if(val ==HIGH && buttonUp != true)
buttonUp = true;

oldval=val;

if(state==1)
for(i = 0;i < 255;i++)
analogWrite(10, i);
delay(10);

for(i =255;i >= 0; i--)
analogWrite(10, i);
delay(10);


if(state==0)
digitalWrite(10,LOW);
delay(100);




还有如果LED改为直流马达怎么接线,用在普通接角上马达几乎不会动

参考技术A 你要想驱动马达的话需要专门的驱动板,arduino 提供电压电流有限,没办法直接驱动 参考技术B 方法可以使用中断,或者在for循环中检测按键.

Arduino UNO 学习 抢答器PWM调光蜂鸣器感光灯三色灯热敏传感器倾斜传感器火焰传感器

(一)、抢答器:

代码

 1 int redled = 8;
 2 int yellowled = 7;
 3 int greenled = 6;
 4 
 5 int redpin = 5;
 6 int yellowpin = 4;
 7 int greenpin = 3;
 8 
 9 int restpin = 2;
10 int red,yellow,green;
11 
12 void setup() {
13   // put your setup code here, to run once:
14   pinMode(redled, OUTPUT);
15   pinMode(yellowled, OUTPUT);
16   pinMode(greenled, OUTPUT);
17   pinMode(redpin, INPUT);
18   pinMode(yellowpin, INPUT);
19   pinMode(greenpin, INPUT);
20 }
21 
22 void loop() {
23   // put your main code here, to run repeatedly:
24   red = digitalRead(redpin);
25   yellow = digitalRead(yellowpin);
26   green = digitalRead(greenpin);
27   
28   if (red == LOW) { RED_YES(); }
29   if (yellow == LOW) { YELLOW_YES(); }
30   if (green == LOW) { GREEN_YES(); }
31 }
32 
33 void RED_YES(){
34   while(digitalRead(restpin) == 1){
35     digitalWrite(redled, HIGH);
36     digitalWrite(greenled, LOW);
37     digitalWrite(yellowled, LOW);
38   }
39   CLEAR_LED();
40 }
41 
42 void YELLOW_YES(){
43   while(digitalRead(restpin) == 1){
44     digitalWrite(redled, LOW);
45     digitalWrite(greenled, LOW);
46     digitalWrite(yellowled, HIGH);
47   }
48   CLEAR_LED();
49 }
50 
51 void GREEN_YES(){
52   while(digitalRead(restpin) == 1){
53     digitalWrite(redled, LOW);
54     digitalWrite(greenled, HIGH);
55     digitalWrite(yellowled, LOW);
56   }
57   CLEAR_LED();
58 }
59 
60 void CLEAR_LED(){
61   digitalWrite(redled, LOW);
62   digitalWrite(greenled, LOW);
63   digitalWrite(yellowled, LOW);
64   }

技术图片

 

(二)、PWM调光

代码

int potpin = 0;
int ledpin = 11;
int val = 0 ;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledpin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  val = analogRead(potpin);
  Serial.println(val);
  analogWrite(ledpin, val/4);
  delay(10);
}

技术图片

 

(三)、蜂鸣器

代码

int buzzer = 8;
// buzzer break, can not make noise
void setup() {
  // put your setup code here, to run once:
  pinMode(buzzer, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  long frequency = 300;
  tone(buzzer, frequency);
  delay(100);

  noTone(buzzer);
  delay(2000);
//  while(1){
//    for(int i; i < 80; i++){
//      digitalWrite(buzzer, HIGH);
//      delay(1);
//      digitalWrite(buzzer, LOW);
//      delay(1);
//      }
//    for(int i; i < 100; i++){
//      digitalWrite(buzzer, HIGH);
//      delay(2);
//      digitalWrite(buzzer, LOW);
//      delay(2);
//      }
//    }
}

技术图片

 

(四)、感光灯

代码

int potpin = 0;
int ledpin = 11;
int val;

void setup() {
  // put your setup code here, to run once:
  pinMode(potpin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  val = analogRead(potpin);
  Serial.println(val);
  analogWrite(ledpin, val);
  delay(10);
}

技术图片

 

(五)、三色灯

代码

int redpin = 11;
int bluepin = 10;
int greenpin = 9;

int redval, blueval, greenval;
int i = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(redval, INPUT);
  pinMode(blueval, INPUT);
  pinMode(greenval, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  i++;
  if(i < 200){
    redval += 1;
    greenval -= 1;
    blueval = 1;
    }
  else if(i < 400){
    redval -= 1;
    greenval = 1;
    blueval += 1;
    }
  else if(i < 600){
      redval = 1;
      greenval += 1;
      blueval -= 1;
      }
  else { i = 0; }

  analogWrite(redpin, redval);
  analogWrite(greenpin, greenval);
  analogWrite(bluepin, blueval);

  Serial.print(i, DEC);
  Serial.print( "   R:");
  Serial.print(redval, DEC);

  Serial.print( "   B:");
  Serial.print(blueval, DEC);

  Serial.print( "   G:");
  Serial.print(greenval, DEC);
}

技术图片

 

(六)、热敏传感器

代码

int sensorInPin = 0;
int ledpin = 8;
int sensorVal = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledpin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  sensorVal = analogRead(sensorInPin);
  Serial.print(sensorVal);
  analogWrite(ledpin, sensorVal);
  delay(5000);
}

技术图片

 

(七)、倾斜传感器

代码

int sensorInPin = A5;
int ledpin = 8;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledpin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int i;
  while(1){
    i = analogRead(sensorInPin);
    Serial.print(i);
    if(i > 1000){
      digitalWrite(ledpin, HIGH);
      }
    else { digitalWrite(ledpin, LOW);}
    }
}

技术图片

 

(八)、火焰传感器

代码

int fireInPin = A5;
int beep = 8;
int val = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(beep, OUTPUT);
  //pinMode(fireInPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  //while(1){
    val = analogRead(fireInPin);
    Serial.print(val);
    Serial.print("
");
    if(val > 200){
      digitalWrite(beep, HIGH);
      }
    else { digitalWrite(beep, LOW); }
   // }
}

忘记拍照了!

 

(九)、声音传感器

代码

int soundInPin = 2;
int ledpin = 13;
int val;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledpin, OUTPUT);
  pinMode(soundInPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  val = analogRead(soundInPin);
  Serial.println(val);
  delay(2000);

  if(val == HIGH){
    digitalWrite(ledpin, HIGH);
    delay(5000);
    digitalWrite(ledpin, LOW);
    }
}

忘记拍照了!

 

总结:

(1)虽说面包板+加跳线更加灵活,但是也容易接触不牢的问题。

(2)蜂鸣器中要使用Tone()函数,在无源蜂鸣器的应用中。

(3)Serial.begin(9600),未开启串口函数,9600为波特率(bps, byte per second),波特率表示每秒钟传送的码元符号的个数,

是衡量数据传送速率的指标,它用单位时间内载波调制状态改变的次数来表示;9600的波特率的信道,

理论上每秒可以传输9600个二进制位,也就是9600/8个英文字母的数据量,也就是1200个字节,大约1.2KB。

而19200则是每秒可传输2400字节,大约2.4KB。

(4)Serial.print(a, DEC),为输出十进制数b。Serial函数的用法参考https://blog.csdn.net/hhaowang/article/details/88529842

(5)火焰传感器并不是通过火焰的温度来实现的,而是通过亮度来实现的。

(6)声音传感器中需要50k欧的电阻,这里没有就没有实现。

(7)是否是长时间使用UNO,会出现设备不稳定的情况?,再没有外接输入源的时候,模拟(或数字)信号端,有信号的变化,

最大到1023!不知道怎么回事!

(8)关于硬件方面的使用还需要加强!

以上是关于arduino uno PWM的for语句执行中按按钮怎么立即停止的主要内容,如果未能解决你的问题,请参考以下文章

可以用Arduino Uno具有PWM输出功能的引脚控制无源蜂鸣器的音调吗?

Arduino UNO 学习 抢答器PWM调光蜂鸣器感光灯三色灯热敏传感器倾斜传感器火焰传感器

Arduino UNO产生100Hz和500Hz的方波在示波器捕捉下的波形图

arduino leonardo r3 和 arduino uno r3 有啥区别

Arduino UNO利用外部中断测量RC接收器PPM脉宽

初学arduino应该买leonardo还是uno?请说明为啥。