Skip to content

Instantly share code, notes, and snippets.

@jakabo27
Created September 16, 2020 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakabo27/f816a512fde69a3207555c16b539d398 to your computer and use it in GitHub Desktop.
Save jakabo27/f816a512fde69a3207555c16b539d398 to your computer and use it in GitHub Desktop.
Architectural Robotics Project 1 for ECE 8680 at Clemson Fall 2020
/*
Name: ArchRob_Project1.ino
Created: 9/5/2020 12:10:28 PM
Author: JACOBT-DESKTOP\Jacob
*/
#include <Servo.h>
#include <Stepper.h>
const int STEPS = 32;
const int StepsPerRotation = 2048; // 2048 steps to 360° of total rotation of motor
// Stepper motor pins
const int Step1IN1 = 8;
const int Step1IN2 = 10;
const int Step1IN3 = 9;
const int Step1IN4 = 11;
Stepper stepper(STEPS, Step1IN1, Step1IN2, Step1IN3, Step1IN4);
//NOTE: Max range of 7600 steps. Start at 0 and go to -7600 and go back
int StepsFrom0 = 0; //Keep track of how far we are from the 0 position for easy resetting
//Servo Motors
const int SERVO1PIN = 5;
const int SERVO2PIN = 6;
Servo servo1;
Servo servo2;
//Sensors
const int PHOTO1PIN = A1; //Photoresistor
const int FORCEPIN = A4; //Flex Pressure sensor
const int LimitSwitch1 = 4;
const int NEXTMODEBUTTON = 3;
void setup()
{
Serial.begin(115200);
pinMode(NEXTMODEBUTTON, INPUT);
stepper.setSpeed(1000);
servo1.attach(SERVO1PIN);
servo2.attach(SERVO2PIN);
//Reset servo positions
servo1.write(0);
servo2.write(30);
delay(100);
//Home the board
SpinHouse(10);
//Demo
/*servo1.write(30);
servo2.write(40);
SpinHouse(1);
delay(2000);
servo1.write(0);
servo2.write(0);
SpinHouse(2);
delay(1000);
SpinHouse(10);*/
}
void loop()
{
//Overall states
/*
1. Start up at 5:00pm, once inside rotate kitchen door from garage to back yard
2. 8:00pm, sun is down, make bedrooms bigger
3. 6:00am, sun detected, rotate parents bedroom towards sun (kids away for sleeping in)
4. People detected in Kitchen - Rotate kitchen door towards cars, make parents bedroom smaller
5. 10:00am, people detected in kitchen again (kids are up), make kid bedroom smaller.
So need 2 photoresistors, pressure sensor in kitchen,
*/
//Initial state
//Exit door is at Garage
//Kitchen is large, bedrooms small
delay(10);
int currentMode = 1;
const int ForceThreshold = 15; //Force Sensor reads 0 if nothing on it, 100+ if pressure
const int PhotoThreshold = 6; //Photoresistor reads 10-12 if covered, 14+ if ambient room light or brighter
const int LargeBedroom1Pos = 40; //Need to tune - position for having bedroom large and kitchen small
const int LargeBedroom2Pos = 60;
Serial.println("Running mode 1");
//1. At 5:00pm, people get home from work and enter through the kitchen. Once inside, rotate towards back yard
while (digitalRead(NEXTMODEBUTTON) == 0)
{
while (analogRead(FORCEPIN) <= ForceThreshold)
{
delay(100);
}
//Person is on sensor
delay(500);
//Rotate exit door to back yard
SpinHouse(1);
servo1.write(0);
//Wait until button is pressed to move onto next mode
while (digitalRead(NEXTMODEBUTTON) == 0) delay(100);
}
Serial.println("Running mode 2");
delay(1000); //Time to release pushbutton
//2. When the sun goes down, make the bedrooms bigger
while (digitalRead(NEXTMODEBUTTON) == 0)
{
while (analogRead(PHOTO1PIN) >= PhotoThreshold)
{
delay(100);
}
//Sensor is covered == sun went down
//Make bedrooms bigger
servo1.write(LargeBedroom1Pos);
servo2.write(0);
delay(400);
//Wait until button is pressed to move onto next mode
while (digitalRead(NEXTMODEBUTTON) == 0) delay(100);
}
Serial.println("Running mode 3");
delay(1000); //Time to release pushbutton
//3. When the sun comes up, rotate parents bedroom toward sun
while (digitalRead(NEXTMODEBUTTON) == 0)
{
while (analogRead(PHOTO1PIN) <= PhotoThreshold) //while it is dark, sensor covered
{
delay(100);
}
//Sensor is uncovered = sun came up
//Rotate parents bedroom toward sun for natural wakeup
SpinHouse(2);
//Wait until button is pressed to move onto next mode
while (digitalRead(NEXTMODEBUTTON) == 0) delay(100);
}
Serial.println("Running mode 4");
delay(1000); //Time to release pushbutton
//4. Parents detected in the kitchen, rotate exit door towards cars
while (digitalRead(NEXTMODEBUTTON) == 0)
{
while (analogRead(FORCEPIN) <= ForceThreshold)
{
delay(100);
}
//Sensor is pressed - person in kitchen
//Rotate exit door to cars
SpinHouse(0);
delay(400);
//Make kitchen bigger, bedroom 1 smaller
servo1.write(0);
//Wait until button is pressed to move onto next mode
while (digitalRead(NEXTMODEBUTTON) == 0) delay(100);
}
Serial.println("Running mode 5");
delay(1000); //Time to release pushbutton
//5. Kids detected in the kitchen - make their bedroom small again
while (digitalRead(NEXTMODEBUTTON) == 0)
{
while (analogRead(FORCEPIN) <= ForceThreshold)
{
delay(100);
}
//Sensor is pressed - person in kitchen
//Make kids bedroom small
servo2.write(LargeBedroom2Pos);
//Wait until button is pressed to move onto next mode
while (digitalRead(NEXTMODEBUTTON) == 0) delay(100);
}
delay(1000); //Time to release pushbutton
}
void SpinHouse(int location)
{
/*
1. Kitchen door at Garage (0)
2. Kitchen door at Back Yard (-5000 temp)
3. Show off?
10. Reset to 0 without limit switch
20. Reset to 0 with limit switch
*/
int GarageSteps = -5000;
int BackYardSteps = -3000;
switch (location) {
case 0:
MoveStepperTo(0);
break;
case 1: //Back yard
MoveStepperTo(BackYardSteps);
break;
case 2: //Parents bedroom toward morning sun
MoveStepperTo(GarageSteps);
break;
case 10:
//Home the stepper with the limit switch
while (digitalRead(LimitSwitch1) == 0)
{
stepper.step(100);
//delay(10);
}
delay(50);
stepper.step(-300);
StepsFrom0 = 0;
break;
}
}
void MoveStepperTo(int thisStep)
{
int numStepsToMove = thisStep - StepsFrom0;
StepsFrom0 = thisStep;
if (numStepsToMove <= 7600 && numStepsToMove >= -7600)
{
//For now be simple and just move there without checking the limit switch - trust yoself!
stepper.step(numStepsToMove);
}
}
//didn't end up using this in this project
void CheckInputs()
{
if (Serial.available() > 0)
{
//Do something with serial inputs
//val = Serial.parseInt();
//stepper.step(val * 1024);
//Serial.println(val); //for debugging
}
}
/*
Overall structure
When the sun goes down, move the walls so the bedrooms get bigger
(When the photoresistors both sense it's dark, move servos)
Then when one detects the sun is coming up, rotate towards it
(When state is
When "end" button pressed, reset motors to initial positions
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment