Project tutorial
Serial Arduino (Control the PWM)

Serial Arduino (Control the PWM) © GPL3+

Using your computer to control the PWM value in your Arduino.

  • 3,194 views
  • 1 comment
  • 4 respects

Components and supplies

Apps and online services

About this project

What is the code: Serial?

Serial is the code allows you to manage the serial of your Arduino using Serial Monitor. This code has been written:

Begin Code:

Serial.begin(baud rate);

Family:

Serial.read;

Serial.write;

Serial.print;

Does Serial Monitor have a hot key?

Yes, the hot key of Serial Monitor is: Ctrl + Shift + M.

Connection

Short leg + GND

Long leg + resistor

Resistor + 11 (Supported PWM)

Code

Untitled fileArduino
byte brightness = 0;
const int LED = 11;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println("enter brightness %");
  while(Serial.available()==0)
  {
    // không làm gì cả
  }
  while(Serial.available()>0)
  {
    char ch = Serial.read();
    if(ch >= '0' && ch <='255')
    {
    //chuyển từ ASCII sang số thập phân
    brightness = brightness*10 + ch - '0';
    delay(5);//chờ tín hiệu tiếp theo được truyền qua cổng serial
             //KHÔNG THỂ THIẾU. Hãy thử bỏ dòng này bạn sẽ thấy điều khác biệt!
    }
    else 
    {
    Serial.print(ch);
    Serial.println(":not valid");
    brightness = 0;
    }
  }
  // chặn giá trị brightness trong khoảng [0,100]
  brightness = constrain(brightness,0,100);
  // in ra giá trị brightness
  Serial.print("brightness = ");
  Serial.println(brightness);
  // chuyển đổi từ giá trị brightness sang PWM
  byte value = map(brightness,0,100,0,255);
  // bật LED với giá trị xung PWM (duty cycle) nhận được
  analogWrite(LED,value);
  // reset biến brightness về giá trị 0 
  //để tiếp tục tính toán
  brightness = 0;
}

Comments

Similar projects you might like

Control an LED with the Remote Control

Project showcase by Nicholas_N

  • 3,195 views
  • 2 comments
  • 9 respects

Control your LED with Cortana

Project tutorial by Mohamed Emam

  • 8,956 views
  • 7 comments
  • 21 respects

Servo Control with TV Remote Control

Project showcase by eldo85

  • 5,595 views
  • 5 comments
  • 17 respects

Health Kit: Humidity and Temperature Control

Project tutorial by Diana Khalipina

  • 3,415 views
  • 2 comments
  • 32 respects

Security System And Access Control With Arduino And RFID

Project tutorial by Sarath P

  • 7,287 views
  • 0 comments
  • 18 respects
Add projectSign up / Login