Can someone please explain to me this line of coding

Hi. I wonder if any of you used the orientation sensor of smartphone before. Cus i don’t quite understand the coding (highlighted part) here. The writer is trying to control the base of the robotic arm using the x-axis of the sensor. From the coding, I know that the writer is trying to get the sensor value between 0 to 180 deg and map it to control the servo motor. But what I don’t understand is why he only include 0 to 180 deg? From what i know is the x-axis of orientation sensor is like a compass, 0/360 deg is always north, 90 east, 180 south and 270 west. How he is so sure that his phone is always facing in between north(0 deg) and south(180 deg)? when he is testing the robot. What if his phone lies between 150 to 250 deg?

Link for the project: https://www.instructables.com/id/Control-robotic-arm-with-gloves-Android-Phone-and--1/?fbclid=IwAR3gruvKfQyK5JJ6iB8aRj3AvyDAcaFt1AWFZlC9Ky2AKztd0Gd8JL1vdsw

#include  <Servo.h>
#include <OneSheeld.h>
Servo myservo1;
Servo myservo2;
Servo myservo5;
Servo myservo6;
void setup() 
{ 
  OneSheeld.begin();
  myservo1.attach(3);
  myservo1.write(0);
  myservo2.attach(5);
  myservo2.write(0);
  myservo5.attach(10);
  myservo5.write(70);
  myservo6.attach(11);
  myservo6.write(10);
  analogWrite(6, 200);
  analogWrite(9, 200);
} 
void loop() {
  if (ProximitySensor.getValue() > 0)
    myservo6.write(10);
  else 
    myservo6.write(120);
  if(OrientationSensor.getX() > 0 && OrientationSensor.getX() <= 180)               <<This line
  myservo1.write(map(abs(OrientationSensor.getX()),0,180,180,0));   
  if(OrientationSensor.getY() < -90 && OrientationSensor.getY() >= -180)
  myservo2.write(map(abs(OrientationSensor.getY()),180,90,0,90));
  if(OrientationSensor.getZ() > 0 &&  abs(OrientationSensor.getZ()) <= 60)
    myservo5.write(map(OrientationSensor.getZ(),0,60,70,180));   
  if(OrientationSensor.getZ() < 0 &&  abs(OrientationSensor.getZ()) <= 60)
    myservo5.write(map(abs(OrientationSensor.getZ()),0,60,70,0));
  OneSheeld.processInput();
}

i.JPG

If you read "How to use this forum" at the top of every forum you'll find out how to post your code so everyone can read it easily. It's much easier to get help that way.

Steve

if(OrientationSensor.getX() > 0 && OrientationSensor.getX() <= 180)               <<This line
  myservo1.write(map(abs(OrientationSensor.getX()),0,180,180,0));

in fact both these 2 are one statement. so if the value of OrientationSensor.getX() is between 0 and 180, (i figure that means it is facing a direction) then write to the servo with inverted mapping.

You’re in the best position to test exactly what values getX() is returning and when. Stick some Serial.print()s in and print the values, then you can see what is going on.

But I do wonder if the Android app has something like a calibrate or centring function which applies an offset to the pure orientation values.

Steve

Deva_Rishi:

if(OrientationSensor.getX() > 0 && OrientationSensor.getX() <= 180)               <<This line

myservo1.write(map(abs(OrientationSensor.getX()),0,180,180,0));


in fact both these 2 are one statement. so if the value of OrientationSensor.getX() is between 0 and 180, (i figure that means it is facing a direction) then write to the servo with inverted mapping.

Ya I understand that. What I don’t understand is why the writer only include 0 to 180 deg? Cause from what I know the smartphone’s orientation sensor has the range from 0 to 360 deg, if the writer includes only 0 to 180 deg, he has to make sure that his phone is always facing between the north (0 deg) to South (180 deg) which I think is not possible in some situations.

+180 degrees, -180 degrees, could be, could be...

Well it is only a 1Sheeld example/ demonstration program. If you need it to work differently you'll have to arrange it.

I might do something simplistic like add a centring button to derive an offset that I could use to modify the getX() value. You don't need a range greater than 180, you just need it to be centred on wherever your phone is pointing and not always on 90. OTOH since I've never used a OneSheeld and only spent a few minutes thinking about it there may be an even easier way to do it.

Steve

slipstick:
Well it is only a 1Sheeld example/ demonstration program. If you need it to work differently you'll have to arrange it.

I might do something simplistic like add a centring button to derive an offset that I could use to modify the getX() value. You don't need a range greater than 180, you just need it to be centred on wherever your phone is pointing and not always on 90. OTOH since I've never used a OneSheeld and only spent a few minutes thinking about it there may be an even easier way to do it.

Steve

Exactly. Alright I will think of that