FIRST CODE:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
}
void loop()
{
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
lcd.setCursor(0,1);
lcd.print("Voltage ");
lcd.print(voltage);
lcd.setCursor(0,0);
lcd.print("Time ");
lcd.print(millis()/1000);
delay(1000);
}
SECOND CODE:
//Motor Control Direction Values
// Direction A (left motor) tells motor A which way to rotate, LeftMotorPower sends the power level for the motor
// Power is between 0-255 (one byte) 0 is off, 255 is max
//CHANGE MOTOR POWER
int LeftMotorDir = 12, LeftMotorPower = 3;
int RightMotorDir = 13, RightMotorPower = 11;
// Decleration for two analog input light sensors
int left_light = A0, right_light = A1;
// Light Sensor Calibration Values
// These you need to set manually (use the LCD to read the values). Make sure to give yourself a buffer
// If line reads 999, may use something like ~925 for your dark value (so you don't miss it).
//CHANGE LIGHT SENSOR CALIBRATION VALUES
int dark = 870;
// Motor maximum and minimum speeds
// Adjust these within the 0-255 range to try and make your bot faster
//CHANGE MOTOR MIN AND MAX SPEEDS
int motor_max = 255, motor_min = 0;
//DON"T CHANGE ANYTHING BELOW THIS LEVEL
int ms_per_cycle = 10; // Milliseconds per cycle -> 10ms would yield loop speed of 100Hz
unsigned long previous_time = 0; // This is updated each time the motors are updated to make sure everything runs exactly at desired loop speed
void setup() {
pinMode(LeftMotorDir, OUTPUT);
pinMode(RightMotorDir, OUTPUT);
pinMode(LeftMotorPower, OUTPUT);
pinMode(RightMotorPower, OUTPUT);
}
void loop() {
if (millis() - previous_time >= ms_per_cycle) // if time since last motor update is >= desired delta t then update motors
{
previous_time = millis(); // Update the cycle timer
int right_reading = analogRead(right_light); // Read from the two light sensors
int left_reading = analogRead(left_light);
if (right_reading >= dark && left_reading < dark) // See the line on the right side of the robot.
{ // Need to turn the robot right to keep the line over the robot.
digitalWrite(LeftMotorDir, HIGH); // Left motor forward
analogWrite(LeftMotorPower, motor_min); // Max power for left motor
digitalWrite(RightMotorDir, HIGH); // Right motor forward
analogWrite(RightMotorPower, motor_max); // Minimum power for right motor
}
else if (right_reading < dark && left_reading >= dark) // See line on the left side of the robot
{
digitalWrite(LeftMotorDir, HIGH); // Left motor forward
analogWrite(LeftMotorPower, motor_max); // Min power for left motor
digitalWrite(RightMotorDir, HIGH); // Right motor forward
analogWrite(RightMotorPower, motor_min); // Max power for right motor
}
else if (right_reading < dark && left_reading < dark) // Neither sensor sees line, must be straddled... Go straight
{
digitalWrite(LeftMotorDir, HIGH); // Left motor forward
analogWrite(LeftMotorPower, motor_max); // Max power for left motor
digitalWrite(RightMotorDir, HIGH); // Right motor forward
analogWrite(RightMotorPower, motor_max); // Max power for right motor
}
else
{
// Both sensors must see black or there is a problem. Turning motors off
digitalWrite(LeftMotorDir, HIGH); // Left motor forward
analogWrite(LeftMotorPower, 0); // Turning motor off
digitalWrite(RightMotorDir, HIGH); // Right motor forward
analogWrite(RightMotorPower, 0); // Turning motor off
}
} // End of cycle timer if statement
// If the if statement is false (hasn't been enough time since last motor update) then the program will simply skip the if statement
// and loop around very very quickly.
}