Saturday, 9 September 2017

Controlling motors with multiple light sensors

Well this post is just to share the sketch and I won't explain in detail. If you want to know why is that then you can read more on all Arduino posts page. Those people coming from the YouTube, you probably already know everything and you just want the sketch, so here it is.
The sketch
int photocellPin1 = 1;     // the cell and 10K pulldown are connected to a1
    int photocellReading1;     // the analog reading from the sensor divider
    int motor1 = 2;          // connect motor to pin D2

    int photocellPin2 = 2;  
    int photocellReading2;
    int motor2 = 3;

    void setup() {
      pinMode(motor1, OUTPUT);
      pinMode(motor2, OUTPUT);
      Serial.begin(9600);
    }
  
    void pca() {
      photocellReading1 = analogRead(photocellPin1);    
   
if (photocellReading1 <=500){
    digitalWrite(motor1, HIGH);
  }
  else {
    digitalWrite(motor1, LOW);
  }
}

    void pcb() {
      photocellReading2 = analogRead(photocellPin2);          
if (photocellReading2 <=500){
    digitalWrite(motor2, HIGH);
  }
  else {
    digitalWrite(motor2, LOW);
  }
}

void loop() {
pca();
pcb();
delay(50);
}

No comments:

Post a Comment