0

I'm trying to create a project with my digispark where i can iterate through an hashmap, when i press the push button the digispark writes via digikeyboard the values from an hashmap. Each time i press the button it's supposed to write the first value, then i press the push button again and in the same line it cleans what was written in the first value and writes the second value and so on. when the last value is reached it goes again to the first value if the button is pressed. Right now i'm trying to put the iterator to work preperlly, this is the code i have:

#include "DigiKeyboard.h"
#include "HashMap.h"

HashMap<String, String> hashMap;
int i = 0;

void setup() {
   //setup hashmap
  hashMap.put("key0","test0");
  hashMap.put("key1","test1");
  hashMap.put("key2","test2");
  hashMap.put("key3","test3");
  hashMap.put("key4","test4");

  DigiKeyboard.sendKeyStroke(0);
  pinMode(0, INPUT);
}
void loop() {
  int sensorValue = digitalRead(0);
      while(sensorValue ==1 and i < hashMap.count())
      { 
        delay(100);
        DigiKeyboard.println( hashMap.valueAt(i));
        i++;
      } 
}

when i plug the digispark it prints automatically the first and the second value ("test0" "test1") and every time i press the button i get nothing.

This is my configuration: http://cdn.instructables.com/FZA/0UN8/HI3TXV8D/FZA0UN8HI3TXV8D.LARGE.jpg

Can anyone help me please?

  • Have you read through your code yet? – Ignacio Vazquez-Abrams Nov 7 '16 at 12:43
  • hi there, yes, i've read it, and i know that the code don't do what i what to do but for now i just wanted to when i press the button the value from the first position appear, and when i push it again the next one appear in another line and so on...from there i would move for something more complex like cleaning the previous value and start from the beginning when the last value is reached... – absint0o Nov 7 '16 at 15:14
  • You've looked at it, but you haven't actually read it. Read each line in loop() and understand fully what it does. Try explaining what each line does to someone else. – Ignacio Vazquez-Abrams Nov 7 '16 at 15:17
  • i've done that and the code inside the loop makes me sense: first loop: it reads the input from the pin 0, and enters the while, inside the while, while the sensorValue is equal to 1 (button pressed) and the i variable is less than the number of item inside the hashmap (5 items), in the first loop the i = 0 so 0 < 5 and it enters the while, inside the while it will print the value from the hashmap in the position 0 (test0), after this it will increment the i variable and start over again... – absint0o Nov 7 '16 at 16:03
  • "sensorValue is equal to 1 (button pressed)" Those are two independent things. You have no idea what the state of the button is after the assignment. – Ignacio Vazquez-Abrams Nov 7 '16 at 18:50
0
  1. Even with a "clean" button, your while() loop will whip through all 5 hashmap values before you can release the button.

  2. Switches and button aren't clean. They will (appear to) bounce, sending many pulses before they settle down to a fully on or fully off state. Read Nick Gammon's tutorial on de-bouncing switches.

  3. I'm not familiar with your display but if it is a one-line display, and you .println() at it, will the newline character "scroll" the line of text off the display? This could explain why you didn't at least see the final line. You may need a plain .print() call in order not to do that.

  4. Most LCD displays have a set of control codes to clear it, move the cursor, etc., which you would have to embed into the text string you send, or send using separate .print() calls. I don't see any provision in your code for including or sending those, so you'll need to fix that, too.
    Right now, I expect your display receives something like

    test0\ntest1\ntest2\ntest3\ntest4\n

    It probably needs to see something more like

    \033Etest0

    , e.g., for the first one, etc. That cruft at the beginning of the string is the VT52-code,
    escE, for "Clear the display". (I have no idea what command set your display uses, but the VT-52 commands have been in use for ... um, hmmph! well, a long time, anyway. :) You'll probably to replace it with whatever your particular device uses.

0

thank you all. i've manage to found a solution to my problem with this code, probably is not the most elegant way to do it but it works:

#include "DigiKeyboard.h"
#include "HashMap.h"

HashMap<char*, char*> hashMap;
int i = 0;
bool flag;

const byte switchPin = 0;
byte oldSwitchState = HIGH;  // assume switch open because of pull-up resistor
const unsigned long debounceTime = 100;  // milliseconds
unsigned long switchPressTime;  // when the switch last changed state

void setup ()
  {
  pinMode (switchPin, INPUT_PULLUP);

     //setup hashmap
  hashMap.put("key0","test0");
  hashMap.put("key1","test1");
  hashMap.put("key2","test2");
  hashMap.put("key3","test3");
  hashMap.put("key4","test4");

  DigiKeyboard.sendKeyStroke(0);

  }  // end of setup

void loop ()
  {
  // see if switch is open or closed
  byte switchState = digitalRead (switchPin);

  // has it changed since last time?
  if (switchState != oldSwitchState)
    {
    // debounce
    if (millis () - switchPressTime >= debounceTime)
       {
       switchPressTime = millis ();  // when we closed the switch 
       oldSwitchState =  switchState;  // remember for next time 
       if (switchState == HIGH)
          {
            delay(500);
            flag = true;
           while(i < hashMap.count() and flag == true)
            {  
              DigiKeyboard.println( hashMap.valueAt(i));
              i++;
              flag = false;
              }
          }  // end if switchState is LOW           
       }  // end if debounce time up

    }  // end of state change

  // other code here ...

  }  // end of loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.