Strange behavior by pressing BTN

Posted on
  • Hi,

    I've tried to write a program for the Espruino.
    I'm new to Espruino and Java, so it is possible that I'm asking stupid questions ;)

    I have a Espruino Classic with a ILI9341 connected.
    If I run the program while the PC is connected to the board, It works fine.
    But when I change (In communications - Save on Send) Direct To Flash, the preset variable "HomeScore" shows "2" on the display, and when pressing the BTN, It goes up with 2 counts or sometimes 3 counts.

    Is this a known error, or do I something wrong?

    Regards,
    Peter

    B2.set(); // light on
     var HomeScore = 0;
    
    SPI1.setup({sck:B3, miso:B4, mosi:B5, baud: 1000000});
    var g = require("ILI9341").connect(SPI1, B6, B8, B7, function() {
      
      g.clear();
      g.setRotation(3);
      
      g.setFontVector(30);
      g.setColor(0,255,0);
      g.drawString("Home",5,50);
      g.setFontVector(45);
      g.setColor(255,255,255);
      g.drawString(HomeScore,40,85);
      
    });
    
    var LocHome = 0;
    function HomeUp() {
      g.setColor(0,0,0);
      g.setFontVector(45);
      g.drawString(HomeScore,25+LocHome,85);
      if (HomeScore<10) LocHome=15;
      if (HomeScore>8) LocHome=0;
      HomeScore = (HomeScore+1);
      g.setColor(255,255,255);
      g.drawString(HomeScore,25+LocHome,85);
    }
    
    setWatch(HomeUp, BTN, {repeat: true, edge:'falling'});
    
  • What you're doing there looks fine to me... You could try:

    setWatch(HomeUp, BTN, {repeat: true, edge:'falling', debounce:50});
    

    instead of

    setWatch(HomeUp, BTN, {repeat: true, edge:'falling'});
    

    I thought debounce on the button should have been automatic but maybe it's not... What can happen is the button actually physically bounces - effectively making two presses. Too fast for a human to see but the microcontroller picks it up.

    Having debounce:50 adds code that checks to ensure the button's been in that state for at least 50ms before calling your code.

    Or... what may be happening is you have the code saved twice somehow (once direct to flash, and once from save()) so you've got two setWatches. Try writing reset(1) on the left-hand side of the IDE and re-uploading your code (and if you're using direct to flash you don't need to call save() since it happens when you upload).

    Hope that helps!

  • Ive tried debounce before, bit it didn't't help.

    But !!!! Reset(1) + direct to flash and without save() did the job ;)
    Is Reset(1) used to completely erase the flash?
    I found "E.setBootCode()" somewhere to erase the flash

  • Because you did the save directly to flash AND did a save() too, you got two watches going on... you do either or... just not both... . Better would be to put your setWatch() into an onInit() function. That's the clean way to make sure what is going on when power cycling. You may even put your counter initialization there, because if you go with save but play a bit before executing the save() command, you save an already incremented counterand and it will start over with the already incremented counter rather than with 0 on power cycle. Take a look at conversation about simple explanation how to save code that espruino run on start? and related ones. It is not anymore all there is because Espruino has evolved and offers additional options, but the explained concepts still apply. If you want to keep the counter going beyond a single power cycle you save its value into the flash store on every increment and pick it up from there in the onInit() for resuming with last value on power cycle. I hope this helps.

  • Wed 2019.07.17

    Hi @user101874,

    'I found "E.setBootCode()" somewhere'

    That 'somewhere' might have been here:

    http://www.espruino.com/Reference#l_E_se­tBootCode

     

    'Is Reset(1) used to completely erase the flash?'

     

    'Calling reset(true) will cause all saved code in flash memory to be cleared as well.'
    http://www.espruino.com/Reference#l__glo­bal_reset


    'I'm new to Espruino and Java, so it is possible that I'm asking stupid questions ;)'

    Okay, you did qualify your statement, and to expand that awareness . . .

    Espruino runs a Javascript interpreter.

    Java is a Sun Microsystems compiler language.

    "Java was originally developed by James Gosling at Sun Microsystems (which has since been acquired by Oracle) and released in 1995"

    https://en.wikipedia.org/wiki/Java_(prog­ramming_language)

    Brendan Eich developed Javascript and was released with Netscape Navigator with the name
    "Mocha, but renamed LiveScript in September 1995 and later JavaScript in the same month"

    https://en.wikipedia.org/wiki/Brendan_Ei­ch

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Strange behavior by pressing BTN

Posted by Avatar for PeterD @PeterD

Actions