שיעור 16 - רמזור ומחסום

ברוכים הבאים לשיעור!

השיעור מורכב מסדרה של סרטונים, צפו בהם לפי הסדר.

תהנו 🙂

חלק 1: רמזור

//Green -> A0
//Yellow -> A1
//Red -> A2

void setup() {

  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);
  pinMode(A2,OUTPUT);

  // Blink code here
  //Turn on
  digitalWrite(A0,HIGH);
  digitalWrite(A1,HIGH);
  digitalWrite(A2,HIGH);

  //wait for one second
  
 
  //Turn off


  //wait another second


  //Turn on red
  digitalWrite(A2,HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:

} 

פתרון:

//Green -> A0
//Yellow -> A1
//Red -> A2

void setup() {

  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);
  pinMode(A2,OUTPUT);

  // Blink code here
  //Turn on
  digitalWrite(A0,HIGH);
  digitalWrite(A1,HIGH);
  digitalWrite(A2,HIGH);

  //wait for one second
  delay(1000);
 
  //Turn off
  digitalWrite(A0,LOW);
  digitalWrite(A1,LOW);
  digitalWrite(A2,LOW);

  //wait another second
  delay(1000);

  //Turn on red
  digitalWrite(A2,HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:

} 

חלק 2: מחסום

//Green -> A0
//Yellow -> A1
//Red -> A2

// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin = 9;
// Create a servo object
Servo Servo1;


void setup() {

   // We need to attach the servo to the used pin number
   Servo1.attach(servoPin);
 
  // put your setup code here, to run once:

  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);
  pinMode(A2,OUTPUT);

  // Blink code here"
  //Turn on
  digitalWrite(A0,HIGH);
  digitalWrite(A1,HIGH);
  digitalWrite(A2,HIGH);

  //wait for one second
  delay(1000);
 

  //Turn off
  digitalWrite(A0,LOW);
  digitalWrite(A1,LOW);
  digitalWrite(A2,LOW);

  //wait another second
  delay(1000);

  //Turn on red
  digitalWrite(A2,HIGH);

  //Slow open
  for(int i=175;i>80;i--)
  {
    Servo1.write(i);
    delay(50);
  }

}

void loop() {
  // put your main code here, to run repeatedly:

} 

חלק 3: חיבור השלט

//Green -> A0
//Yellow -> A1
//Red -> A2

// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin = 9;
// Create a servo object
Servo Servo1;

// Radio config
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00003";


void setup() {
  //Radio
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  Serial.println("Listening");

  //Servo Config
  // We need to attach the servo to the used pin number
  Servo1.attach(servoPin);
 
  //Config leds pin
  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);
  pinMode(A2,OUTPUT);

  // Blink Begin
  //Turn on
  digitalWrite(A0,HIGH);
  digitalWrite(A1,HIGH);
  digitalWrite(A2,HIGH);

  //wait for one second
  delay(1000);

  //Turn off
  digitalWrite(A0,LOW);
  digitalWrite(A1,LOW);
  digitalWrite(A2,LOW);

  //Blink END

  //wait another second
  delay(1000);

  //Turn on red
  digitalWrite(A2,HIGH);

  //Close the gate
  for(int i=80;i<175;i++)
  {
    Servo1.write(i);
    delay(50);
  }
}

void loop()
{
  // put your main code here, to run repeatedly:
    if (radio.available())
    {
      char text[32] = "";
      radio.read(&text, sizeof(text));
      Serial.println(text);
      if (text[0] == 'S')
      {
        //Slow open
        for(int i=175;i>80;i--)
        {
          Servo1.write(i);
          delay(50);
        }
      }
      if (text[0] == 'F')
      {
        //Slow open
        for(int i=80;i<175;i++)
        {
          Servo1.write(i);
          delay(50);
        }
      }

     
    }
}