DIY Electronics

Simple electronics as a hobby

Animation using a Seeeduino xiao and Oled 128x64

image To display images on an Oled 128x64 I made 128x64 pixels BMP images with Photoshop. The BMP file has to be converted into a hex file, readable by the microcontroller. Every pixel is a bit. The first byte of 8-bits represents the first 8 pixels in the first column, second byte are the 8 pixels in the second column. There are 16 bytes for each column and 64 bytes for each row. A c# program converts the BMP to Hex. I added a tiny boost converter to raise the voltage from 1.5v to 5v, so it can run on a AAA battery. image

Source code Seeeduino Arduino IDE


source code for lalinea


Convert BMP to Hex using c#

image The library Attiny_minimal reads 8 pixels from top to bottom (y);
Make the image height divisable by 8.
The library Adafruit_GFX reads 8 pixels (bites) from left to right (x).
Make the image width divisable by 8.
>>> Convert BMP to Hex code <<<


Seeeduino Xiao - HID device to emulate keyboard

This website shows how to use seeeduino xiao with Adafruit_TinyUSb library to emulate keyboard strokes.
When seeeduino is inserted into USB port it will run the code.
The code starts "cmd" from Windows+R run box and connects to sever using FTP and uploads files, then it starts putty, connects to the server and logs in.
I put a digitalRead on pin 4 just to have a part of the code run. Pin 4 is connect to a push button to ground (not shown in the picture).
pinMode(4, INPUT_PULLUP) seems to work.
Initially I got this error when uploading the code: Adafruit_USBD_HID does not name a type; did you mean Adafruit_USBD_CDC?
The cause was two libraries with the same name Adafruit_TinyUSB_arduino; one in arduino/libraries and the other one in ..\AppData\Local\Arduino15\packages\Seeeduino\hardware\samd\1.8.5\libraries.
Then there was the return-key error. I used #define KEY_RETURN 40 to fix the problem.

>>> Seeeduino Xiao HID code <<<


8 queens problem in Python

The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens can capture each other; thus, a solution requires that no two queens share the same row, column, or diagonal. There are 92 solutions.

image

import sys

#-------------------------------------------------
def backtrack():    # curr_col == 8
    
    global curr_row
    global curr_col
    
    board[curr_row][7] = ' ' # remove queens from last row
    curr_row -= 1            # down one row
    if (curr_row < 0):
        end_of_the_game("row number less than 0 (zero)")
    curr_col = queens[curr_row]         # get col value from queens
    board[curr_row][curr_col] = ' '     # clean board from this row
    curr_col += 1             # advance col
    if (curr_col == 8):
        backtrack()


#------------------------------------------

def print_board(num):
    for i in range(7,-1,-1):
        print('|', end="")
        print ('|'.join(board[i]), end ="")
        print('|')
    print(f"------------------------------{num}------")
        
#-----------------------------------------------------        
def check_position(row,col):
    for y in [x for x in range(8) if x != col]:  # row check
        if board[row][y] == 'Q':
            #print(row,y)
            return False
    for x in [y for y in range(8) if y != row]: # column check
        if board[x][col] == 'Q':
            #print(row,y)
            return False
    x = row + 1
    y = col + 1
    while (x < 8 and y < 8):  # right & up
         # 1,3    2,4 3,5 4,6 5,7 6,8
         # 3,1    4,2 5,3 6,4 7,5 8,6 
        if board[x][y] == 'Q':
            #print(row,y)
            return False
        x += 1
        y += 1
    x = row - 1
    y = col - 1
    while (x > -1 and y > -1):  # left & down
        if board[x][y] == 'Q':
            #print(row,y)
            return False
        x -= 1
        y -= 1       
    x = row + 1
    y = col - 1
    while (x < 8 and y > -1):  # right & down
        if board[x][y] == 'Q':
            #print(row,y)
            return False
        x += 1
        y -= 1
    x = row - 1
    y = col + 1
    while (x > -1 and y < 8):  # left & up
        if board[x][y] == 'Q':
            #print(row,y)
            return False
        x -= 1
        y += 1
    #print(row,y)
    return True
#-----------------------------------------------
def end_of_the_game(msg):
    print (f"{msg}: exiting")

    sys.exit(0)    


#--------------INIT-----------------------------


curr_row = 0 # start at the first row
curr_col = 0 # start at the first column
board = list() # init
queens = list() # init

for x in range(8):
    board.append([])  #creates new row before populate
    for y in range(8):
        board[x].append(' ')

for x in range(8):
    queens.append(0)

# start here, set row = 0 board[0][0]='Q'
# when row > 7 print board and
# set row = 7 and advance curr_col
# stop when row = -1

queens[curr_row]=0
curr_col = 0
board[curr_row][curr_col] = 'Q'
sol = 0
    
while (True):  # end of game happens in backtrack()



    while (curr_row < 8):
        while (check_position(curr_row,curr_col) == False): # while False advance col
            board[curr_row][curr_col] = ' '
            curr_col += 1
            if (curr_col == 8):
                backtrack()
            
        queens[curr_row]=curr_col
        board[curr_row][curr_col] = 'Q'

        curr_row += 1  # go to next row and check
        curr_col = 0
        # end of while

    sol += 1 # number of solutions
    print_board(sol) # row == 8, col = 0
    # end of while (curr_row < 8)

    curr_row = 7
    curr_col = queens[curr_row]
    board[curr_row][curr_col] = ' '
    curr_col += 1
    if (curr_col == 8):
        backtrack()    
    queens[curr_row]=curr_col
    board[curr_row][curr_col] = 'Q'


Monostable 555 with touch plate

image

Classic monostable 555 with touch plate connected to the base of a NPN transistor. The pullup resistor of pin 2 is 1M ohm. Increasing the voltage helps the sensitivity of the plate, but I wanted to stick to 5 volt, because its availability. A better design would be with 2 plates with one to the base and the other one to Vcc.

Program Attiny85 using Arduino Uno

image

image

From the Arduino IDE Go to Arduino->Preferences then scroll down to Additional Board Managers URLs Copy & paste the following (if you already have a board manager URL just add a comma before pasting)

https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json


1: Uncomment the #define USE_OLD_STYLE_WIRING in the ArduinoISP sketch!!!!!
2: Remove the 10uF capacitor when uploading the ISP sketch.

After uploading a sketch with an external clock (16 or 20Mhz) and you want to go back to an internal clock you have to attach the crystal when uploading the next sketch or you get an Device signature = 0xff00ff error.



Happy birthday gadget

>>> Attiny85 code plus library files <<<


Silhouette game with Arduino and Oled 128x64

Switch the game on, read the name of the animal, when ready press left button and silhouettes of animals appear in succsession. Press right button when the silhouette of the animal you've just read shows up. When correct you will see a the text "right" and the number of correct choices until now. When incorrect the text "wrong" with the number of incorrect choices is displayed.
After a short delay the game starts again.
>>> silhouette game code <<<
Top