Robojax

How Measure Tilt Angle using SCA60C angle sensor and take action based on Angle

دروس آردوینو به فارسی

How Measure Tilt Angle using SCA60C angle sensor and take action based on Angle

Using this Arduino code and SCA60C title angle sensor we can measure the angle of robotic arm or element. this Code is a vriation of the main code here where you can use this to take action for example turn ON or OFF something or do something when agale is equal, less or greator than some value.

Turning ON or OFF a buzzer or relay at certain angle

We define a pin of arduino as OUTPUT where relay or buzzer is connected. we call that pin controPin and have a vairable for desired angle where we take action at. We call it myAnle which holds the angle. We can have a time when the Arduino takes acction to turn ON or OFF the buzzer or relay, we can keep it in that state for certain time and we callit it waitTime. The wait time is millisecond. For 3 seconds we enter 3000 for example. If we don't want any delay, then we enter zero.

Topics and Timing of section of video

Resources for this sketch

Basic code to control direction of rotation of motor with 2 relay


 /*
 * Tile Angle Sensor SCA60C1 N1000060
 * it displays the angle from 0 to 180 degrees.
 * 
 * Watch video instructions for this code: https://youtu.be/5DjO8Fo_AtE
 * 
written by Ahmad Shamshiri on Mar 17, 2020 at 20:20 
 
 * in Ajax, Ontario, Canada. www.robojax.com
 * 
  Need wiring diagram from this code: 
  Purchase My Arduino course on Udemy.com http://robojax.com/L/?id=62
 * 

 * Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon http://robojax.com/L/?id=63

or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

const int controPin = 2;//pin to for control
const int waitTime = 2000;//2000mS or 2 seconds wait
const in myAnle = 45;
 
const int anglePin =A0;
int angle =0;
const int angleCompensate =0;

const int d1Pin =A1;
int d1State =LOW;
int d1Delay =0;//watch 

const int d2Pin =A2;
int d2State =LOW;
int d2Delay =0;

char *lables[]={"Angle","Alarm D1","Alarm D2"};

void setup()

{

  Serial.begin(9600);          //  setup serial
  Serial.println("Robojax SCA60C Angle Sensor");
  pinMode(controPin, OUTPUT);
  digitalWrite(controPin, LOW);//LOW is initial state of buzzer. Set to HIGH if  you want to it the other way

}



void loop(){
  //Robojax Tilt Angle Sensor
  //Watch video for details: https://youtu.be/5DjO8Fo_AtE
  getAngle();
  d1Alarm();
  d2Alarm();
  
  //take action here
  if(angle >=myAnle)
  {
	  //turn the buzzer ON
	  digitalWrite(controPin, HIGH);//HIGH or LOW as desired
	  delay(waitTime);// keep it waiting for the waitTime
  }
  
  // print out the value you read:
  Serial.print(lables[0]);// 
  Serial.print(":");
  Serial.print(angle);
  Serial.println("°");//degree symbol
//  Serial.print("	");
//  Serial.print(lables[1]);
//  Serial.print(":");  
//  Serial.print(d1State);
//  Serial.print("	");
//  Serial.print(lables[2]);
//  Serial.print(":");
//  Serial.println(d2State);  
//  Serial.println("======");
  delay(100);
//Robojax Tilt Angle Sensor
}

void getAngle()
{
 //watch video instruction https://youtu.be/5DjO8Fo_AtE
   angle =map(analogRead(A0), 96, 927, -90, 90) + angleCompensate; 
 
}

/**************************************/
/*! 
    @brief  get the state of d1 Alarm  HIGH if triggered
    @param  no parameter
    @returns returns nothing
    written by Ahmad Shamshiri on Mar 17, 2020 at 20:20 in Ajax, Ontario ,Canada
*/
/**************************************/
void d1Alarm()
{
  //watch video for details: https://youtu.be/5DjO8Fo_AtE
  //
  float vo = analogRead(d1Pin) * (5.0 / 1023.0);
  if(vo < 3.5)
  {
    d1State =HIGH;
    delay(d1Delay);    
  }else{
    d1State =LOW;    
  }

}//d1Alarm() end


/**************************************/
/*! 
    @brief  get the state of d1 Alarm  HIGH if triggered
    @param  no parameter
    @returns returns nothing
    written by Ahmad Shamshiri on Mar 17, 2020 at 20:20 in Ajax, Ontario ,Canada
*/
/**************************************/
void d2Alarm()
{
  //watch video for details: https://youtu.be/5DjO8Fo_AtE
  //
  float vo = analogRead(d2Pin) * (5.0 / 1023.0);

  if(vo < 3.5)
  {
    d2State =HIGH;
    delay(d2Delay);    
  }else{
    d2State =LOW;    
  }  

}//d1Alarm() end