hi, I'm trying to use the mpu6050 to move the servo and the led, but how should I set if(ax == 17000 or angle 180) so that it turns (rgb led)red, how should I do it? thank you
[code]
#include <FastLED.h>
#define NUM_LEDS 1
CRGB leds[NUM_LEDS];
#include <Wire.h>
#include <MPU6050.h>
#include <Servo.h>
Servo sg90;
int servo_pin = 2;
MPU6050 sensor ;
int16_t ax, ay, az ;
int16_t gx, gy, gz ;
int pos = 90; // variable to store the servo position
int lastPos = 90;
void setup ( )
{
FastLED.addLeds<WS2812, 6>(leds, NUM_LEDS);
sg90.attach ( servo_pin );
Serial.begin(9600);
Wire.begin ( );
sensor.initialize ( );
}
void loop ( )
{
sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
ax = map (ax, -17000, 17000, 0, 180) ;
if(ax <= -4700)
{
leds[1] = CRGB(255,0,0);
Serial.print("YES");
}
else
{
leds[1] = CRGB(0,0,0);
Serial.print("NO");
}
sg90.write (ax);
delay (20);
}
[/code]
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
See also FAQ - Arduino Forum for general rules on forum behaviour and etiquette.
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…
Please use code tags when posting code.
Tutorial on measuring tilt angles: How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing-DFRobot
I'm trying to put the code in the code tags, but it doesn't work
In the post editor, select the code and push the "</>" editor button.
but what about the code, how to solve it? Thank you
I'm the one who solved it, I had to use degrees
Hi,
Can you please post your working code to help close this thread and for others who may have a similar problem.
Thanks.. Tom...
[code]
#include <Wire.h>
#include <MPU6050.h>
#include <Servo.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6
#define NUMPIXELS 1
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels
MPU6050 sensor;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int pos = 90; // variable to store the servo position
int lastPos = 90;
Servo sg90;
int servo_pin = 2;
void setup()
{
sensor.setFullScaleAccelRange(8);
sg90.attach(servo_pin);
Serial.begin(9600);
Wire.begin();
pixels.begin();
sensor.initialize();
}
void loop()
{
pixels.clear();
sensor.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
ax = map(ax, -17000, 17000, 180, 0);
sg90.write(ax);
if(ax <= 50)
{
pixels.setPixelColor(0, pixels.Color(255, 0, 0));
}
delay(500);
}
[/code]
system
Closed
May 6, 2023, 12:42pm
11
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.