Last active
November 3, 2020 09:09
-
-
Save botamochi6277/ac10f6d956511e2bdc8e0b5d16cb7d6e to your computer and use it in GitHub Desktop.
M5Atom_Sweep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file M5Atom_Sweep.ino | |
* @author botamochi6277 ([email protected]) | |
* @brief Sweep sample with M5Atom Series | |
* @version 0.1 | |
* @date 2020-11-02 | |
* | |
* @copyright Copyright (c) 2020 | |
* @licence MIT LICENCE | |
*/ | |
#include "M5Atom.h" | |
#include <ESP32Servo.h> | |
Servo myServo0; | |
// the setup routine runs once when M5Stack starts up | |
void setup() | |
{ | |
ESP32PWM::allocateTimer(0); | |
ESP32PWM::allocateTimer(1); | |
ESP32PWM::allocateTimer(2); | |
ESP32PWM::allocateTimer(3); | |
// Initialize the M5 object | |
M5.begin(); | |
myServo0.setPeriodHertz(50); // standard 50 hz servo | |
myServo0.attach(21, 600, 2400); | |
delay(1000); | |
} | |
/** | |
* @brief make Triangle Pulse | |
* /\ | |
* x / \ | |
* ---|----- ------- | |
* @param x | |
* @param center center of the pulse (peak) | |
* @param height hight of the pulse | |
* @param width width of the pulse | |
* @return int wave value at x | |
*/ | |
int TrianglePulse(int x, int center, int height = 255, int width = 1000) | |
{ | |
int begin_point = center - 0.5 * width; | |
int end_point = center + 0.5 * width; | |
if (x <= begin_point) | |
{ | |
return 0; | |
} | |
if ((begin_point < x) && (x <= center)) | |
{ | |
return map(x, begin_point, center, 0, height); | |
} | |
if ((center < x) && (x <= end_point)) | |
{ | |
return map(x, center, end_point, height, 0); | |
} | |
if (end_point < x) | |
{ | |
return 0; | |
} | |
} | |
void Update() | |
{ | |
static int count = 0; | |
// get reference angle for the servo | |
int y = TrianglePulse(count, 300, 180, 200); | |
myServo0.write(y); | |
count++; | |
if (count > 500) | |
{ | |
count = 0; | |
} | |
Serial.println(y); | |
} | |
// the loop routine runs over and over again forever | |
void loop() | |
{ | |
Update(); | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment