Wednesday, June 29, 2022

tinkercad sine wave



int F = 2; //frequency of signal
int Fs = 500; //sampling frequency
int n = 500; //number of samples
float t; //time instance
int sampling_interval;
byte samples[500]; //to store the samples


void setup()
{
  pinMode(10, OUTPUT);
  for (int n=0; n<500; n++){
    t = (float)n/Fs; //create time instance to find the 500 samples
    samples[n] = (byte)(127*sin(2*3.14*t)+127);
  }
  sampling_interval = 1000000 / (F * n);
  //sampling inverval Ts = 1/frequency x number of sample (Ts = 1/Fn or Ts = T/n)x1000000 to convert to uS
  
}

void loop()
{
  for (int j=0; j<500; j++){
    analogWrite(10, samples[j]);
    delayMicroseconds(sampling_interval);
  }
}

reference:

Tuesday, June 28, 2022

tinkercad low pass filter


int d = 1000;
int count = 0;
float count_max = 3;
  
void setup()
{
  DDRD = 0b10000000; //pin 7 as output
  Serial.begin(9600);
}

void loop()
{
  PORTD ^= 0b10000000; //toggle pin 7
  Serial.println(d);
  delay(d);
  
  count++;
  if(count>count_max){
  d = d*0.9;
    count = 0;
    count_max = count_max * 1.11;
    Serial.println(count_max);
  }
  
  if(d<1){d=1000;}
}

reference:

Saturday, June 25, 2022

tinkercad ADC


#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd1(0x20,16,2);

void setup()
{
  lcd1.init();
  lcd1.clear(); 
  lcd1.backlight(); 
}

void loop()
{
  float ADC_A0 = analogRead(A0);
  float V_A0 = ADC_A0 * 5 / 1024;
  
  float ADC_A1 = analogRead(A1);
  float V_A1 = ADC_A1 * 5 / 1024;
  
  lcd1.setCursor(0,0);
  lcd1.print("A0  ");
  lcd1.print(V_A0, 4);
  
  lcd1.setCursor(0,1);
  lcd1.print("A1  ");
  lcd1.print(V_A1, 4);
  
  delay(500);
}

reference:

Thursday, June 23, 2022

Putin's Collection

tinkercad I2C communication between controllers

//Master

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd1(0x21,16,2);
byte board_address = 1;

byte Right[8] = {
  0b00000,
0b00100,
0b00110,
  0b11111,
  0b00110,
  0b00100,
  0b00000,
  0b00000,
};

byte x;

void setup()
{
  lcd1.init();
  lcd1.clear(); 
  lcd1.backlight(); 
  lcd1.createChar(0, Right);
  
  Wire.begin(1); 
  
}

void loop()
  //transmit to slave 2 and 3, then wait for slave response
  for(int i=2; i<4; i++){
    x=random(0, 255);
    
  Wire.beginTransmission(i); // transmit to device #2
  Wire.write(x);              // sends x 
  Wire.endTransmission();    // stop transmitting
 
  lcd1.setCursor(0,0);
  lcd1.clear(); 
  lcd1.print("M ");
  lcd1.write(0);
    lcd1.write(0);
  lcd1.print(" S");
  lcd1.print(i);
  lcd1.print("  ");
  lcd1.print(x);
 
  Wire.requestFrom(i, 2); //request 2 bytes from device 2 
  lcd1.setCursor(0,1);
 
  while(Wire.available()) {
    byte data = Wire.read(); 
    
  lcd1.print("S");
    lcd1.print(data); //1st byte is slave address
  lcd1.print(" ");
      lcd1.write(0);
      lcd1.write(0);
  lcd1.print(" M");
  lcd1.print("  ");
    
    data = Wire.read(); 
  lcd1.print(data); //2nd byte is echo data
  }
 
  delay(500);
  }
}

---------------------------
//Slave

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

byte board_address = 2;
byte data;

void setup()
{
  Wire.begin(board_address);               
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
}

void loop()
{
  
}

void receiveEvent(int n) {
  
  
  while(Wire.available()) {
    data = Wire.read(); 
  }
}

void requestEvent() {
  Wire.write(board_address);
  Wire.write(data);
}

reference:

I2C lcd

Tuesday, June 21, 2022

Jupiter Discoveries

tinkercad PCF8574 I2C reader + 74HC4017 decade couter


#include <Wire.h>

void setup()
{
  TCCR1A = 0b00000000; //TCCR1A bit 0, 1 and TCCR1B bit 3, 4 set timer mode
  TCCR1B = 0b00001101; //Set mode to be clear on compare. set scaler to be clock/1024
  OCR1A = 7811; //period = (OCR1A/B + 1)*scaler*62.5ns = 0.5s
  TIMSK1 = 0b00000010; //enable timer counter
  sei();
  
  Wire.begin();
  Serial.begin(9600);
}

void loop()
{
  byte data;
  Wire.requestFrom(0x20, 1); //request 1 byte from I2C expander
  while(Wire.available()) {
data = Wire.read();
      Serial.print("reader1 data ");
      printBin(data);
      Serial.println();
}
  
  Wire.requestFrom(0x21, 1); //request 1 byte from I2C expander
  while(Wire.available()) {
data = Wire.read();
      Serial.print("reader2 data ");
      printBin(data);
      Serial.println();
}
  
  delay(1000);
}

ISR(TIMER1_COMPA_vect){
PORTC ^= 0b00000001; //toggle pin  6(shift register clock)
}

void printBin(byte aByte) {
  for (int8_t aBit = 7; aBit >= 0; aBit--)
    Serial.write(bitRead(aByte, aBit) ? '1' : '0');
}
Johnson decade counter
reference:

Johnson decade counter

California High Speed Rail

Monday, June 20, 2022

tinkercad I2C LCDs


#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd1(0x20,16,2);
LiquidCrystal_I2C lcd2(0x21,16,2);

byte Up[8] = {
  0b00000,
0b00100,
0b01110,
  0b11111,
  0b00100,
  0b00100,
  0b00100,
  0b00000,
};

byte Down[8] = {
  0b00000,
0b00100,
0b00100,
  0b00100,
  0b11111,
  0b01110,
  0b00100,
  0b00000,
};

void setup()
{
  lcd1.init();
  lcd1.clear(); 
  lcd1.backlight(); 
  
  lcd1.createChar(0, Up);
  lcd1.createChar(1, Down);
  
  lcd1.setCursor(4,0); 
  lcd1.print("Commodities LCD 0x20");
  lcd1.setCursor(0,1);
  lcd1.print("Brent Oil ");
  lcd1.write(0);
  lcd1.print("0.87% ");
  lcd1.print("Natural Gas ");
  lcd1.write(0);
  lcd1.print("0.43% ");
  
  lcd2.init();
  lcd2.clear(); 
  lcd2.backlight(); 
  
  lcd2.createChar(0, Up);
  lcd2.createChar(1, Down);
  
  lcd2.setCursor(4,0); 
  lcd2.print("Commodities LCD 0x21");
  lcd2.setCursor(0,1);
  lcd2.print("Gold ");
  lcd2.write(0);
  lcd2.print("0.12% ");
  lcd2.print("Soybeans ");
  lcd2.write(1);
  lcd2.print("0.84% ");
}

void loop()
{
   lcd1.scrollDisplayLeft();     
   lcd2.scrollDisplayLeft(); 
  
  delay(200);
}
//I2C address sniffer
#include <Wire.h>

void setup()
{
  Serial.begin (9600);

  // Leonardo: wait for serial port to connect
  while (!Serial) 
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 8; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}

void loop()
{
 
}

reference:

scroll left

multiple LCDS

How Far Can U.S. Artillery Shoot?

Sunday, June 19, 2022

ibex climbs a dam

tinkercad PCF8574 I2C expander write


#include <Wire.h>

byte data = 0;
int num = 0, num1 = 0, num2 = 0, num3 = 0;

void setup()
{
  Wire.begin();
}

void loop()
{
  Wire.beginTransmission(0x20);
  data = segment_decoder(num%10);
  Wire.write(data);
  Wire.endTransmission();
  
  Wire.beginTransmission(0x21);
  data = segment_decoder(num1%10);
  Wire.write(data);
  Wire.endTransmission();
  
  Wire.beginTransmission(0x22);
  data = segment_decoder(num2%10);
  Wire.write(data);
  Wire.endTransmission();
  
  Wire.beginTransmission(0x23);
  data = segment_decoder(num3%10);
  Wire.write(data);
  Wire.endTransmission();
  
  num++;
  if(num==80){num=0;}
  if(num%2==0){num1++;}
  if(num1==10){num1=0;}
  if(num%4==0){num2++;}
  if(num2==10){num2=0;}
  if(num%8==0){num3++;}
  if(num3==10){num3=0;}
  
  delay(200);
}

byte segment_decoder(int n){
  byte b;
  
  switch(n){
  case 0:
    b = 0b00111111;
    break;
    case 1:
    b = 0b00000110;
    break;
    case 2:
    b = 0b01011011;
    break;
    case 3:
    b = 0b01001111;
    break;
    case 4:
    b = 0b01100110;
    break;
    case 5:
    b = 0b01101101;
    break;
    case 6:
    b = 0b01111100;
    break;
    case 7:
    b = 0b00000111;
    break;
    case 8:
    b = 0b01111111;
    break;
    case 9:
    b = 0b01101111;
    break;
    default:
    b = 0b00000000;
    break;
  }
  
  return ~b;
}
The main difference between I2C and Serial, is that Serial is a point to point connection and I2C is a bus which support many devices, each with own address. 

 I2C have better speed than Serial, but Serial support longer wires, depends what your needs are.



reference:

Saturday, June 18, 2022

Gold Jewelry Factory

ZUHAIR MURAD 2020 Couture Show

Got Talent World Best LIGHT SHOWS

tinkercad 74HC595 shift register + 7 segment display 1


bool shifting = false;
int shift_count = 0;
byte data1 = 0, data2 = 0;
int num = 0;

void setup() 
{
  DDRD = 0b01111100;  //pin 2, 3, 4, 5, 6 output
  // arduino pin 3 connects to 74HC595 output enable pin
  // arduino pin 4 connects to 74HC595 serial input pin
  // arduino pin 5 connects to 74HC595 output register clock pin
  // arduino pin 6 connects to 74HC595 shift register clock pin
  
  //shift register clock
  TCCR1A = 0b00000000; //TCCR1A bit 0, 1 and TCCR1B bit 3, 4 set timer mode
  TCCR1B = 0b00001010; //Set mode to be clear on compare. set scaler to be clock/8
  OCR1A = 1999; //period = (OCR1A/B + 1)*scaler*62.5ns = 1ms
  TIMSK1 = 0b00000010; //enable timer counter
  sei();
}

void loop() 
{
  //updateShiftRegister();
  shifting = true;
  //data = random(0, 255);
  data1 = segment_decoder(num/10);
  data2 = segment_decoder(num%10);
  num++;
  if(num==100){num=0;}
  delay(200);
}

//shift register clock
ISR(TIMER1_COMPA_vect){
PORTD ^= 0b01000000; //toggle pin  6(shift register clock)
  byte array1[2] = {0b00010000, 0b00000100};
  byte array2[2] = {data1, data2};
  
  shift_register(array1, array2);
}

//shift both registers at the same time
void shift_register(byte input_pin[], byte data_byte[]){
if(shifting){
    //disable register output
    PORTD = PIND | 0b00001000; //set output enable high
    
  //if shift register clock is high
  if(PIND & 0b01000000){
    PORTD = PIND & 0b11011111; //set output register clock low
      
      for(int i=0; i<sizeof(data_byte); i++){
      //if data bit is high
      if(bitRead(data_byte[i], 7-shift_count)){
      //set serial input pin high
        PORTD = PIND | input_pin[i];
     
      //if data bit is low
      else{
      //set serial input pin low
        PORTD = PIND & ~input_pin[i];
      }
      }
      
      shift_count++;
  }
    //if shift register clock is low
    else{
      //if data bit is read
      if(shift_count >0){
      PORTD = PIND | 0b00100000; //set output register clock high
        //if all 8 data bits are read
        if(shift_count == 9){
        shifting = false;
          shift_count = 0;
        }
      }
    }
  }
  //shift finished
  else{
  PORTD = PIND & 0b11110111; //set output enable low
  }
}

byte segment_decoder(int n){
  byte b;
  
  switch(n){
  case 0:
    b = 0b01111110;
    break;
    case 1:
    b = 0b00001100;
    break;
    case 2:
    b = 0b10110110;
    break;
    case 3:
    b = 0b10011110;
    break;
    case 4:
    b = 0b11001100;
    break;
    case 5:
    b = 0b11011010;
    break;
    case 6:
    b = 0b11111000;
    break;
    case 7:
    b = 0b00001110;
    break;
    case 8:
    b = 0b11111110;
    break;
    case 9:
    b = 0b11011110;
    break;
    default:
    b = 0b00000000;
    break;
  }
  
  return b;
}

reference:

Friday, June 17, 2022

tinkercad 74HC595 shift register 2


bool shifting = false;
int shift_count = 0;
byte data = 0;

void setup() 
{
  DDRD = 0b01110000;  //pin 4, 5, 6 output
  // arduino pin 4 connects to 74HC595 serial input pin
  // arduino pin 5 connects to 74HC595 output register clock pin
  // arduino pin 6 connects to 74HC595 shift register clock pin
  Serial.begin(9600);
  
  //shift register clock
  TCCR1A = 0b00000000; //TCCR1A bit 0, 1 and TCCR1B bit 3, 4 set timer mode
  TCCR1B = 0b00001101; //Set mode to be clear on compare. set scaler to be clock/1024
  OCR1A = 1562; //period = (OCR1A/B + 1)*scaler*62.5ns = 0.1s
  TIMSK1 = 0b00000010; //enable timer counter
  sei();
}

void loop() 
{
  //updateShiftRegister();
  shifting = true;
  data = random(0, 255);
  printBin(data);
  Serial.println();
  delay(8000);
}



//shift register clock
ISR(TIMER1_COMPA_vect){
PORTD ^= 0b01000000; //toggle pin  6(shift register clock)
  if(shifting){
  //if shift register clock is high
  if(PIND & 0b01000000){
    PORTD = PIND & 0b11011111; //set output register clock low
      //if data bit is high
      if(bitRead(data, 7-shift_count)){
      //set serial input pin high
        PORTD = PIND | 0b00010000;
      } 
      //if data bit is low
      else{
      //set serial input pin low
        PORTD = PIND & 0b11101111;
      }
      
      shift_count++;
  }
    //if shift register clock is low
    else{
      //if data bit is read
      if(shift_count >0){
      PORTD = PIND | 0b00100000; //set output register clock high
        //if all 8 data bits are read
        if(shift_count == 9){
        shifting = false;
          shift_count = 0;
        }
      }
    }
  }
  
}

void printBin(byte aByte) {
  for (int8_t aBit = 7; aBit >= 0; aBit--)
    Serial.write(bitRead(aByte, aBit) ? '1' : '0');
}

reference:

timer interrupt

port manipulation

Thursday, June 16, 2022

tinkercad 74HC595 shift register 1


void setup() 
{
  DDRD = 0b01110000;  //pin 4, 5, 6 output
  // arduino pin 4 connects to 74HC595 serial input pin
  // arduino pin 5 connects to 74HC595 output register clock pin
  // arduino pin 6 connects to 74HC595 shift register clock pin
  Serial.begin(9600);
}

void loop() 
{
  updateShiftRegister();
  delay(5000);
}

void updateShiftRegister()
{
   //digitalWrite(latchPin, LOW);
   PORTD = PIND & 0b11011111;
  
   //shiftOut(dataPin, clockPin, LSBFIRST, 0b10101010);
  byte data = random(0, 255);
  printBin(data);
  Serial.println();
  
  shiftOut(4, 6, MSBFIRST, data);
  
   //digitalWrite(latchPin, HIGH);
  PORTD = PIND | 0b00100000;
}

void printBin(byte aByte) {
  for (int8_t aBit = 7; aBit >= 0; aBit--)
    Serial.write(bitRead(aByte, aBit) ? '1' : '0');


printBin

Kaliningrad

Tuesday, June 7, 2022

tinkercad lcd screen

#include <LiquidCrystal.h>

char text1[] = "   Time up is";
unsigned long t;

LiquidCrystal lcd(7, 6, 5, 4, 3, 2); 

void setup()
{
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print(text1);
  
  TCCR1A = 0b00000000; //TCCR1A bit 0, 1 and TCCR1B bit 3, 4 set timer mode
  TCCR1B = 0b00001101; //Set mode to be clear on compare. set scaler to be clock/1024
  OCR1A = 15624; //period = (OCR1A/B + 1)*scaler*62.5ns = 1s
  TIMSK1 = 0b00000010; //enable timer counter
  sei();
}

void loop()
{
}

ISR(TIMER1_COMPA_vect){
t = millis();
 lcd.setCursor(0, 1);
  lcd.print("       ");
  lcd.print(t/1000);
  lcd.print("s");
}

reference:

timer

Sunday, June 5, 2022

Steve Winwood - Higher Love

 

tinkercat H-bridge and motor

click buttons to change motor direction
void setup()
{
  DDRD = 0b01101100;  //pin 2, 3, 5, 6 output
  PORTD = 0b01000100; //set initial motor directions
  PCICR = 0b00000001;  //interrupt group 1
  PCMSK0 = 0b00000011; //pin 7 interrupt
  sei();
}

void loop()
{
 
}

ISR(PCINT0_vect){
  //if interrupt on pin 8(PB0), toggle pin 2, 3
  if ( (PINB&(1<<0)) ){
  PORTD ^= 0b00001100;
  }
  
  
  //if interrupt on pin 9(PB1), toggle pin 5, 6
  if ( (PINB&(1<<1)) ){
  PORTD ^= 0b01100000;
  }
}

reference:

port register

H bridge driver

Saturday, June 4, 2022

tinkercat motor and sensor


when object moves in detected area, motor spins
void setup()
{
  DDRD = 0b00100000;  //pin 5 output
  PCICR = 0b00000100;  //interrupt group 3
  PCMSK2 = 0b10000000; //pin 7 interrupt
  sei();
}

void loop()
{
 
}

ISR(PCINT2_vect){
PORTD ^= 0b00100000; //toggle pin 5
}

reference:

Wednesday, June 1, 2022

Chanel | Cruise 2023

tinkercat fast pwm normal mode


toggle clock on timer overflow
int period;

void setup()
{
   TCCR2A = 0x00; // Wave Form Generation Mode 0: Normal Mode; OC2A disconnected
  TCCR2B = (1<<CS22) + (1<<CS21) + (1<<CS20); // prescaler = 1024
  TIMSK2 = (1<<TOIE2); // interrupt when TCNT2 is overflowed
  DDRD |= (1<<PD7);  // Portmanipulation: replaces pinMode(7, OUTPUT);

  Serial.begin(9600);
}

void loop()
{
 
}

ISR(TIMER2_OVF_vect){
    PORTD ^= (1<<PD7); // toggle PD7
  period = TCNT2 *0.0000625*1024;
  Serial.print("Period: ");
  Serial.println(period);
}

reference: