Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
F. Yao
Created November 17, 2016 © MIT

DESKTOP GREENHOUSE CONTROL HUB

Desktop Greenhouse can make pleasant scene and fresh moisture air. But it will take extra effort to irrigation and monitoring for better gro

BeginnerShowcase (no instructions)2 hours123
DESKTOP GREENHOUSE CONTROL HUB

Things used in this project

Hardware components

PSoC Analog Coprocessor Pioneer Kit
Cypress PSoC Analog Coprocessor Pioneer Kit
×1

Software apps and online services

PSoC Creator
Cypress PSoC Creator

Hand tools and fabrication machines

LED array

Story

Read more

Schematics

Design Principle Files

Design Principle Files

Code

main.c

C/C++
main.c
/******************************************************************************
* Project Name      : GreenPlant Control Hub
* Version           : 1.0
* Device Used       : CY8C4A45LQI-L483
* Software Used     : PSoC Creator 3.3 CP3
* Compiler Used     : ARM GCC 4.9.3 
* Related Hardware  : CY8CKIT-048 PSoC Analog Coprocessor Pioneer Kit 
*******************************************************************************
* This project is demo project for grenn plant control hub, modifiey from cypress sample
* Project. This project is for leaning and education purpose only. 11/24/2016
* Copyright claim from Cypress is attached as follows 
*******************************************************************************

*******************************************************************************
* Project Name      : CE211301_PIR_Motion_Sensing
* Version           : 1.0
* Device Used       : CY8C4A45LQI-L483
* Software Used     : PSoC Creator 3.3 CP3
* Compiler Used     : ARM GCC 4.9.3 
* Related Hardware  : CY8CKIT-048 PSoC Analog Coprocessor Pioneer Kit 
*******************************************************************************
* Copyright (2016), Cypress Semiconductor Corporation.
*******************************************************************************
* This software, including source code, documentation and related materials
* ("Software") is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and 
* foreign), United States copyright laws and international treaty provisions. 
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the 
* Cypress source code and derivative works for the sole purpose of creating 
* custom software in support of licensee product, such licensee product to be
* used only in conjunction with Cypress's integrated circuit as specified in the
* applicable agreement. Any reproduction, modification, translation, compilation,
* or representation of this Software except as specified above is prohibited 
* without the express written permission of Cypress.
* 
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND, 
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED 
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* Cypress reserves the right to make changes to the Software without notice. 
* Cypress does not assume any liability arising out of the application or use
* of Software or any product or circuit described in the Software. Cypress does
* not authorize its products for use as critical components in any products 
* where a malfunction or failure may reasonably be expected to result in 
* significant injury or death ("ACTIVE Risk Product"). By including Cypress's 
* product in a ACTIVE Risk Product, the manufacturer of such system or application
* assumes all risk of such use and in doing so indemnifies Cypress against all
* liability. Use of this Software may be limited by and subject to the applicable
* Cypress software license agreement.
*******************************************************************************/
/*******************************************************************************
* Theory of Operation: This example demonstrates how to implement an analog front end 
* (AFE) for a Pyroelectric Infrared (PIR) motion sensor using PSoC Analog Coprocessor.
* The code example is designed to measure the voltage signal from a PIR sensor to detect 
* the movement of IR emitting object. The sensor data is communicated over I2C. 
* Also, RGB LED is also turned ON once the motion is detected.
*******************************************************************************/

/* Header File Includes */
#include <project.h>

#define PUMP_ON                      (0u)
#define PUMP_OFF                     (1u)
#define DRY_DETECTED             (1u)
#define DRY_NOT_DETECTED         (0u)
#define MOIST_DRY                  (3u)
#define ADC_CHANNEL_GHC             (0u)
#define SENSOR_RAW_INITIAL          (0)
/* EzI2C Read/Write Boundary */
#define READ_WRITE_BOUNDARY         (1u)

/* High Threshold for soil moist detection (80% of positive peak count) */
#define MOIST_HIGH         (1200)
/* Low Threshold for soil moist  detection (80% of negative peak count) */
#define MOIST_LOW          (-1200)   

	
/* Structure that holds the sensor values                                        */
/* Use __attribute__((packed)) for GCC and MDK compilers to pack structures      */
/* For other compilers use the corresponding directive.                          */
/* For example, for IAR use the following directive                              */
/* typedef __packed struct {..}struct_name;                                      */
typedef struct __attribute__((packed))
{
    uint8 detectionMoist;      	/* PIR detection distance */
	int16 sensorRawValue;        	/* ADC result */
	int16 highThreshold;		    /* High threshold for motion detection */
	int16 lowThreshold;			    /* Low threshold for motion detection */
    uint8 moistDetected;		    /* Motion detection flag */
}ghc_sensor_data;


/* Function Prototypes */
void InitResources(void );
CY_ISR(TIMEBASE_ISR);

/* Declare the i2cBuffer to exchange sensor data between Bridge Control Panel (BCP) and PSoC Analog Coprocessor */
ghc_sensor_data i2cBuffer = {MOIST_DRY, SENSOR_RAW_INITIAL, 
                             MOIST_HIGH, MOIST_LOW, 
                             DRY_NOT_DETECTED};

/*******************************************************************************
* Function Name: main
********************************************************************************
*
* Summary:
*  This function initializes all the resources, and in an infinite loop, performs tasks to measure all the sensor
*  parameters from sensors and to send the data over I2C.
*
* Parameters:
*  None
*
* Return:
*  int
*
* Side Effects:
*   None
*******************************************************************************/
int main()
{
	/* Sensor raw value */
	int16 sensorRawValue = 0;
	
	/* Motion detection thresholds */
	int16 highThreshold = MOIST_HIGH;		                           
	int16 lowThreshold = MOIST_LOW;
	
    /* Variable that stores the previous detection distance, used for checking if 
        the detection distance is changed by the master(BCP) */
    uint8 prevDetectionDistance = MOIST_DRY;
    
    /* Variable to store the status returned by CyEnterCriticalSection() */
    uint8 interruptState = 0;
    
    /* Enable global interrupts */
    CyGlobalIntEnable;
    
    /* Initialize all the hardware resources */
    InitResources();
    
    /* Infinite Loop */
    for(;;)
    {        
        /* If the master(BCP) changed the detection distance, change the second stage 
            amplifier (PGA) gain and the thresholds for the required detection distance*/
        if (i2cBuffer.detectionMoist != prevDetectionDistance)
        {
            prevDetectionDistance = i2cBuffer.detectionMoist;
            
            /* Set the required detection moist */
            switch (i2cBuffer.detectionMoist)
            {
                case MOIST_DRY:
                    /* Set second stage PGA gain and thresholds that gives 
                        3 feet detection distance */
                    AmplifierStage2_SetGain(AmplifierStage2_GAIN_1);
                    highThreshold = MOIST_HIGH;
                    lowThreshold = MOIST_LOW;
                    break;
                default:
                    /* Set second stage PGA gain and thresholds that gives 
                        3 feet detection distance */
                    AmplifierStage2_SetGain(AmplifierStage2_GAIN_1);
                    highThreshold = MOIST_HIGH;
                    lowThreshold = MOIST_LOW;
                    break;
            }
        }            
        /* Check if ADC data is ready */
        if(ADC_IsEndConversion(ADC_RETURN_STATUS))
        {
            /* Read ADC result */
            sensorRawValue = ADC_GetResult16(ADC_CHANNEL_GHC);
            
            /* Check if motion is detected */
            if((sensorRawValue > highThreshold) || 
               (sensorRawValue < lowThreshold))
            {
                /* Once the motion is detected, the pump is ON for 5s
                   and the motion detected variable in I2C buffer is latched to '1' for 5s.
                   If another motion is detected before 5s elapsed the timer is restarted 
                   to maintain 5s time window */
               	
				/* Stop the timer */
                Timebase5s_Stop();
				
				/* Reload the counter */
				Timebase5s_WriteCounter(Timebase5s_TC_PERIOD_VALUE);
				
				/* Start the 5s timer */
                Timebase5s_Start();
				
				/* Update the status of motion detection */
				i2cBuffer.moistDetected = DRY_DETECTED;
				
				/* Turn ON the LED */
                Pin_PUMP_Write(PUMP_ON);
            }
        } 
		
		/* Enter critical section to check if I2C bus is busy or not */
        interruptState = CyEnterCriticalSection();
            
        if(!(EzI2C_EzI2CGetActivity() & EzI2C_EZI2C_STATUS_BUSY))
        {
            /* Update the I2C buffer  */
            i2cBuffer.sensorRawValue = sensorRawValue;
			i2cBuffer.highThreshold = highThreshold;
			i2cBuffer.lowThreshold = lowThreshold;							
        }
		
		/* Exit critical section */
        CyExitCriticalSection(interruptState);		
    }    
}

/*******************************************************************************
* Function Name: void InitResources(void)
********************************************************************************
*
* Summary:
*  This function initializes all the hardware resources
*
* Parameters:
*  None
*
* Return:
*  None
*
* Side Effects:
*   None
*******************************************************************************/
void InitResources(void)
{
    /* Start EZI2C Slave Component and initialize buffer */
    EzI2C_Start();
    EzI2C_EzI2CSetBuffer1(sizeof(i2cBuffer), READ_WRITE_BOUNDARY, (uint8*)&i2cBuffer);
    
    /* Enable LED timebase ISR */
    isr_Timebase5s_Start();
    isr_Timebase5s_StartEx(TIMEBASE_ISR);   
    
    /* Start the Scanning SAR ADC Component and start conversion */
    ADC_Start();
    ADC_StartConvert();
    
    /* Start the Reference Buffer */
    RefBuffer_Start();
    
    /* Start Programmable Voltage Reference */
    PVref_Start();
    
    /* Enable Programmable Voltage Reference */
    PVref_Enable();
    
    /* Start the first stage amplifier */
    AmplifierStage1_Start();
    
    /* Start the second stage amplifier (PGA) */
    AmplifierStage2_Start();    
 }

/*******************************************************************************
* Function Name: CY_ISR(TIMEBASE_ISR)
********************************************************************************
*
* Summary:
*  This function implements the ISR for 5s timebase
* 
* Parameters:
*  None
*
* Return:
*  None
*
* Side Effects:
*   None
*******************************************************************************/
CY_ISR(TIMEBASE_ISR)
{
   	/* Reset the motion detection flag */
    i2cBuffer.moistDetected = DRY_NOT_DETECTED;
    /* Turn OFF the LED */
    Pin_PUMP_Write(PUMP_OFF);
    /* Stop the 5s timer */
    Timebase5s_Stop();
}
/* [] END OF FILE */

Credits

F. Yao

F. Yao

18 projects • 13 followers
.

Comments