Go Down

Topic: Arduino leonardo code structure (Read 2883 times) previous topic - next topic

folgad0

Hi there, i got this piece of code that works perfectly, when a button is pressed in arduino an output 'n' will be printed on screen.

Code: [Select]
void loop(){

  button1State = digitalRead(button1);
 
  if (button1State == HIGH) {     
    digitalWrite(led, HIGH);
    digitalWrite(button1, HIGH);
   
    Keyboard.begin();
   
    while(digitalRead(button1)==HIGH){
      delay(50);
    }
    delay(50);
    Keyboard.press('n');
    delay(50);
    Keyboard.releaseAll();
    delay(50);
  }

else {
digitalWrite(led, LOW);
}
}


 The main goal now is to "replicate", this code s i can have several buttons to "press" diffrent letters.

so i tried this :

Code: [Select]


  button1State = digitalRead(button1);
 
  if (button1State == HIGH) {     
    digitalWrite(led, HIGH);
    digitalWrite(button1, HIGH);
   
    Keyboard.begin();
   
    while(digitalRead(button1)==HIGH){
      delay(50);
    }
    delay(50);
    Keyboard.press('n');
    delay(50);
    Keyboard.releaseAll();
    delay(50);
  }

else {
digitalWrite(led, LOW);
}

 button2State = digitalRead(button2);
 
  if (button2State == HIGH) {     
    digitalWrite(led, HIGH);
    digitalWrite(button2, HIGH);
   
    Keyboard.begin();
   
    while(digitalRead(button2)==HIGH){
      delay(50);
    }
    delay(50);
    Keyboard.press('a');
    delay(50);
    Keyboard.releaseAll();
    delay(50);
  }

else {
digitalWrite(led, LOW);
}




When i apload the code i can press 'n', and nothing else happens...
Is it the way the code is struvtured ? 

TY

UKHeliBob

Please post the whole of the program that does not work.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

folgad0

#2
Mar 26, 2015, 12:04 pm Last Edit: Mar 26, 2015, 12:05 pm by folgad0
This is the code for pressing 'n' , everything else that  i try to do in order to get other buttons to work is not successfull.


Code: [Select]
const int button1 = 8;
const int button2 = 9;
const int button3 = 10;
const int button4 = 11;
const int button5 = 12;
const int joy1 = 2;
const int joy2 = 3;
const int joy3 = 4;
const int joy4 = 5;
const int led = 13;

int button1State = 0;
int button2State = 0;
int button3State = 0;
int button4State = 0;
int button5State = 0;
int joy1State = 0;
int joy2State = 0;
int joy3State = 0;
int joy4State = 0;

void setup() {
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  pinMode(button5, INPUT);
  pinMode(joy1, INPUT);
  pinMode(joy2, INPUT);
  pinMode(joy3, INPUT);
  pinMode(joy4, INPUT);
  pinMode(led, OUTPUT);
}

void loop(){

  button1State = digitalRead(button1);
 
  if (button1State == HIGH) {     
    digitalWrite(led, HIGH);
    digitalWrite(button1, HIGH);
   
    Keyboard.begin();
   
    while(digitalRead(button1)==HIGH){
      delay(50);
    }
    delay(50);
    Keyboard.press('n');
    delay(50);
    Keyboard.releaseAll();
    delay(50);
  }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}


Thanks

aarg

He asked for the code that doesn't work...
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

UKHeliBob

That program is only reading one input.  Is it the one that works or not ?

You have button1 set as INPUT_PULLUP so nothing is going to happen until the button is pressed because of
Code: [Select]
  while (digitalRead(button1) == HIGH) {
      delay(50);
    }

So even if this one works you cannot add any more inputs.
By the way, why the delay() ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

folgad0

The delay comes from an arduino leonardo tutorial ive seen here : http://arduino.cc/en/Reference/KeyboardReleaseAll

And that means any more buttons that i place, will only work after that one ?

UKHeliBob

Quote
And that means any more buttons that i place, will only work after that one ?
No, as currently structured no other buttons will work because the first one has to be pressed in order for the program to continue.

As a first step consider changing from a while to an if in that code structure, although there are much better ways of checking several inputs and producing different outputs.  Arrays of input pins and associated outputs would be one way.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

folgad0

would u mind to guide to some easier way ?

Im rly stuck at this point  :(

UKHeliBob

Code: [Select]

const byte buttonPins[] = {A1, A2, A3};
const char outputs[] = {'a', 'b', 'c'};
const byte NUMBER_OF_INPUTS = sizeof(buttonPins);
byte previousButtonStates[NUMBER_OF_INPUTS];
byte currentButtonState;

void setup()
{
  Serial.begin(115200);
  for (int p = 0; p < NUMBER_OF_INPUTS; p++)
  {
    pinMode(buttonPins[p], INPUT_PULLUP);
  }
}

void loop()
{
  for (int p = 0; p < NUMBER_OF_INPUTS; p++)
  {
    currentButtonState = digitalRead(buttonPins[p]);
    if (currentButtonState != previousButtonStates[p] && currentButtonState == LOW) //button has become pressed
    {
      Serial.println(outputs[p]);
    }
    previousButtonStates[p] = currentButtonState;
  }
}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

johnwasser

I suspect that part of the problem is that you didn't enable the pull-up resistor on the added button pins:
Code: [Select]
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT);
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

folgad0

i think im missing something in that code u posted earlier, where you say
Code: [Select]
const byte buttonPins[] = {A8, A2, A3}; means my buttons pin ?

if so then A1 would make 'a' as output A2 makes 'b' as its output etc .. ?


UKHeliBob

Quote
if so then A1 would make 'a' as output A2 makes 'b' as its output etc .. ?
Yes.  Substitute your pin numbers and outputs.  Even better, try the code I posted and see what you get in the serial monitor before you attempt to apply the principles do your actual project.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

folgad0

the exact code u gave me , with no changes does nothing on the serial monitor.

folgad0

no , im sorry in the monitor serie i can print 'a' :D

UKHeliBob

Please post the code you are running copied from your IDE.
How are the inputs wired ?  What baud rate do you have set in the Serial monitor ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up