Můj problém je to takový že: Mám NANO to mi podle potenciometru rozsvítí LEDky 0-5 0-1020. Když jsem na levelu 5 svítí jich všech 5 když na 2 svítí dvě atd... easy že? ne....
Protože pak potřebuju aby mi Leonardo podle každý tý urovně zmáčklo klávesu na PC. Když bude první ledka LOW( v mém případě protože relátka se zapínají LOW signálem) zmáčkne to "A" když bude první a druhá zmáčkne to "S" když 1,2,3 zmáčkne "D" když 1,2,3,4 zmáčkne "F" a když 1,2,3,4,5 zmáčne "G"
Potřebuju aby to nedělalo "ASDFG" na levelu 5 chci jen "G" to je první problém.Jako druhý mi to nechce psát jen jedno písmeno. Použil jsem millis a počítám pulzy moc tomu sám nerozumím. Prý tam mám přidat debounce ale to asi sám už nezvládnu dostat to do tohodle stavu bylo peklo dál už nevím.
Dolu přihodím kód můžete ho editovat jak chcete. Nano je ve stavu klidu teda HIGH a když zapíná relé a ledky tak LOW. Takže to musí reagovat na low. Díky za snahu.
Kód: Vybrat vše
#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 = 5; // 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(2, INPUT);
pinMode(0, INPUT);
Serial.begin(9600);
Keyboard.begin();
}
void loop() {
A();
}
void A() {
buttonState1 = digitalRead(13);
if (digitalRead(2) == 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 == 3) {
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);
}
}
}