Payton Ball
Created April 20, 2016 © GPL3+

Servo Motor Controlled Light Switch

Programmed a servo motor to remotely turn the light to my room on/off. Included a photocell that tells me if it is on or off as well.

IntermediateProtip4 hours907
Servo Motor Controlled Light Switch

Things used in this project

Hardware components

Photon
Particle Photon
×1
Servos (Tower Pro MG996R)
×1
Photo resistor
Photo resistor
×1

Story

Read more

Code

Tinker

Arduino
Tinker is already uploaded into the photon. I messed around with what was already in place to create the results I wanted
/* Function prototypes -------------------------------------------------------*/
int tinkerDigitalRead(String pin);
int tinkerDigitalWrite(String command);
int tinkerAnalogRead(String pin);
int tinkerAnalogWrite(String command);


int pos = 90;
Servo myservo;
int photocell = A0;
int power = A4;
Servo myphotocell;


/* This function is called once at start up ----------------------------------*/
void setup()
{
   // This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.
	//Register all the Tinker functions
	Spark.function("digitalread", tinkerDigitalRead);
	Spark.function("digitalwrite", tinkerDigitalWrite);
	Spark.function("analogread", tinkerAnalogRead);
	Spark.function("analogwrite", tinkerAnalogWrite);
    myservo.attach(A5);
    myservo.write(pos);
}

/* This function loops forever --------------------------------------------*/
void loop()
{
 
}

/*******************************************************************************
 * Function Name  : tinkerDigitalRead
 * Description    : Reads the digital value of a given pin
 * Input          : Pin
 * Output         : None.
 * Return         : Value of the pin (0 or 1) in INT type
                    Returns a negative number on failure
 *******************************************************************************/
int tinkerDigitalRead(String pin)
{
	//convert ascii to integer
	int pinNumber = pin.charAt(1) - '0';
	//Sanity check to see if the pin numbers are within limits
	if (pinNumber< 0 || pinNumber >7) return -1;

	if(pin.startsWith("D"))
	{
		pinMode(pinNumber, INPUT_PULLDOWN);
		return digitalRead(pinNumber);
	}
	else if (pin.startsWith("A"))
	{
		pinMode(pinNumber+10, INPUT_PULLDOWN);
		return digitalRead(pinNumber+10);
	}
	return -2;
}

/*******************************************************************************
 * Function Name  : tinkerDigitalWrite
 * Description    : Sets the specified pin HIGH or LOW
 * Input          : Pin and value
 * Output         : None.
 * Return         : 1 on success and a negative number on failure
 *******************************************************************************/
int tinkerDigitalWrite(String command)
{
int value = 0;
	//convert ascii to integer
	int pinNumber = command.charAt(1) - '0';
	//Sanity check to see if the pin numbers are within limits
	if (pinNumber< 0 || pinNumber >7) return -1;

	if(command.substring(3,7) == "HIGH") value = 25;
	if(command.substring(3,6) == "LOW") value =120;
   
    pos = value;
	myservo.write(pos);
/*
	if(command.startsWith("D"))
	{
		pinMode(A5, OUTPUT);
		digitalWrite(A5, 45);
		return 1;
	}
	else if(command.startsWith("A"))
	{
		pinMode(A5, OUTPUT);
		digitalWrite(A5, 135);
		return 1;
	}
*/
	 return -3;
	
	
}

/*******************************************************************************
 * Function Name  : tinkerAnalogRead
 * Description    : Reads the analog value of a pin
 * Input          : Pin
 * Output         : None.
 * Return         : Returns the analog value in INT type (0 to 4095)
                    Returns a negative number on failure
 *******************************************************************************/
int tinkerAnalogRead(String pin)
{
	//convert ascii to integer
	int pinNumber = pin.charAt(1) - '0';
	//Sanity check to see if the pin numbers are within limits
	

	if (photocell > 5000);
	myphotocell.write(photocell);
	return -1;
	if (photocell < 5000);
	myphotocell.write(photocell);
	return 1;
	/* if(pin.startsWith("D"))
	{ 
		return -3;
	}
	else if (pin.startsWith("A"))
	{
		return analogRead(pinNumber+10);
	}
	*/
	

}

/*******************************************************************************
 * Function Name  : tinkerAnalogWrite
 * Description    : Writes an analog value (PWM) to the specified pin
 * Input          : Pin and Value (0 to 255)
 * Output         : None.
 * Return         : 1 on success and a negative number on failure
 *******************************************************************************/
int tinkerAnalogWrite(String command)
{
	//convert ascii to integer
	int pinNumber = command.charAt(1) - '35';
	//Sanity check to see if the pin numbers are within limits
	if (pinNumber< 0 || pinNumber >7) return -1;

	String value = command.substring(3);

	if(command.startsWith("D"))
	{
		pinMode(pinNumber, OUTPUT);
		analogWrite(pinNumber, value.toInt());
		return 1;
	}
	else if(command.startsWith("A"))
	{
		pinMode(pinNumber+10, OUTPUT);
		analogWrite(pinNumber+10, value.toInt());
		return 1;
	}
	else return -2;
}

Credits

Payton Ball

Payton Ball

1 project • 3 followers
UNCC Mechanical Engineering Student
Thanks to Professor McAlpine and Particle.

Comments