วันอาทิตย์ที่ 27 ตุลาคม พ.ศ. 2562

วงจรระบบสัญญาณไฟจราจรที่มีความหนาแน่น

  วงจรระบบสัญญาณไฟจราจรที่มีความหนาแน่น


ส่วนประกอบวงจร:

  • ตัวควบคุม ATmega8
  • บอร์ด PCB
  • เซ็นเซอร์ IR -4
  • ไฟ LED-12 (4 สีแดง 4 สีเขียว, สี 4 สีเหลือง)
  • 12 โวลต์แบตเตอรี่หรืออะแดปเตอร์
  • สายเคเบิลอนุกรม
  • สายเชื่อมต่อ
  code
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>

#define R1 PB0
#define Y1 PB1
#define G1 PB2

#define R2 PB3
#define Y2 PB4
#define G2 PB5

#define R3 PD5
#define Y3 PD4
#define G3 PD3

#define R4 PD2
#define Y4 PD1
#define G4 PD0

int main(void)
{
    DDRB = 0xff;
    DDRD = 0xff;
    DDRC = 0x00;
   
    PORTB = 0x00;
    PORTD = 0x00;
   
    while(1)
    {
        if((PINC&0x01) == 0x01)
        {
        PORTB |= (1<<G1);
        PORTB |= (1<<Y2);
        PORTD |= (1<<R3);
        PORTD |= (1<<R4);
       
        }
        else if((PINC&0x02) == 0x02)
        {
        PORTB |= (1<<R1);
        PORTB |= (1<<G2);
        PORTD |= (1<<Y3);
        PORTD |= (1<<R4);   
       
        }
       
        else if((PINC&0x04) == 0x04)
        {
        PORTB |= (1<<R1);
        PORTB |= (1<<R2);
        PORTD |= (1<<G3);
        PORTD |= (1<<Y4);
       
        }
       
        else if((PINC&0x08) == 0x08)
        {
        PORTB |= (1<<Y1);
        PORTB |= (1<<R2);
        PORTD |= (1<<R3);
        PORTD |= (1<<G4);
       
        }
       
        else
        {
            PORTB = 0x00;
        PORTD = 0x00;
           
        PORTB |= (1<<G1);
        PORTB |= (1<<Y2);
        PORTD |= (1<<R3);
        PORTD |= (1<<R4);
        _delay_ms(7000);
       
        PORTB = 0x00;
        PORTD = 0x00;
       
        PORTB |= (1<<R1);
        PORTB |= (1<<G2);
        PORTD |= (1<<Y3);
        PORTD |= (1<<R4);
        _delay_ms(7000);
       
        PORTB = 0x00;
        PORTD = 0x00;
       
        PORTB |= (1<<R1);
        PORTB |= (1<<R2);
        PORTD |= (1<<G3);
        PORTD |= (1<<Y4);
        _delay_ms(7000);
       
        PORTB = 0x00;
        PORTD = 0x00;
        PORTB |= (1<<Y1);
        PORTB |= (1<<R2);
        PORTD |= (1<<R3);
        PORTD |= (1<<G4);
        _delay_ms(7000);
        PORTB = 0x00;
        PORTD = 0x00;
        }       
    }
}

วันพุธที่ 2 ตุลาคม พ.ศ. 2562

รถดั๊มตรวจจับสี

รถดั๊มตรวจจับสี

อุปกรณ์
1.รถดั้มสิบล้อ
2.servo motor
3.color sensor
4.กล่องกระดาษสีแดง สีเขียว สีน้ำเงิน
5.ลูกแก้ว

#include <Servo.h>
#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define Out 6
Servo topServo;
int frequency = 0;
int color=0;
void setup() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(Out, INPUT);
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
  topServo.attach(7);
  Serial.begin(9600);
}
void loop() {
  color = readColor();
  delay(10);
  switch (color) {
    case 1:
    topServo.write(0);
    delay(5000);
    topServo.write(180);
    }
  color=0;
}
int readColor() {
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  frequency = pulseIn(Out, LOW);
  int R = frequency;
  Serial.print("R= ");
  Serial.print(frequency);
  Serial.print("  ");
  delay(50);
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  frequency = pulseIn(Out, LOW);
  int G = frequency;
  Serial.print("G= ");
  Serial.print(frequency);
  Serial.print("  ");
  delay(500);
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  frequency = pulseIn(Out, LOW);
  int B = frequency;
  Serial.print("B= ");
  Serial.print(frequency);
  Serial.println("  ");
  delay(50);
  if(R<35 & R>25 & G<104 & G>94){
    color = 1; // Red
  }
  return color;
}

หลักการทำงาน
   เมื่อ color sensor ตรวจสีจับสีที่กำหนดไว้ได้ servor motor จะทำงาน