Leonardo keyboard stroke

Hey I'm building keyboard with 2 boards. One is Nano and Two is Leonardo

Nano is measuring potentiometer from 0-1023 and using my code to power Relays
nano will hold that potentiometer value for 10sec and other board will set that value to 0 after 10sec.

int potPin = A1;          //Sensor
int LEDPin = 9;
const int buttonPin = 12; //Reset
int LEDButtR = A3;
int LEDButtG = A4;
int LEDButtB = A2;
int readValue, lastRead;


int level1 = 2;
int level2 = 3;
int level3 = 4;
int level4 = 5;
int level5 = 6;
int level6 = 7;
int level7 = 8;
int level8 = 9;
int level9 = 10;
int level10 = 11;





int buttonState = 0;

unsigned long writeValue;  // to get the calculation done better, without using floats

void setup()  {

  pinMode(potPin, INPUT);
  pinMode(LEDPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(LEDButtG, OUTPUT);
  pinMode(LEDButtR, OUTPUT);
  pinMode(LEDButtB, INPUT);
  Serial.begin(57600);


  pinMode(level1, OUTPUT);
  pinMode(level2, OUTPUT);
  pinMode(level3, OUTPUT);
  pinMode(level4, OUTPUT);
  pinMode(level5, OUTPUT);
  pinMode(level6, OUTPUT);
  pinMode(level7, OUTPUT);
  pinMode(level8, OUTPUT);
  pinMode(level9, OUTPUT);
  pinMode(level10, OUTPUT);


}

void loop()  {

  one();
  reset();
  ledka();





  levelone();
  leveltwo();
  levelthree();
  levelfour();
  levelfive();
  levelsix();
  levelseven();
  leveleight();
  levelnine();
  levelten();




}



void reset()    {


  buttonState = digitalRead(buttonPin);
  Serial.println("Tlačítko");
  Serial.println(buttonState);


  if (buttonState == HIGH)


  {

    lastRead = 0;
    delay(50);
  }
}



void one()  {




  readValue = analogRead(potPin);
  if ( lastRead > readValue )

  {
    readValue = lastRead;
  }

  else

  {
    lastRead = readValue;
  }


  Serial.println("read");
  Serial.println(readValue);
}




void ledka() {

  if (readValue < 550) {
    digitalWrite(LEDButtR, HIGH);
    digitalWrite(LEDButtG, LOW);
  }

  else {

    digitalWrite(LEDButtR, LOW);
    digitalWrite(LEDButtG, HIGH);
  }
}



void levelone() {

  if (readValue > 600)

  {

    digitalWrite(level1, HIGH);
  }

  else

  {
    digitalWrite(level1, LOW);
  }
}




void leveltwo() {

  if (readValue > 650)

  {
    digitalWrite(level1, LOW);
    digitalWrite(level2, HIGH);
  }

  else

  {
    digitalWrite(level2, LOW);
  }
}



void levelthree() {

  if (readValue > 700)

  {
    digitalWrite(level2, LOW);
    digitalWrite(level3, HIGH);
  }

  else

  {
    digitalWrite(level3, LOW);
  }
}

void levelfour() {

  if (readValue > 750)

  {
    digitalWrite(level3, LOW);
    digitalWrite(level4, HIGH);
  }

  else

  {
    digitalWrite(level4, LOW);
  }


}

void levelfive() {

  if (readValue > 775)

  {
    digitalWrite(level4, LOW);
    digitalWrite(level5, HIGH);
  }

  else

  {
    digitalWrite(level5, LOW);
  }
}

void levelsix() {

  if (readValue > 800)

  {
    digitalWrite(level5, LOW);
    digitalWrite(level6, HIGH);
  }

  else

  {
    digitalWrite(level6, LOW);
  }

}

void levelseven() {

  if (readValue > 850)

  {
    digitalWrite(level6, LOW);
    digitalWrite(level7, HIGH);
  }

  else

  {
    digitalWrite(level7, LOW);
  }

}
void leveleight() {

  if (readValue > 975)

  {
    digitalWrite(level7, LOW);
    digitalWrite(level8, HIGH);
  }

  else

  {
    digitalWrite(level8, LOW);
  }

}
void levelnine() {

  if (readValue > 980)

  {
    digitalWrite(level8, LOW);
    digitalWrite(level9, HIGH);
  }

  else

  {
    digitalWrite(level9, LOW);
  }
}
void levelten() {

  if (readValue > 1020)

  {
    digitalWrite(level9, LOW);
    digitalWrite(level10, HIGH);
  }

  else

  {
    digitalWrite(level10, LOW);
  }
}

Leonardo... so with leonardo I have bit of problem. I need to light up LED at every level and when LED is light press Key on PC. But if I got to level idk 5 I need to press only key 5 not key 1,2,3,4 and 5 if you know what I mean. Every level will have its sounds and I need to play only one at the time for highest level. Can you take a look at it guys. There is my code. It will fire every key and not "disable" those before and it will spam until reset. I need to stroke just once and not again. Hey guys do you got me? I hope so...

(its like virtual piano and when Relay1 is ON (LOW) it will press key1 only once.)
(if Relay1 and Relay2 is ON (both LOW) it will press only key2 once and so on...)

For leonardo I need press key once just for highest LED. Not "ASD" just D for level 3 LED you got me? I need to shift it somehow and not pres "AS". Each key will play its sound in PC and I need just You WON sound on 5th LED. My code will play all five tones before sound YOU WON. I need just the highest one and only once.

#include "Keyboard.h"

const int level1 = 2;
const int level2 = 3;
const int level3 = 4;
const int level4 = 5;
const int level5 = 6;

void setup() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);

  Keyboard.begin();

}

void loop() {

  if (digitalRead(level1) == LOW) {
    Keyboard.press('A');
    delay(10);
    Keyboard.releaseAll();
  }
  if (digitalRead(level2) == LOW) {
    Keyboard.write('S');
    delay(10);
    Keyboard.releaseAll();
  }
  if (digitalRead(level3) == LOW) {
    Keyboard.write('D');
    delay(10);
    Keyboard.releaseAll();
  }
  if (digitalRead(level4) == LOW) {
    Keyboard.write('F');
    delay(10);
    Keyboard.releaseAll();
  }
  if (digitalRead(level5) == LOW) {
    Keyboard.write('G');
    delay(10);
    Keyboard.releaseAll();
  }
}

Thanks

Bit of edit there I need from you just idea or bit of help with code how to shift those LEDs. I need to somehow buff it in there wait for biggest number and then be like yeah we are on 40% we will press "F" key. My stuff can do "A,S,D,F,A,S,D,F,A,S,D,F,A,S,D,F,A,S,D,F,A,S,D,F,A,S,D,F"

Could you help me? I kinda cant sleep now so I need to finish it I guess...

BTW I dont understand much of coding. Everthing I learned here. Never been thought on school and I cant even speak english right so its kinda hard. Anyway thanks.

Guys I still need help

I dont know what is state changes... I will take a look at it right know. Hope it will help. Thanks if any other idea let me know.

Ill keep you updated

Update. Its multitasking. I used blink without a delay and state change together. Somehow its working nice.... but not as I wanted. It will print "AA" and I want just to print "A"

So once again. I want to take D2 from my nano and whenever its LOW I want Leonardo to write "A" on my MAC.
When D2 and D3 is LOW (Both at the same time) I want Leonardo to write "S" on my MAC.
When D2, D3, D4 is low I need to write "D"
When D2, D3, D4, D5 is low I need to write "F"
When D2, D3, D4, D5, D6 is low I need to write "G"

So I wrote part of code just for A and it will fire up twice and I cant do anything about it.

2nd problem is that I have no idea how to make it fire just "G" and not "ASDFG". Since each D2 to D6 is each level for Relay and controlled by potentiometer from 0-1023

I need to Prioritize "G" before "F", "F" before "D", "D" before "S", "S" before "A".

Any other ideas or help? Feel free to edit it I know its kinda basic but I dont know much about C and C++

#include "Keyboard.h"

// constants won't change. Used here to set a pin number:
const int  buttonPin = 12;
const int ledPin1 =  13;// the number of the LED pin

// Variables will change:
int ledState1 = LOW;             // ledState1 used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis1 = 0;        // will store last time LED was updated

// constants won't change:
const long interval1 = 20;           // interval1 at which to blink (milliseconds)

// Variables will change:
int buttonPushCounter1 = 0;   // counter for the number of button presses
int buttonState1 = 0;         // current state of the button
int lastbuttonState1 = 0;     // previous state of the button

void setup() {
  // set the digital pin as output:
  pinMode(ledPin1, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(1, INPUT);
  pinMode(0, INPUT);

}


void loop() {
  A();
}

void A() {


  buttonState1 = digitalRead(buttonPin);

  if (digitalRead(1) == LOW) {

    // compare the buttonState1 to its previous state
    if (buttonState1 != lastbuttonState1) {
      // if the state has changed, increment the counter
      if (buttonState1 == HIGH) {
        // if the current state is HIGH then the button went from off to on:
        buttonPushCounter1++;
        Serial.println("on");
        Serial.print("number of button pushes: ");
        Serial.println(buttonPushCounter1);
      } else {
        // if the current state is LOW then the button went from on to off:
        Serial.println("off");
      }
      // Delay a little bit to avoid bouncing
      delay(50);


    }
    // save the current state as the last state, for next time through the loop
    lastbuttonState1 = buttonState1;


    // turns on the LED every four button pushes by checking the modulo of the
    // button push counter. the modulo function gives you the remainder of the

    if (buttonPushCounter1 % 50 == 5) {
      Keyboard.write('A');
    } else {
      digitalWrite(9, LOW);
    }




    // here is where you'd put code that needs to be running all the time.

    // check to see if it's time to blink the LED; that is, if the difference
    // between the current time and last time you blinked the LED is bigger than
    // the interval1 at which you want to blink the LED.
    unsigned long currentMillis1 = millis();

    if (currentMillis1 - previousMillis1 >= interval1) {
      // save the last time you blinked the LED
      previousMillis1 = currentMillis1;

      // if the LED is off turn it on and vice-versa:
      if (ledState1 == LOW) {
        ledState1 = HIGH;
      } else {
        ledState1 = LOW;
      }

      // set the LED with the ledState1 of the variable:
      digitalWrite(ledPin1, ledState1);
    }
  }
}

OK I let it run for bit and it kinda broke itself and spam "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" so IDK HELP please I dont have much time left to finish it

Hey I thought that all that Error was thrue using PIN 1 but now its not working not even if I use code I posted here not even if I edit it. I tried other serial.read code and serial is working OK.... And I dont know what you meant with that "Also, work carefully through your logic there and think about when things are LOW and HIGH."

It should stand by when its pin1 now changed to pin2 is HIGH (since relays are on when LOW) and press key once when its LOW.

Also nobody told me about any idea about that prioritize stuff.

"I need to Prioritize "G" before "F", "F" before "D", "D" before "S", "S" before "A"."

I'm kinda desperate.... no ideas... I dont even have those fancy key on keyboard like square version of this... ()

First of all thanks for you help. Im so lost and depressed... working on that for last 2 days.... Nobody else helped me and idk why :frowning: So thanks.

So it work as you told me on that state change counter and delay less blink. So if pin 1 (in code I will edit it to 2) is LOW it will start blink and count it. On 5th pulse it should fire letter "A" on keyboad and wait until 2nd board (NANO) it reseted(5s). That all over again. I had problem that it fired letter "A" twice and after lets say 15-30mins it would broke. Some times it would spam "A" sometime it would press it 5 times etc... I thought I could fix it with changing start from pin1 to pin2 but now it wont work. It will blink leds on leonardo as it works but no serial and no keys being pressed. Board is working so idk. Im kinda clue less.

I need help to tidy up that timing stuff and make it work with 5 keys. Now it's kinda working and thats not good. I have no idea what I´m doing I´m learning as I go.

You can change what you want to make it fix yourself. Maybe my board is broke after all. Its some kind of $10 fake. Once my code wasnt working right on cheap UNO maybe its same.

Sir, I dont know have to mix it together tbh I already mixed with blink without delay. Its too much for me. Lot of same lines like (buttonState, etc...). Could you just clean my code maybe bit edit it. Its very overwhelming for me. Never wrote code that use more than 15% and now this... 22% and its not even working. I never thought it would be so hard to press one damn key when pin is LOW ffs >:( .

I swear I edited it to much longer post. I wrote that before your post showed up. Last post took 30mins to show up. I think site is having bit of problems