#include <IRremote.h>
#include <Stepper.h>
#include <Servo.h>
#include <LiquidCrystal.h>
//PHOTORESISTORS
//PR1_AOpin = A1; // PR 1 & 2 are top & bottom on the panel
//PR2_AOpin = A2; // PR 3 & 4 are L & R on the panel
//PR3_AOpin = A3;
//PR4_AOpin = A4;
const int PR1_DO = 12;
const int PR2_DO = 16;
const int PR3_DO = 17;
const int PR4_DO = 18;
// BUZZER
const int buzzerPin=21;
// RGB LED
const int redpin = 9;
const int greenpin = 8;
const int bluepin = 13;
//IR receiver (THESE VALUES DEPEND ON REMOTE)
const int RECV_PIN=14; //signal pin
unsigned long up= 4244832000;
unsigned long down=1738080000;
unsigned long left = 534839040;
unsigned long right = 1871773440;
unsigned long power = 1570963200;
unsigned long stop = 501415680;
// STEPPER
const int stepsPerRev = 200; //OUR REAL MOTOR IS 2038 STEPS
Stepper myStepper(stepsPerRev,4,5,6,7); //ORDER OF PIN SETUP DIFF IRL!!
//should be 4657 IRL i think or whatever is in the arduino code file
// SERVO
const int servoPin = 2;
float servoPos = 0;
Servo myServo;
// LCD
const int rs=15, E=19, d4=3, d5=26, d6=24, d7=20;
LiquidCrystal lcd(rs, E, d4, d5, d6, d7);
// ULTRASONIC SENSOR
const int trigPin = 10;
const int echoPin = 11;
float duration, distance;
// MODE (Automatic or IR Remote)
int mode=0;
//mode 0 = start up
//mode 1 = Auto
//mode 2 = IR remote
void setup() {
// SERVO
myServo.attach(servoPin);
myServo.write(servoPos); //start as position 0
// IR REMOTE
IrReceiver.begin(RECV_PIN);
// LED
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
// Photoresistor
pinMode(A1, INPUT); //top
pinMode(A2, INPUT); //bottom
pinMode(A3, INPUT); //right
pinMode(A4, INPUT); //left
pinMode(PR1_DO, OUTPUT);
pinMode(PR2_DO, OUTPUT);
pinMode(PR3_DO, OUTPUT);
pinMode(PR4_DO, OUTPUT);
//ULTRASONIC SENSOR
//trig pin
pinMode(trigPin, OUTPUT);
//echo pin
pinMode(echoPin, INPUT);
// LCD
lcd.begin(16,2);
Serial.begin(9600);
}
void loop() {
delay(10);
int photoresist1 = analogRead(A1);
int photoresist2 = analogRead(A2);
int photoresist3 = analogRead(A3);
int photoresist4 = analogRead(A4);
//IR reciever -- reading inputs (Original code)
if (IrReceiver.decode()){ //0 if no data ready, 1 if data ready
//Serial.print("Code: ");
//Serial.println(IrReceiver.decodedIRData.decodedRawData);
//returns raw decoded value, 32 bits
IrReceiver.resume(); //ready to recieve next value
}
//****************** CHECKING/CHANGING MODE ********************
if (IrReceiver.decodedIRData.decodedRawData == power){
mode = 2; //change mode to remote if power button pressed
lcd.setCursor(0,0);
lcd.print("Mode: Remote");
}
else if(IrReceiver.decodedIRData.decodedRawData == stop){ //have to press menu button to "escape" remote mode
mode = 1; //automatic
lcd.setCursor(0,0);
lcd.print("Mode: Auto ");
}
if (mode==0){
lcd.setCursor(0,0);
lcd.print("Set a mode");
}
//Serial.print("Mode: ");
//Serial.println(mode);
//******************* IR REMOTE MODE **************************
if (mode == 2){
//--------- U & D --> STEPPER (ontop of base plate, attatched to solar panel)
if (IrReceiver.decodedIRData.decodedRawData == up){
myStepper.setSpeed(30);
myStepper.step(50);
Serial.println("right");
}
if (IrReceiver.decodedIRData.decodedRawData == down){
myStepper.setSpeed(30);
myStepper.step(-50);
Serial.println("left");
}
// -------- L & R --> SERVO (on bottom of base plate)
if (IrReceiver.decodedIRData.decodedRawData == right){
if (servoPos<180){
myServo.write(servoPos);
servoPos+=5;
delay(5);
}
}
if (IrReceiver.decodedIRData.decodedRawData == left){
if (servoPos>0){
myServo.write(servoPos);
servoPos-=5;
delay(5);
}
}
}
//***************** AUTO MODE *********************
else if (mode==1){
Serial.println("auto mode");
if (abs(photoresist1 - photoresist2) > 20){
Serial.println("stepper");
if (photoresist1 < photoresist2){ //PR1 has more light so move towards it
// 1 & 2 controlled by STEPPER up & down
myStepper.setSpeed(30); //move whichever direction pr1 is
myStepper.step(-50);
delay(15);
}
else if(photoresist2 < photoresist1){ //move towards PR2
myStepper.setSpeed(30); //move whichever direction pr2 is
myStepper.step(50);
delay(15);
}
}
if (abs(photoresist3 - photoresist4) > 20){
if (photoresist4 < photoresist3){ // PR3 has more light so move towards it
// 3 & 4 controlled by SERVO l & r
if (servoPos<180){ //right
servoPos+=5;
myServo.write(servoPos);
delay(15);
}
}
else if(photoresist3 < photoresist4) {
if (servoPos>0){ //left
servoPos-=5;
myServo.write(servoPos);
delay(15);
}
}
}
}
//**************** RGB LED *************************
if (photoresist1 < 169){ //perf - fully green
setColor(0,255,0);
}
else if (photoresist1 > 511){ // poor - fully red
setColor(255,0,0);
}
else{ // med - yellow
setColor(255,255,0);
}
//************* ULTRASONIC SENSOR ********************
//trig pin starts on low for 2 us
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
//then we set it to high for 10 us, so that it will then send a burst
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
//now we are awaiting a pulse back, so we can read the duration
duration = pulseIn(echoPin, HIGH); //reads how long echo is high aka how long until the pulse returns
//vsound = 0.034cm/μs
//calculate distance
float v = 0.034;
distance = (duration * v)/2; //distance is in CM!!!
if (distance < 10){ //something comes 10 cm near the solar panel
digitalWrite(buzzerPin, HIGH); //play sound for 2s, then shut off
lcd.setCursor(13,1);
lcd.print("****");
delay(3000);
lcd.setCursor(13,1);
lcd.print(" ");
digitalWrite(buzzerPin, LOW);
//Serial.println("******obstruction*******");
}
lcd.setCursor(0,1);
lcd.print("Voltage:");
float voltage = (photoresist1*5.0)/1024;
lcd.setCursor(8,1);
lcd.print(voltage);
delay(100);
}
//************* RGB LED Function to Set a Color ***********
//analogWrite can be 0 to 225 (0=low, 225 = high)
//a function to set the analog value of the PWM pins so we get a diff color
void setColor(int redval, int greenval, int blueval){
analogWrite(redpin, redval);
analogWrite(greenpin, greenval);
analogWrite(bluepin, blueval);
}