fnf — ESP32-S3 simulation

An interactive ESP32-S3 circuit simulation you can run free in your browser on Velxio, by adam-berglund.

Components

Sketch code

int pinsleft[10]={5,6,7,15,2,3,8,9};
int pwmFrequency = 5000;
int pwmResolution = 8;
unsigned long lastcheck[2] = {0,0};
unsigned long lastcheck2[2] = {0,0};
int speed = 2;

int maxlight = 80;

unsigned long resetcheck[2] = {0,0};
int chartleft = 2000;
int chartright = 1000;
int listlength = 4;
int buttonpin[2] = {11,20};

int buttonState[2] = {HIGH,HIGH};
int lastButtonState[2] = {HIGH,HIGH};
int score = 0;
int reading[2] = {HIGH,HIGH};

unsigned long lastDebounceTime[2] = {0,0};
unsigned long debounceDelay[2] = {20,20}; // Debounce time in milliseconds

bool running[2] = {false,false};
bool firstrun[2] = {false,false};
int lightlevel[2] = {0,0};
int led[2] = {0};

bool burunning[2] = {false,false};
bool bufirstrun[2] = {true,true};
int bulightlevel[2] = {0,0};
int buled[2] = {0,0};

void setup() {
  Serial.begin(115200);
  pinMode(buttonpin[0],INPUT_PULLUP);
  pinMode(buttonpin[1],INPUT_PULLUP);
  for (int x = 0; x<listlength*2; x++){
    ledcAttach(pinsleft[x],pwmFrequency,pwmResolution);
  }
}

void loop() {
  //leftside
  sendnote(0,0);
  if (running[0]){
    sendbackup(0,0);
  }
  if (burunning[0]){
    sendbackup(0,0);
  }
  bool pressedleft = buttonpress(0);
  if (pressedleft){
    score = score + scoring(0);
  }
  if (millis()-resetcheck[0] > chartleft){
    resetcheck[0] = millis();
    Serial.println(score);
    reset(0);
  }
  //rightside
  sendnote(1,4);
  if (running[1]){
    sendbackup(1,4);
  }
  if (burunning[1]){
    sendbackup(1,4);
  }
  bool pressedright = buttonpress(1);
  if (pressedright){
    score = score + scoring(1);
  }
  if (millis()-resetcheck[1] > chartright){
    resetcheck[1] = millis();
    Serial.println(score);
    reset(1);
  }
  
}


int buttonpress(int lane){
  reading[lane] = digitalRead(buttonpin[lane]);

  if (reading[lane] != lastButtonState[lane]) {
    lastDebounceTime[lane] = millis();
    lastButtonState[lane] = reading[lane];
  }

  if ((millis() - lastDebounceTime[lane]) > debounceDelay[lane]) {
    if (reading[lane] != buttonState[lane]) {
      buttonState[lane] = reading[lane];
      if (buttonState[lane] == LOW) {
        Serial.println("hej");
        lastButtonState[lane] = reading[lane];
        return true;
      }
    }
  }
  return false;
}

void reset(int lane){
  if (running[lane] && !burunning[lane]){
    bufirstrun[lane] = false;
  } else {
    firstrun[lane] = false;
  }
  
 
}

int scoring(int lane){
  if (led[lane]==listlength-2 || buled[lane]==listlength-2){
    return lightlevel[lane];
  } else if(led[lane]==listlength-1 || buled[lane]==listlength-1){
    return maxlight - lightlevel[lane];
  } else {
    return -100;
  }
}

void sendnote(int lane, int pins){
  if (firstrun[lane]){
    if (running[lane]){
 if (led[lane]>=listlength){
      running[lane] = false;
    }
    if (millis()-lastcheck2[lane] > speed){
    lastcheck2[lane] = millis();
      /*Serial.println(led);
      Serial.println(lightlevel);*/
      ledcWrite(pinsleft[led[lane]+pins],maxlight-lightlevel[lane]);