Guess i should post my working code to help anyone else
AirsoftFCU.ino
/*
Hector Hind - 10.08.2019
Fire Control Unit "FCU"
Single,Burst,Full-Auto with single and burst controls via pot on 0
Mosfet on 36,Trigger on 26, & Mode button = HOME
*/
#include <M5StickC.h>
#include "images.c"
//****************************************************************PICTURES**************************************************************************************
const uint8_t*image1[] = {single,image_data_safe,image_data_hectechfculogo,image_data_burst,image_data_fullauto };
//**************************************************************************************************************************************************************
//*****************************************************************GPIO*****************************************************************************************
const int TriggerPin = 26; //Trigger switch attached to digital pin 4
const int MotorPin = 36; //Motor drive pin (on MOSFET) connected to PWM pin at pin 3
const int SelectorPin = M5_BUTTON_HOME; //Selector Pin
const int timingpot = 0; //pot
//**************************************************************************************************************************************************************
//******************************************************************VAR*****************************************************************************************
int buttonState; // variable for reading the pin status
int lastButtonState; // variable for reading the delayed status
int FireMode = 0; //Firing mode of the gearbox. 0 is always safe, 1 is always singleshot
int TriggerState = 0; // Trigger button state
const int RunTime = 50; //Motor run time - THIS CAN AND WILL CHANGE WITH YOUR SETUP, IF YOU SHOT MORE THEN ONCE LOWER IT ,SOMETIMES NOT SHOOTING THEN RASE IT
int potval;
int potvaltime;
int potvalshots;
//int val = 0; //for hall sensor , Thinking about adding a magnet to the gearbox to count each shot or rotation of the gears ect instead of guessing a 50ms on time as this will change with gear ratio,spring size & even battery voltage. 50ms "RunTime" is what works for me now
//**************************************************************************************************************************************************************
void setup()
{
M5.begin();
//!Logo
M5.Lcd.pushImage(0,0,80,160, (uint16_t *)image1[2]);
delay(5000);
// pinMode(M5_BUTTON_HOME, INPUT);
// pinMode(BUZZER_PIN, OUTPUT);
// SelectorPin = M5_BUTTON_HOME;
pinMode(timingpot, INPUT);
pinMode(SelectorPin, INPUT); // Set the switch pin as input
// Serial.begin(115200); // serial for testing - >>>>>>>>>>>>>>>>>> DISABLE ONCE WORKING!!!!!!!!!!!!!!!!!!!!!!!!!<<<<<<<<<<<<<<<<<<<<<<<
pinMode(MotorPin, OUTPUT); //Mosfet
pinMode(TriggerPin, INPUT_PULLUP); //Trigger
digitalWrite(MotorPin, LOW);
}
void loop()
{
//val = hallRead();
// Serial.println(val);//to graph
potval = analogRead(timingpot);
potvaltime = map(potval, 0, 4095, 300, 2300);
potvalshots = map(potval, 0, 4095, 1, 22);
buttonState = digitalRead(SelectorPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is LOW then the button went from off to on:
FireMode++;
if(FireMode == 4)
FireMode = 0;
} else {
// if the current state is HIGH then the button went from on to off:
//OFF
}
// Delay a little bit to avoid bouncing
delay(150);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// Different fire modes
if (FireMode == 0)
{ // all-off
M5.Lcd.pushImage(0,0,80,160, (uint16_t *)image1[1]); //safe
// M5.Lcd.setTextColor(BLACK, WHITE);
// M5.Lcd.setCursor(7, 0, 2);
// M5.Lcd.print("POT: "); M5.Lcd.print(potval);
digitalWrite(MotorPin, LOW);
}
else if (FireMode == 1)
{ // Single
M5.Lcd.pushImage(0,0,80,160, (uint16_t *)image1[0]); //single
TriggerState = digitalRead(TriggerPin);
if (TriggerState == LOW)
{
SINGLESHOT();
}
else
{
digitalWrite(MotorPin, LOW);
}
}
else if (FireMode == 2)
{ //Burst
// M5.Lcd.drawBitmap(0, 0, 80, 160,(uint16_t *)image_data_burst);
M5.Lcd.pushImage(0,0,80,160, (uint16_t *)image1[3]); //Burst
TriggerState = digitalRead(TriggerPin);
if (TriggerState == LOW)
{
BURST(potvalshots);
// digitalWrite(13, HIGH);
}
else
{
// digitalWrite(13, LOW);
digitalWrite(MotorPin, LOW);
}
}
else if (FireMode == 3)
{ //Fullauto
M5.Lcd.pushImage(0,0,80,160, (uint16_t *)image1[4]); //AUTO
TriggerState = digitalRead(TriggerPin);
if (TriggerState == LOW)
{
digitalWrite(MotorPin, HIGH); //turn on Motor at MotorSpeed
}
else
{
digitalWrite(MotorPin, LOW);
}
}
}
void SINGLESHOT()
{
digitalWrite(MotorPin, HIGH);
delay(RunTime); //For how long time the motor should run
digitalWrite(MotorPin, LOW);
delay(potvaltime);
}
void BURST(int BurstCount)
{
for (int j = 0; j < BurstCount; j++) //run the loop enough times to fire the burst
{
digitalWrite(MotorPin, HIGH); //turn on Motor at MotorSpeed
delay(RunTime); //For how long time the motor should run
digitalWrite(MotorPin, LOW);
}
digitalWrite(MotorPin, LOW);
}