Abdulrhman Batik
Published © Apache-2.0

Distance Sensor Alarm

Control with a sound level depending on reading (IR sensor).

BeginnerFull instructions provided2 hours111
Distance Sensor Alarm

Things used in this project

Story

Read more

Code

H08R6 (IR sensor)

C/C++
code
/**
  ******************************************************************************
  * File Name          : main.c
  * Description        : Main program body
  ******************************************************************************
  *
  * COPYRIGHT(c) 2015 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
	
/*
		MODIFIED by Hexabitz for BitzOS (BOS) V0.2.4 - Copyright (C) 2017-2021 Hexabitz
    All rights reserved
*/

/* Includes ------------------------------------------------------------------*/
#include "BOS.h"

/* Private variables ---------------------------------------------------------*/

float sensor = 0.0f;
uint8_t state_sensor;
uint8_t old_state_sensor;
uint8_t mode=1;
uint8_t sample = 200;
/* Private function prototypes -----------------------------------------------*/
uint8_t AbsDistanceToColor(float distance);

uint8_t counter=0;

/* Main functions ------------------------------------------------------------*/

int main(void)
{


  /* MCU Configuration----------------------------------------------------------*/

  /* Reset all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all user peripherals */

  /* Initialize BitzOS */
  BOS_Init();

  /* Call init function for freertos objects (in freertos.c) */
  MX_FREERTOS_Init();

  /* Start scheduler */
  osKernelStart();
  
  /* We should never get here as control is now taken by the scheduler */

  /* Infinite loop */
  while (1)
  {

  }

}

/*-----------------------------------------------------------*/

/* User Task */
void UserTask(void * argument)
{

//	 Set units to mm
	SetRangeUnit(UNIT_MEASUREMENT_CM);
//	 Stream to memory
	Stream_ToF_Memory(50, portMAX_DELAY, &sensor);

  for(;;)
  {

	  state_sensor = AbsDistanceToColor(sensor);

	  Delay_ms(250);
	  switch(state_sensor)
	  {

		  case 1:
			        //freq
			  	  	messageParams[0] = (uint8_t)(150>>24);
		 		  	  messageParams[1] = (uint8_t)(150>>16);
		 		  	  messageParams[2] = (uint8_t)(150>>8);
		 		  	  messageParams[3] = (uint8_t)(150);
		 		  	  // MusicNotesNumOfSamples
		 		  	  messageParams[4] = sample;
		 		  	  // durationInSeconds 2 sec
		 		  	  //Note: time Division 16 in sec
		 		  	  //Time = durationTime / 16 in sec
		 		  	  messageParams[5] = 2;
		 		  	  SendMessageToModule (2,CODE_H07R3_PLAY_SINE, 6);
		 		  	  break;
		  case 2:

		  			  //freq
		  			  messageParams[0] = (uint8_t)(300>>24);
		  			  messageParams[1] = (uint8_t)(300>>16);
		  			  messageParams[2] = (uint8_t)(300>>8);
		  			  messageParams[3] = (uint8_t)(300);
		  			  // MusicNotesNumOfSamples
		  			  messageParams[4] = sample;
		  			  // durationInSeconds 2 sec
		  			  //Note: time Division 16 in sec
		  			  //Time = durationTime / 16 in sec
		  			  messageParams[5] = 2;
		  			  SendMessageToModule (2,CODE_H07R3_PLAY_SINE, 6);
		  			  break;
          case 3:
        	  	//freq
        	    messageParams[0] = (uint8_t)(500>>24);
        	  	messageParams[1] = (uint8_t)(500>>16);
        	  	messageParams[2] = (uint8_t)(500>>8);
        	  	messageParams[3] = (uint8_t)(500);
        	  	// MusicNotesNumOfSamples
        	  	messageParams[4] = sample;
        	  	// durationInSeconds 2 sec
        	  	messageParams[5] = 2;
        	  	SendMessageToModule (2,CODE_H07R3_PLAY_SINE, 6);	  
		  		  	break;
          case 4:
        	  	//freq
		  		  	messageParams[0] = (uint8_t)(700>>24);
		  		  	messageParams[1] = (uint8_t)(700>>16);
		  		  	messageParams[2] = (uint8_t)(700>>8);
		  		  	messageParams[3] = (uint8_t)(700);
		  		  	// MusicNotesNumOfSamples
		  		  	messageParams[4] = sample;
		  		  	// durationInSeconds 2 sec
		  		  	messageParams[5] = 2;
		  		  	SendMessageToModule (2,CODE_H07R3_PLAY_SINE, 6);
		  		  	break;
		  case 5:
              //freq
		  		  	messageParams[0] = (uint8_t)(1000>>24);
		  		    messageParams[1] = (uint8_t)(1000>>16);
		  		    messageParams[2] = (uint8_t)(1000>>8);
    	  			messageParams[3] = (uint8_t)(1000);
		  		    // MusicNotesNumOfSamples
		  		    messageParams[4] = sample;
		  		    // durationInSeconds 2 sec
		  		    messageParams[5] = 2;
		  		    SendMessageToModule (2,CODE_H07R3_PLAY_SINE, 6);
		  		    break;
		  case 6:
              //freq
		  		  	messageParams[0] = (uint8_t)(1500>>24);
		  		  	messageParams[1] = (uint8_t)(1500>>16);
		  		  	messageParams[2] = (uint8_t)(1500>>8);
		      	  messageParams[3] = (uint8_t)(1500);
		  		  	// MusicNotesNumOfSamples
		  		  	messageParams[4] = sample;
		  		  	// durationInSeconds 2 sec
		  		  	messageParams[5] = 2;
		  		    SendMessageToModule (2,CODE_H07R3_PLAY_SINE, 6);
		  		  	break;	  	
		  case 7:
              //freq
		  		  	messageParams[0] = (uint8_t)(3000>>24);
		  		  	messageParams[1] = (uint8_t)(3000>>16);
		  		  	messageParams[2] = (uint8_t)(3000>>8);
		      	  messageParams[3] = (uint8_t)(3000);
		  		  	// MusicNotesNumOfSamples
		  		  	messageParams[4] = sample;
		  		  	// durationInSeconds 2 sec
		  		  	messageParams[5] = 2;
		  		  	SendMessageToModule (2,CODE_H07R3_PLAY_SINE, 6);
		  		  	break;

		  defult :
				      break;
	  }
  }
}

uint8_t AbsDistanceToColor(float distance)
{


	if (distance < 15.0f)
		return 1;
	else if (distance < 20.0f)
		return 2;
	else if (distance < 25.0f)
		return 3;
	else if (distance < 30.0f)
	  	return 4;
	else if (distance < 35.0f)
	  	return 5;
	else if (distance < 40.0f)
	  	return 6;
	else if (distance < 45.0f)
		return 7;
	else
		return 8;


}

Credits

Abdulrhman Batik

Abdulrhman Batik

1 project • 0 followers

Comments