SSD1306 OLED - sample code isn't working

Posted on
Page
of 2
Prev
/ 2
  • yes, I found it myself Gordon ;) just wanted to post I found solution ;)
    my code looks now that, and is working now ;)

    
    
    function OLED(){
    clearWatch();
    
    I2C1.setup({scl:B6,sda:B7});
    var g = require("SSD1306").connect(I2C1);
    g.clear();
      g.flip();
    
    pinMode(B15, 'input_pulldown');
    
    //Button tests
    var BTN22 = B15;
    var screen = 0;
    var battery; // = analogRead(A5)*2*3.3;
    var analogSample;
    
    function screens(){
      g.clear();
      g.setFontVector(15);
      g.drawString("one",25,20);
      g.flip();
    }
    
    function screens2(){
      analogSample = analogRead(A2);
     g.clear();
     g.setFontVector(15);
     g.drawString("OLED OFF?");
     g.setFontVector(10);
     g.drawString(analogSample,60,50);
     g.flip();
    }
    
    function screens3(){
      battery = analogRead(A5);
     g.clear();
     g.setFontVector(20);
     g.drawString("" + battery.toFixed(2),20,20);
     g.flip();
    }
    
    
    setWatch(function(e) {
        screen++;
        console.log(screen);
        clearInterval();
        if(screen === 0) screens();
      if(screen === 1) setInterval(screens2,200);
      if(screen === 2) setInterval(screens3,500);
      if(screen >= 2) screen = 0;
    
      
    },BTN22, {repeat: true, edge: 'rising', debounce:150});
    
    }
               
    OLED();
    
    
    
  • @bigplik

    took a copy of your code

    got syntax error on first line: "Unmatched '{' ." where does function OLED end ?

    ups, to slow, you alread found it by yourself ;-)

  • found another issue,
    if I want to just read analog values there clearInterval is ok, but if I want to measure something continuously, like time, stopwatch etc. then every time I swap between my screens I clear interval and will stop time or stopwatch which I don't want,
    any solution?
    or maybe there is some already written stopwatch code here?

  • When you do setInterval or setTimeout they return a value. Pass that value to clearInterval and it will only clear that one.

  • @DrAzzy ,what is the proper code form to stop interval that way you said?
    I tried

    clearInterval(){1}
    

    and it works when I typed it into console,
    but when I placed that form into my code into if statement
    WebIDE shows errors,
    I used then clearInterval(1); instead but it isn't work

  • DrAzzy means when you set your interval you assign it to a global variable. For example

    var state, interval;
    function toggleLed(){
      digitalWrite(LED1,state=!state);
    }
    
    setWatch(function() {interval=setInterval(toggleLed,500);},B­TN, {repeat: true, edge: 'rising', debounce:5});
    

    You would call clearInterval(interval) to stop only this interval.

  • @Ducky thanks! I just edited your post above to change var interval=setInterval to interval=setInterval though - as using var would create a local variable rather than actually assigning to the global interval.

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

SSD1306 OLED - sample code isn't working

Posted by Avatar for bigplik @bigplik

Actions