Thursday, March 3, 2022

Reading HEX from SD card and updating the neopixel display

 /*

  the following is modified from SD card file dump


  This sketch shows how to read some data from a file in the SD card using the 

  SD library and update an array of ledMat.


  The circuit:

   SD card attached to SPI bus as follows:

 ** MOSI - pin 11

 ** MISO - pin 12

 ** CLK - pin 13

 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)


  created  22 December 2010

  by Limor Fried

  modified 9 Apr 2012

  by Tom Igoe

  modified 3 Mar 2022

  by Jeszki

  */

#include <SPI.h>

#include <SD.h>

#include <FastLED.h>


#define DATA_PIN    6

#define LED_TYPE    NEOPIXEL

#define COLOR_ORDER GRB

#define NUM_LEDS    128


CRGB ledMat[NUM_LEDS];

     

const int chipSelect = 4;


void setup() {

  // Open serial communications and wait for port to open:


 FastLED.addLeds<WS2812, DATA_PIN, COLOR_ORDER>(ledMat, NUM_LEDS);


  Serial.begin(9600);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for native USB port only

  }

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:

  if (!SD.begin(chipSelect)) {

    Serial.println("Card failed, or not present");

    // don't do anything more:

    while (1);

  }

  Serial.println("card initialized.");

  // open the file. note that only one file can be open at a time,

  // so you have to close this one before opening another.

  File dataFile = SD.open("test.bin");  //just a text file


  // if the file is available, write to it:

  if (dataFile) {

    int x = 0;

    while (dataFile.available()) {

      char x1 = dataFile.read();

      char x2 = dataFile.read();

      char x3 = dataFile.read();

      Serial.print(x1,HEX);

      Serial.print(x2,HEX);

      Serial.print(x3,HEX);

 if (((x+1)%16)==0) { Serial.println();}     // print 16 char on 1 line

        ledMat[x++]= CRGB(x1,x2,x3);

    }  

    dataFile.close();

  }

  // if the file isn't open, pop up an error:

  else {

    Serial.println("error opening datalog.txt");

  }

   FastLED.show();

   delay(100);

}

void loop() {

}


Wednesday, March 2, 2022

Use union to convert long to bytes

// reference: https://forum.arduino.cc/t/solved-splitting-and-rebuilding-of-long-data-type-numbers/606975/3

//..\Union Example\union_example.ino

// doesn't work on Leonardo but could work on Nano 

//

typedef union u_conv

{

    long    lValue;

    byte    lByte[4];

};


u_conv

    strt,

    finish;

     

void setup() 

{

    Serial.begin(9600);

    strt.lValue = 2147483643;

    Serial.println("Original values:");

    Serial.println(strt.lValue, DEC);

    Serial.println(strt.lValue, BIN);

    //send start.lByte[0..3] to storage medium

    //

    //here, the retrieval is simulated by copying the byte values from the "strt" union

    //to the corresponding byet values in the "finish" union

    //

    //in use, you'd retrieve the bytes from storage and put them in a destination union


    for( int i=0; i<4; i++ )

        finish.lByte[i] = strt.lByte[i];


    Serial.println("Processed values:");

    Serial.println(finish.lValue, DEC);

    Serial.println(finish.lValue, BIN);

}//setup


void loop() 

{

}//loop

Thursday, October 28, 2021

esp32cam

Based on the setup in the following link:

https://how2electronics.com/esp32-cam-based-object-detection-identification-with-opencv/

1. open example -> esp32cam -> WifiCam 

2. update AP

3. tested working


Next, try combine with wifimanagerExample

Sunday, April 11, 2021

NANO Arduino CNC shield

 Needed to remove Mot_VOT_Sel (green jumper next power connector) before programming from Arduino IDE as supposed the external power prevent the reseting of the NANO

NOTE that most of the online pictures of the A4988 modules are inserted in the WRONG orientation.  Following picture is my working one.


CONTROL     Dir    Step
       X             pin2    pin5
       Y             pin3    pin6
       Z             pin4    pin7


Used the following to test the board with the modules:

/*     Simple Stepper Motor Control Exaple Code
 *      
 *  by Dejan Nedelkovski, www.HowToMechatronics.com
 *  
 */

// defines pins numbers for X
const int stepPin = 5; 
const int dirPin = 2; 
 
void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
}
void loop() {
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH); 
// increase the delay to 1 msec as the original 0.5 msec wouldn't work well
    delayMicroseconds(1000); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(1000); 
  }
  delay(1000); // One second delay
  
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for(int x = 0; x < 400; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(1000);
  }
  delay(1000);
}




Saturday, March 6, 2021

IOT using mkr 1010

Environment

Acer Aspire 4741G

Ubuntu 20.04.2 LTS

Arduino IDE 1.8.13

WifiNINA firmware ver. 1.3 (after installation of library)

Lesson

LEARN - Building Internet of Things Projects with Arduino IOT Cloud by Lee Assam

Setup

sudo snap install arduino

Board manager - install Arduino SAMD boards (32-bits ARM Cortex-M0+) by Arduino version 1.8.11

Library manager - install WifiNINA

in ubuntu

sudo usermod -a -G dialout $USER & reboot