官方教程2中电路的逻辑与和问题-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3587|回复: 0

官方教程2中电路的逻辑与和问题

[复制链接]
发表于 2013-5-4 19:07 | 显示全部楼层 |阅读模式
我按照官方教程搭建的电路,(没有微动开关,我直接用电路短接来代替开关)。但是。。。测试后红灯闪烁,绿灯不亮。不管开关线闭合与否。然后又调用了arduino的示例程序,发现与视频中的不同。那个delay(250)的语句,视频中是写到if里的,而示例是写到了else里。我修改调整过很多次后终于实现了视频中的效果,但是语句与原文已经大相径庭了。
这里我把官方的和我修改后得到的语句都贴出来。高手帮我分析一下这里面的逻辑关系。。
########################################################
官方视频版本:
[mw_shl_code=c,true]const int redled1=5;
const int redled2=4;
const int greenled=3;
const int switchPin=2;

void setup(){
pinMode(redled1,OUTPUT);
pinMode(redled2,OUTPUT);
pinMode(greenled,OUTPUT);
pinMode(switchPin,INPUT);
}
void loop(){
int switchstate;
switchstate=digitalRead(switchPin);
if(switchstate == LOW){
digitalWrite(greenled,LOW);
digitalWrite(redled1,HIGH);
digitalWrite(redled2,LOW);
delay(250);
digitalWrite(redled1,LOW);
digitalWrite(redled2,HIGH);
delay(250);
}
else{
digitalWrite(greenled,HIGH);
digitalWrite(redled1,LOW);
digitalWrite(redled2,LOW);
}
}[/mw_shl_code]
######################################################
示例版本:
[mw_shl_code=c,true]/*
  Arduino Starter Kit example
Project 2  - Spaceship Interface

This sketch is written to accompany Project 2 in the
Arduino Starter Kit

Parts required:
1 green LED
2 red LEDs
pushbutton
10 kilohm resistor
3 220 ohm resistors

Created 13 September 2012
by Scott Fitzgerald

http://arduino.cc/starterKit

This example code is part of the public domain
*/

// Create a global variable to hold the
// state of the switch. This variable is persistent
// throughout the program. Whenever you refer to
// switchState, you’re talking about the number it holds
int switchstate = 0;

void setup(){
  // declare the LED pins as outputs
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);

  // declare the switch pin as an input   
  pinMode(2,INPUT);
}

void loop(){

  // read the value of the switch
  // digitalRead() checks to see if there is voltage
  // on the pin or not  
  switchstate = digitalRead(2);

  // if the button is not pressed
  // blink the red LEDs  
  if (switchstate == LOW) {
    digitalWrite(3, HIGH); // turn the green LED on pin 3 on
    digitalWrite(4, LOW);  // turn the red LED on pin 4 off
    digitalWrite(5, LOW);  // turn the red LED on pin 5 off
  }
  // this else is part of the above if() statement.
  // if the switch is not LOW (the button is pressed)
  // the code below will run  
  else {
    digitalWrite(3, LOW);  // turn the green LED on pin 3 off
    digitalWrite(4, LOW);  // turn the red LED on pin 4 off
    digitalWrite(5, HIGH); // turn the red LED on pin 5 on
    // wait for a quarter second before changing the light
    delay(250);
    digitalWrite(4, HIGH); // turn the red LED on pin 4 on
    digitalWrite(5, LOW);  // turn the red LED on pin 5 off
    // wait for a quarter second before changing the light
    delay(250);
  }
}
[/mw_shl_code]
######################################################
最终修改实现效果版:
[mw_shl_code=c,true]const int redled1=5;
const int redled2=4;
const int greenled=3;

const int switchPin=2;

void setup(){
pinMode(redled1,OUTPUT);
pinMode(redled2,OUTPUT);
pinMode(greenled,OUTPUT);
pinMode(switchPin,INPUT);
}
void loop(){
int switchstate;
switchstate=digitalRead(switchPin);

if(switchstate == LOW){
digitalWrite(greenled,LOW);
digitalWrite(redled1,HIGH);
digitalWrite(redled2,HIGH);

}
else{
digitalWrite(greenled,HIGH);

digitalWrite(redled1,HIGH);
digitalWrite(redled2,LOW);

delay(250);
digitalWrite(redled1,LOW);
digitalWrite(redled2,HIGH);
delay(250);
}
}
[/mw_shl_code]

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-28 15:53 , Processed in 0.069164 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表