Skip to content

Instantly share code, notes, and snippets.

@GOROman
Created May 22, 2023 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GOROman/d992830712b39389df02b83cc6bc6cb1 to your computer and use it in GitHub Desktop.
Save GOROman/d992830712b39389df02b83cc6bc6cb1 to your computer and use it in GitHub Desktop.
M5Atom Lite + AtomFly2 モーター制御 by GOROman (怪我して死んでも知らんVer.)
// M5Atom Lite + AtomFly2 モーター制御 by GOROman
#include <M5Atom.h>
// モーターの数
const int MOTOR_COUNT = 4;
// モーターのGPIOピンアサイン
const int MOTOR_PINS[] = {
22, // [A] FL
19, // [B] FR
23, // [D] RL
33, // [C] RR
};
const int MOTOR_PWM_FREQ = 300000; // 300kHz
const int MOTOR_PWM_BIT_NUM = 8; // 8bit (PWM Reso)
// モーターの初期化
void initMotor( int id )
{
// PWM チャンネル設定
ledcSetup( id, MOTOR_PWM_FREQ, MOTOR_PWM_BIT_NUM );
// PWM ピンアサイン
ledcAttachPin( MOTOR_PINS[id], id );
}
// モーターの更新
void updateMotor( int id, float duty )
{
// PWM Duty比設定
const uint32_t MAX = ( 1 << MOTOR_PWM_BIT_NUM ) - 1;
ledcWrite( id, (uint32_t) ( MAX * duty ) );
}
void setup()
{
M5.begin( true, true, true );
// LED赤点灯
M5.dis.drawpix( 0, 0xff0000 );
for (int i=0;i<MOTOR_COUNT;i++) {
initMotor( i );
}
delay( 500 );
}
void loop()
{
M5.dis.drawpix( 0, 0x000000 );
for ( int i = 0; i < MOTOR_COUNT; i++ )
{
updateMotor( i, 0.0f ); // Duty 0% (回転しない)
}
delay( 1000 );
M5.dis.drawpix( 0, 0xffffff );
for ( int i = 0; i < MOTOR_COUNT; i++ )
{
updateMotor( i, 0.1f ); // Duty 10%
}
delay( 1000 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment