Skip to content

Instantly share code, notes, and snippets.

@e-Gizmo
Created June 10, 2019 09:36
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 e-Gizmo/7c29c83f2fca0b9fd8cf495a04e27734 to your computer and use it in GitHub Desktop.
Save e-Gizmo/7c29c83f2fca0b9fd8cf495a04e27734 to your computer and use it in GitHub Desktop.
Sample Codes for Direction Tilt Robot with Bluetooth Master.
/*
e-Gizmo RPI-1031 4-Direction Sensor
This is a sample sketch for Tilt direction sensor
to display the sensor output positions.
For the RPI-1031 - http://www.sparkfun.com/products/10621
Modified by e-Gizmo Mechatronix Central
http://www.e-gizmo.com
July 18,2017
*/
#define TILT_S1 4
#define TILT_S2 5
#define LED_TOP 10
#define LED_RIGHT 9
#define LED_BOTTOM 11
#define LED_LEFT 8
int state = 0;
void setup(){
pinMode(TILT_S1, INPUT);
pinMode(TILT_S2, INPUT);
pinMode(LED_TOP, OUTPUT);
pinMode(LED_RIGHT, OUTPUT);
pinMode(LED_BOTTOM, OUTPUT);
pinMode(LED_LEFT, OUTPUT);
Serial.begin(9600);
}
void loop(){
int position = GET_TILT_POSITION();
Serial.println(position);
if (Serial.available() > 0){
state = Serial.read();
}
//TOP
if(position == 0)
{
digitalWrite(LED_TOP, HIGH);
digitalWrite(LED_RIGHT, LOW);
digitalWrite(LED_BOTTOM, LOW);
digitalWrite(LED_LEFT, LOW);
Serial.write('0');
}
//RIGHT
if(position == 2)
{
digitalWrite(LED_TOP, LOW);
digitalWrite(LED_RIGHT, HIGH);
digitalWrite(LED_BOTTOM, LOW);
digitalWrite(LED_LEFT, LOW);
Serial.write('2');
}
//LEFT
if(position == 1)
{
digitalWrite(LED_TOP, LOW);
digitalWrite(LED_RIGHT, LOW);
digitalWrite(LED_BOTTOM, LOW);
digitalWrite(LED_LEFT, HIGH);
Serial.write('1');
}
//BOTTOM
if(position == 3)
{
digitalWrite(LED_TOP, LOW);
digitalWrite(LED_RIGHT, LOW);
digitalWrite(LED_BOTTOM, HIGH);
digitalWrite(LED_LEFT, LOW);
Serial.write('3');
}
delay(200); //DELAY
}
int GET_TILT_POSITION(){
int S1 = digitalRead(TILT_S1);
int S2 = digitalRead(TILT_S2);
return (S1 << 1) | S2; //BITWISE MATH
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment