2020年2月1日 星期六

認識Arduino UNO 用類比輸入當數位輸出,原因數位腳不夠用

認識Arduino UNO:


(圖片來源:http://interactive2go.blogspot.tw/2009/04/get-to-start.html)

int ledPin1 =14;//用類比輸入當數位輸出,原因數位腳不夠用
int ledPin2 =15;
int delaytime=500;
void setup()
{
  // put your setup code here, to run once:
  pinMode(ledPin1,OUTPUT);
  pinMode(ledPin2,OUTPUT);
}

void loop() 
{
  // put your main code here, to run repeatedly:
  digitalWrite(ledPin1, HIGH);   // sets the LED on
  digitalWrite(ledPin2, LOW);   // sets the LED on
  delay(delaytime);                  // waits for a second
  digitalWrite(ledPin1, LOW);    // sets the LED off
  digitalWrite(ledPin2, HIGH);    // sets the LED off
  delay(delaytime);                  // waits for a second

}
Digital I/O共13,數位輸出/輸入端(pin 13作為LED指示用)
Analog Out共6,在Digital I/O中的pin 3,5,6,9,10,11 可做類比輸出使用
Analog In共6,類比輸入端pin 0~5
Tx/Rx支援Tx/Rx訊號輸入輸出(若使用時,Digital I/O pin 0,1不可
作為數位輸出入/使用)
USB傳輸與供電支援USB直接供電,以及USB接頭資料傳輸
輸入電壓可選擇USB直接供電或外部供電(建議7V~12V),用JUMP切換 (Duemilanove版本改用自動切換)
輸出電壓有5V、3.3V與Vin三種電壓輸出
支援線上燒錄功能免去燒入經片需要重複拔插晶片的痛苦
LED 13pin 13內建一個LED
p.s.
(1) 當Digital I/O不敷使用時,可用Analog In pin另外代用,宣告為pin 14~19
(2) 一般不建議使用Digital I/O pin 0,1,因為常作為Serialport傳輸用
(資料來源:http://interactive2go.blogspot.tw/2009/04/get-to-start.html)

主要就是DIGITAL(數位腳)類比(ANALOG)還有電源(POWER)
常用的就是:
數位腳D2~D13當作數位輸出(5V),也可以當成數位輸入,判斷有沒有訊號輸入。
其中有[~]符號的D3,5,6,9,10,11也可以當作類比輸出,利用PWM模擬不同的電壓輸出。
類比腳A0~A5一般用來接受類比電壓輸入,也可以當作數位腳D14~D19
註:參考http://blog.ilc.edu.tw/blog/index.php?op=printView&articleId=601903&blogId=4950

arduino sw按一下加1 sw按一下減1

//結構先用出來(0~9),再慢慢加功能
int seg7[] = { 2, 3, 4, 5, 6, 7, 8};
int table[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
int i, j;
int KeyData1, KeyData2;
int num = 0;
int delaytime = 500;
int sw1 = 9;
int sw2 = 10;
void setup()
{
  pinMode(sw1, INPUT);
  pinMode(sw2, INPUT);
  for (i = 0; i < 7; i++)
  {
    pinMode(seg7[i], OUTPUT);
  }
}
void loop()
{
  KeyData1 = digitalRead(sw1);
  KeyData2 = digitalRead(sw2);
  if (KeyData1 == LOW)
  {
    delay(20);
    while (digitalRead(sw1) == LOW);
    num++;
    if (num == 10)num = 0;
  }

  if (KeyData2 == LOW)
  {
    delay(20);
    while (digitalRead(sw2) == LOW);
    num--;
    if (num < 0)num = 9;
  }
  out(~table[num]);
  delay(delaytime);//我的板子是共陰極+npn電晶體
}
void out(byte data)
{
  for (j = 0; j < 7; j++)
  {
    if (data % 2 == 1)
      digitalWrite(seg7[j], HIGH);//共陰極,1亮
    else
      digitalWrite(seg7[j], LOW);
    data = data / 2;
  }
}

7-seg 上數_arduino

int seg7[] = { 2, 3, 4, 5, 6, 7, 8};
int table[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
int i, j;
int val=0;
int KeyData;
int num = 0;
int delaytime = 500;
int sw = 9;
void setup()
{
  pinMode(sw, INPUT);
  for (i = 0; i < 7; i++)
  {
    pinMode(seg7[i], OUTPUT);
  }
}
void loop()
{
  KeyData = digitalRead(sw);
  if (KeyData == LOW)
  {
    delay(20);
    while(digitalRead(sw)==LOW);
    num++;
  }
  if (num % 2 == 0)
  {
   val++;
    if (val > 9)
      val = 0;
  }
  else
  {
    val--;
    if (val < 0)
      val = 9;
  }
  if(val>9)val=0;
  out(~table[val]);
  delay(delaytime);//我的板子是共陰極+npn電晶體
}
void out(byte data)
{
  for (j = 0; j < 7; j++)
  {
    if (data % 2 == 1)
      digitalWrite(seg7[j], HIGH);//共陰極,1亮
    else
      digitalWrite(seg7[j], LOW);
    data = data / 2;
  }
}

LED bar graph

/*
  LED bar graph

  Turns on a series of LEDs based on the value of an analog sensor.
  This is a simple way to make a bar graph display. Though this graph uses 10
  LEDs, you can use any number by changing the LED count and the pins in the
  array.

  This method can be used to control any series of digital outputs that depends
  on an analog input.

  The circuit:
  - LEDs from pins 2 through 11 to ground

  created 4 Sep 2010
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/BarGraph
*/

// these constants won't change:
const int analogPin = A0;   // the pin that the potentiometer is attached to
const int ledCount = 10;    // the number of LEDs in the bar graph

int ledPins[] = {
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};   // an array of pin numbers to which LEDs are attached


void setup() {
  // loop over the pin array and set them all to output:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT);
  }
}

void loop() {
  // read the potentiometer:
  int sensorReading = analogRead(analogPin);
  // map the result to a range from 0 to the number of LEDs:
  int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

  // loop over the LED array:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    // if the array element's index is less than ledLevel,
    // turn the pin for this element on:
    if (thisLed < ledLevel) {
      digitalWrite(ledPins[thisLed], HIGH);
    }
    // turn off all pins higher than the ledLevel:
    else {
      digitalWrite(ledPins[thisLed], LOW);
    }
  }
}

pili燈左右

/*
  Arrays

  Demonstrates the use of an array to hold pin numbers in order to iterate over
  the pins in a sequence. Lights multiple LEDs in sequence, then in reverse.

  Unlike the For Loop tutorial, where the pins have to be contiguous, here the
  pins can be in any random order.

  The circuit:
  - LEDs from pins 2 through 7 to ground

  created 2006
  by David A. Mellis
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Array
*/

int timer = 100;           // The higher the number, the slower the timing.
int ledPins[] = {
  2, 3, 4, 5, 6, 7,8,9
};       // an array of pin numbers to which LEDs are attached
int pinCount = 8;           // the number of pins (i.e. the length of the array)

void setup()
{
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }
}

void loop()
{
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++)
  {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);

  }

  // loop from the highest pin to the lowest:
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);
  }
}

三個按鈕練習





























const int ledPin1 =  2;
const int ledPin2 =  3;
const int ledPin3 = 4;
const int buttonPin1 = 8;
const int buttonPin2 = 9;
const int buttonPin3 = 10;
int bz = 11;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

void setup()
{
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
}
void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  if (buttonState1 == LOW)
  {
    digitalWrite(ledPin1, HIGH);
    tone(bz, 100, 1000);
  }
  else if (buttonState2 == LOW)
  {
    digitalWrite(ledPin2, HIGH);
    tone(bz, 500, 1000);
  }
  else if (buttonState3 == LOW)
  {
    digitalWrite(ledPin3, HIGH);
    tone(bz, 1000, 1000);
  }
  else
  {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    noTone(bz);
  }
}

調光電路

int led=3;
int sensor_val;
int delaytime=1;
void setup()
{
  // put your setup code here, to run once:
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  // put your main code here, to run repeatedly:
  sensor_val = analogRead(0);//0~1023
  sensor_val = map(sensor_val, 0, 1023, 0, 255);
  Serial.print("sensor_val=");
  Serial.println(sensor_val);
  analogWrite(led, sensor_val);//0~255
  delay(delaytime);
}

algorithm

 #include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv)  { for(int j=2;j<=100;j++)//j...