Arduino_Scuola
Published © CC BY-NC-SA

A Simple Light Follower

This lesson aims to show how to make a simple light follower made of cardboard using a microservo.

IntermediateFull instructions provided40 minutes13,305
A Simple Light Follower

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
mini breadboard - mixed colours
×1
10 jumper wires 200mm male
×1
5 LDR
×1
10 10k 1/4w resistor
×1
Analog 180° Micro Servo
×1
4mm cardboard
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

LightFollower DXF

Schematics

Layout

Code

Light Follower code

Arduino
/*
A simple light follower
 
author: Arturo Guadalupi <a.guadalupi@arduino.cc>
 */
#include <Servo.h>
Servo myservo;
int up_average;
int down_average;
int A0_val;
int A1_val;
int A2_val;
int A3_val;
int val = 90;
int diff;
const int threshold = 50;
void setup() {
  pinMode(A0, INPUT); 
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  myservo.attach(11);
  myservo.write(val);
}
void loop() {
  A0_val = analogRead(A0); //reading data from sensors
  A1_val = analogRead(A1); 
  A2_val = analogRead(A2);
  A3_val = analogRead(A3);
  
  down_average = (A0_val + A1_val)/2; //the sum of the two lower sensors /2
  up_average = (A2_val + A3_val)/2;	  //the sum of the two upper sensors /2
  diff = abs(up_average-down_average); //checking the difference between the two averages
  if ((up_average > down_average) && (diff > threshold))
    {
     if (val < 180) //if different from max val
       {
       val++;
       myservo.write(val);
       }
    }
  if((down_average > up_average) && (diff > threshold))
    {
    if (val > 0) //if different from min val
      {
      val--;
      myservo.write(val);
      }
    }
  delay(25);
}

Credits

a_guadalupi

Posted by Arduino_Scuola

Comments