Friday, 28 April 2017

Functions - Arduino

 As your programs become more complicated we try to break up the code into
more easily managed blocks.
We break them up into "functions".
 
The arduino main reference page lists all the major functions:

The two essential functions in any sketch are
void setup (){}
and 
void loop (){}

 All functions must have these basic things:
1. unique name [alphanumeric characters (A to Z; a to z; 0 to 9) and the underscore (_).]
    You can't start the name with a number.
2. the name must be followed by ()
3. they must have a return type : eg void
4. The body of the function is enclosed in braces {}
 
-------------------------------------------------------------------------

Structure of functions.

This is an example of a function that was created to print the numbers 123456789. 
In this example the name "NumbersAcend()" is the function name.
In this example "void" is referred to as the return type

void NumbersAcend()
{
      Serial.println("123456789");
 }
 
 
 Links
+ https://www.youtube.com/watch?v=VHK69SB3lo4
 
 
 ---------------------------------
-------------------------------------

Tuesday, 25 April 2017

Known Space - Part 4 - Faster-Than-Light (2651 AD and later)

This is part 4 of  a suggested reading order for Larry Niven's novels set in Known Space.
Parts 1 & 2 covering the Belter & Slower Than Light Stories are here. 
Part 3 covering the years 2351 - 2650 AD (including the The Man - Kzin Wars)is here
 
There are about 9 novels and 35 short stories set in this universe spanning  over 1000 years in the future. The region covers a small portion of the Milky way.
Beginning with the first human explorations of the solar system, the later stories describe humanity's expansion into other near systems.
 
You can probably divide this time span into 4 periods:
1.  Early Exploration (Pre 2000 AD)
2.  Slower-Than-Light (2000 to 2350AD)
4.  Faster-Than-Light (2651 AD and later)
 
This period is covered in Ringworld, Crashlander, MKW (Man-Kzin Wars), Neutron Star.
and the Fleet of Worlds - FoW series


  • [2650-2652]
     Fleet of Worlds
  • Set 200 years before the discovery of Ringworld

 The Fleet of World series consists of five books by the same authors:

The first four novels are prequels to Ringworld; the last one is a sequel.

  • [2658]
     Ghost (Crashlander)

 Novelette

 

 

 

 

 

 

 

 


  • [2658]
     Betrayed (JoW)

 Juggler of Worlds is the second book in the Fleet of Worlds series which is intended to serve as a prequel to Ringworld.

 

 

 

 

 

 

 

 

  • [2658 AD] Fly By Night (MKW 9)

 


  • [2658 AD]
    The Soft Weapon (Neutron Star)
  It was first published in the February 1967 issue of If
The story introduces the character of Nessus, a Puppeteer who later became one of the main characters of the novel Ringworld.

 

 

 

 

 

 

  • [2658-2660 C.E.] The Outsiders-Epilogue (JoW)

 


  • [2675 AD]
     Destroyer of Worlds

 Destroyer of Worlds is the third book in the Fleet of Worlds series which is intended to serve as a prequel to Ringworld. 

It is set ten years after Juggler of Worlds, drawing heavily from Protector

 

 

 

 

 

 


  • [2780-2781 AD]
    Betrayer of Worlds
 Betrayer of Worlds is the fourth book in the Fleet of Worlds prequel series to Ringworld. 
 
 
 
 
 
 
 
 
 
 
 
 
 
  • [2686 C.E.] The Color of Sunfire (LarryNiven.net)

 http://www.larryniven.net/stories/color_of_sunfire.shtml


  • [2830 AD.]
     There Is A Tide (TKS)
 First printed in Galaxy, July 1968.
The reprinted in the collections A Hole in Space 1974, Tales of Known Space 1975 and Three Books of Known Space 1996.

 

 

 

 

 

 


  • [2850-2851 AD.]
     Ringworld

 + Ringworld - Larry Niven  

 ***

 

 

 

 

 

 

 


  • [2871-2881 AD.]
     The Ringworld Engineers

 

 

 

 

 

 

 

 

 


  • [2882-2893 AD.]
     The Ringworld Throne

 

 

 

 

 

 

 

  • [2892 C.E.] Peter Robinson (MKW10)

 


  • [2893 AD.]
     Ringworld’s Children

 

 

 

 

 

 

 

 

 

  • [2893-2894 AD.] Fate of Worlds: Return from the Ringworld

 

  • [2895 AD] String (MKW12)
  • [2899 AD] The Hunting Park (MKW 11)
  • [2965 AD.] Peace and Freedom (MKW12)]

 


  • [3101 AD.]
     Safe at Any Speed (TKS)

First published in the Magazine of fantasy and science fiction may 1967 

You can also find this in Larry Niven's "Tales of Known Space". 

 

 

 

 

 

-------------------------------------------------------------------------------------------------
In alphabetical Order, The stories & novels set in the Known Universe are:
 
 
  • The Adults (short story)
  • ARM (short story)
  • At The Bottom Of A Hole (short story)
  • At The Core (short story)
  • Becalmed In Hell (short story)
  • The Borderland Of Sol (short story)
  • Choosing Names (short story)
  • Cloak Of Anarchy (short story)
  • The Coldest Place (short story)
  • The Deceivers (short story)
  • The Defenseless Dead (short story)
  • The Ethics Of Madness (short story)
  • Eye Of An Octopus (short story)
  • Flatlander (short story)
  • Fleet Of Worlds (novel, written in collaboration with Edward M. Lerner)
    • Juggler Of Worlds (novel, written in collaboration with Edward M. Lerner)
    • Destroyer Of Worlds (novel, written in collaboration with Edward M. Lerner)
    • Betrayer Of Worlds (novel, written in collaboration with Edward M. Lerner)
    • Fate Of Worlds (novel, written in collaboration with Edward M. Lerner)
  • Fly-By-Night (short story)
  • Ghost (short story)
  • A Gift From Earth (novel)
  • Grendel (short story)
  • The Handicapped (short story)
  • How The Heroes Die (short story)
  • The Hunting Park (short story)
  • The Jigsaw Man (short story)
  • Madness Has Its Place (short story)
  • N-Space (anthology)
  • Neutron Star (short story)
  • The Organleggers (short story)
  • The Patchwork Girl (novel)
  • Procrustes (short story)
  • Protector (novel)
  • A Relic Of The Empire (short story)
  • Ring World (novel)
    • The Ringworld Engineers (novel)
    • The Ringworld Throne (novel)
    • Ringworld's Children (novel)
  • Safe At Any Speed (short story)
  • Slowboat Cargo (short story)
  • There Is A Tide (short story)
  • The Soft Weapon (short story, adapted into an episode of Star Trek: The Animated Series)
  • Wait It Out (short story)
  • The Warriors (short story, introduced the Kzin)
  • The Woman In Del Rey Crater (short story)
  • World Of Ptavvs (novel)

Do let me know is there are any mistakes, errors or omissions.  

--------------------------------------------

sci Fi Index

----------------------------------------------

 

The coldest Place - Larry Niven

 This story first appeared in the December 1964 issue of If magazine.
 

It is Larry Niven's first published work.
The "coldest Place" refers to the far side on Mercury. ... the side that faces away from the Sun.
Back then, it was thought that Mercury was "tidal locked" in a 1:1 orbit, it's "day" equal to its "year" and that it's dark side would be colder even than the outer planets as it never rotated.
 
Then in 1965, the astronomer Giuseppe Colombo,  discovered that it did in fact rotate with a 3:2 spin orbit resonance.
 
The story revolves around a manned mission to Mercury to search for life which was detected 2 years earlier by the Messenger VI  probe.
There are two crew on this mission : Howie & Eric.
Eric appears to be some form of AI or a live brain hooked up to the ship.
 
 
 
"Eric looked much like an electrical network, 
    except for the grey mass at the top which was his brain."
"In all directions from his spinal cord and his brain....
Eric's nerves reached out to master the ship"
...... "If Eric dies, I die, because Eric is the ship"
If I die, Eric dies .... Insane".
 

They catch what appears to be an amoeba shaped blob of Helium II. Is this life ???

"It's only helium!"
"I get it now Howie"
"Helium II. That's all our monsters are."
"Nuts"
"Helium II, the superfluid that flows uphill"
"Check them for contaminants."
"For what?"
"Contaminants. My body is hydrogen oxide with contaminants. If the contaminants in the helium are complex enough, it might be alive."
"There are plenty of other substances".
 
 
 
The interesting bit about this story isn't the discovery of life. Rather I think it's Eric.
This idea of a "sentient ship" predates Arthur C Clarke's depiction of HAL in the novel 2001: A Space Odyssey, which was published in 1968. 
Eric appears to be emotionally sensitive,.....  maybe even a tad unstable.
Today, the concept of a sentient spaceship is quite common. "Ancilliary Justice" by Ann Leckie freatures a ship's AI that is reduced to a single soldier. The Ship Who Sang by Anne McCaffrey is another great example as are many of the novels of Ian M. Banks.
 
"The ship who sang" is particularly interesting.
Anne wrote these stories in the 1960's
They feature one person, Helva, who becomes brainship XH-834.  
The Ship Who Sang novel was published in 1969, however the first five chapters incorporate
early magazine Novelettes.

So maybe Anne McCaffrey can claim the title as the first to write about a sentient ship.
In any case, The Coldest Place is really short ... just 3 pages , but Larry Niven manages to pack in quite a lot.
Well worth a read.



--------------------------------------------

sci Fi Index

----------------------------------------------

Saturday, 22 April 2017

San Franscisco - Alcatraz Island.

 Alcatraz Island.


The Guards Apartments & entrance to the island.



 
If you look closely, you can see the Golden Gate Bridge in the distance rising up through the fog


San Fran architecture






Friday, 21 April 2017

Serial Monitor - Reading numbers

This is all about how to read data from the serial monitor.
The serial monitor, allows you to capture and enter data in real time.
It is the 'tether' between the computer and your Arduino - it lets you send and receive text messages
which is handy for debugging and also controlling the Arduino from a keyboard! 
 
The Arduino IDE serial monitor looks like this:
 

 
Below is some code on entering data into the serial monitor.

The arduino will wait till you have entered a value.

This serial monitor is in TinkerCad. 

Example 1

// variables
int myNumber;
String msg="Please Enter Your Number:";
String msg2="Your Number is: ";

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

void loop()
{
  // 3 things we do: ask wait read
  Serial.println(msg); //ask
  while (Serial.available()==0){
    // while there is no data
    //on the serial loop do nothing ..wait
   }
    //read
 
    myNumber=Serial.parseInt();
    Serial.print(msg2);

    Serial.println(myNumber);
 
  }

-----------------------------------------------------

Example 2
// we want the user to select how many times to blink the LED

// we want the user to select how many times to blink the LED

// variables
int numBlinks;
String msg="How many blinks do you want?:";
int j;
int bt=100;
int redPin=12;


void setup()
{
  Serial.begin(9600);
  pinMode(redPin,OUTPUT);
}

void loop()
{
  // 3 things we do: ask wait read
  Serial.println(msg); //ask
  while (Serial.available()==0){
    // while there is no data
    //on the serial loop do nothing ..wait
  }
    //read
    numBlinks=Serial.parseInt();
  for (j=1;j<=numBlinks;j=j+1){
    digitalWrite(redPin,HIGH);
    delay(bt);
    digitalWrite(redPin,LOW);
    delay(bt);
  }
      
   
 
}

-----------------------------------------------------------------------------------

Example 3
Not using the LED this time.

-------
// calculate the area of a circle

// variables

String msg="What is the radius of your circle ?:";
float radius;
float area;
String msg2="Your Circle Has an Area of : ";
float pi=3.14;

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

void loop()
{
  // 3 things we do: ask wait read
  Serial.println(msg); //ask
  {
  while (Serial.available()==0);
    // while there is no data
    //on the serial loop do nothing ..wait
}
    //read
    radius=Serial.parseFloat(); // parseFloat  not parseInt
  area=pi*radius*radius;
  Serial.print(msg2);
  Serial.println(area);
 
      
   
 
}

 ----------------------------------------------

Links

 
 ---------------------------------
------------------------------------- 

Saturday, 15 April 2017

Adding more steps to the pioneer squid sequence

 Increasing track length.for the Toraiz squid

The standard number of steps in a sequence is 16.
So just 1 bar.


This can be increased to 32, 48, and 64 (4 bars) by pressing "shift" and the "->" buttons .
 
Doing this once will add a new set of 16 steps ,linked with the previous 16, to create a 32 step pattern.
Pressing the <- and -> buttons allows you to toggle between the pages.
 
 -----

 
 


Deluge Effects - Reverb


The Reverb grid Short cuts  are
Room size - Room size applied to the whole song
Dampening
Width
Pan - Enables panning to one side or the other.
Amount

1. Press Shift + the pad to activate.
    You must be in Clip view.
2. turn (SELECT) to adjust the value.


Room Size
Larger rooms would provide longer delay
range: 0-50.

Dampening
emulates things like curtains, furniture, people -- things that absorb the sounds
Adds warmth and reduces high frequencies.

Width
 Sets the spread of the reverb to give stereo effects
range: 0-50.

Pan
 - Enables panning to one side or the other.
Range is 32L-0-32R.

Amount
This Sets the amount of signal sent to the reverb.
The sound can come from a synth or a sample or kit.
range: 0-50.

Side
Reverb has a specific side chain compressor
 You can only access this via the select menu knob.
1. clip view
2. Press select knob
3. Navigate to the FX section and press (SELECT).
4. Navigate to the ‘rEVE’ section and press (SELECT).
5. Select ‘SidE’ - sidechain. Press (SELECT)
6. Select ‘VoLU’ - volume level. Press (SELECT)

options : 0 - 50 & AUTO
AUTO takes its parameters from the sidechain compressor of the
sound with the highest reverb.


-------------------------------------------------------

You can also use the gold knobs to effect the reverb
 1. Ensure in clip view for a kit, synth.
2. Press [SIDECHAIN / REVERB] affect button. Button illuminates orange.
3. Turn (LOWER) to change reverb amount.

Monday, 3 April 2017

Complier directives - Arduino

Complier directives are sometimes called Preprocessor directives. 
They are commands that tell the complier how to behave & treat certain parts of code.
Note, they are not part of the code.
They aren't complied (converted into zeros and ones) and loaded into the arduino.
 
The preprocessor works by searching for lines that start with the hash sign (#) and have some text after it. This is called a preprocessor directive and it’s a sort of “command” for the preprocessor.
 
 Examples are:
 
 #include - 
We use this when including a arduino library ... “adds” the library to the main sketch.
It tells the compiler to get the code from that library.


#define
 
eg 
#define VALUE 3
Here we define a word as a number.
This tells the compiler, every time it sees the word "VALUE"
to change the word VALUE into a 3

 
#endif
 
 
#ifndef
This checks whether a library is included

#ifdef
a just shorthand for #if defined, which can be used to test multiple macros in a single condition. Note that every condition has to end with #endif directive, to specify which parts of the code are affected by the condition, and which ones are not.


#warning 
#error
 
When preprocessor finds these directives, it will print the message into the Arduino IDE console. The difference between the two is that after #warning, compilation proceeds as normal, while #error stops the compilation altogether.
 
 
#ifdef DEBUG
#if DEBUG

#else
 
 
links
+ https://www.youtube.com/watch?v=kBTM0dLzNXk
 
 ---------------------------------
-------------------------------------  

Sunday, 2 April 2017

i2c LCD display with rotary encoder 1

 My first test of trying a rotary encoder with a LCD display.
Many thanks to John Miller for his excellent video.
Project Source: https://github.com/jpsrmiller/rotary-encoder-i2c-lcd
 

I've had to adjust the code a bit along with the wiring of the encoder as I didn't have a ky-040 encoder
The KY-040 has a breakout board which adds a Vcc pin
The  virgin encoder doesn't have this.
 
It has 5 pins:
1. CLK - Clock (Out B)
2. DT - data (Out A)
3. SW - switch (when we press the shaft, the switch connects to GND. Otherwise it is floating.)
4. GND
5. GND
 

You most often will find encoders without the breakout board.
 
They still have 5 pins.
Here there are two GNDs
There is no Vcc
 
 
 
 
 
 
The bare Rotary Encoder is connected to the following Arduino Uno pins
//      o CLK (out B) --> Pin 2
//      o DT  (Out A) --> Pin 3
//      o SW  --> Pin 4
//      o GND --> Pin 6 & GND on arduino
 

Here is my adapted code:

// ********************************************
 
 

 
// *************************************************************************************
// LCDRotaryTest16x2.ino - Test Program for Character LCD with I2C and KY-040 Rotary Encoder
//    Author:         John Miller
//    Revision:       1.0.1
//    Date:           5/25/2020
//    Project Source: https://github.com/jpsrmiller/rotary-encoder-i2c-lcd
//
// This Arduino program provides an example for using a KY-040 Rotary Encoder and
//   a 16x2 or 20x4 Character LCD with I2C interface
//
// The program may be freely copied, modified, and used for any Arduino projects
//
// The KY-040 Rotary Encoder is connected to the following Arduino Uno pins
//      o CLK --> Pin 2
//      o DT  --> Pin 3
//      o SW  --> Pin 4
//      o +   --> Pin 5
//      o GND --> Pin 6
//
// The LCD is connected to the following Arduino Uno pins
//      o GND --> GND
//      o VCC --> 5V
//      o SDA --> Pin A4 (SDA)
//      o SCL --> Pin A5 (SCL)
//
// The Rotary Encoder uses Arduino pins for the GND and +5V so that the remaining
//   5V and GND pins on the Arduino Uno can be used for other peripheral devices
// This works because the the Rotary Encoder draws less than the 40 mA
//   maximum current allowed on the Arduino Uno I/O pins  
//
// *** External Libraries Required ***
// The following libraries must be downloaded and copied into the Arduino "libraries"
// folder in order for the program to compile:
//    o OneButton - https://github.com/mathertel/OneButton
//    o LiquidCrystal_I2C - https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
//
// *********************************************************************************

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

// Define the IO Pins Used
#define PIN_ROTARY_CLK    2   // Used for generating interrupts using CLK signal
#define PIN_ROTARY_DAT    3   // Used for reading DT signal
#define PIN_ROTARY_SW     4   // Used for the Rotary push button switch
#define PIN_ROTARY_5V     5   // Set to HIGH to be the 5V pin for the Rotary Encoder
#define PIN_ROTARY_GND    6   // Set to LOW to be the GND pin for the Rotary Encoder

// Most I2C LCD's have an I2C Address of either 0x27 or 0x3F
// If the LCD doesn't work with one address, try the other
//#define LCD_I2C_ADDRESS 0x27
//#define LCD_I2C_ADDRESS     0x3F

// Define the size of the LCD.  Most LCD's are either 16x2 or 20x4
//#define LCD_ROW_COUNT       2    // Number of Rows
//#define LCD_COL_COUNT       16   // Number of Characters per Row

// OneButton class handles Debounce and detects button press
OneButton btnRot(PIN_ROTARY_SW, HIGH);      // Rotary Select button

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// LiquidCrystal_I2C lcd(LCD_I2C_ADDRESS, LCD_COL_COUNT, LCD_ROW_COUNT);

// Used for the Rotary Encoder interrupt routines PinA() and PinB()
volatile int rotaryCount = 0;

// Disables the Rotary Encoder interrupts while the LCD is being updated
byte rotaryDisabled;

volatile byte aFlag = 0; // lets us know when we're expecting a rising edge on pinA
             // to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // lets us know when we're expecting a rising edge on pinB
             // to signal that the encoder has arrived at a detent
             // (opposite direction to when aFlag is set)
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt
               // pins before checking to see if we have moved a whole detent

// ****************************************************************************
// PinA() - Called by the Interrupt pin when the Rotary Encoder Turned
//    Routine based on Simon Merrett - Improved Arduino Rotary Encoder Reading  
//    https://www.instructables.com/id/Improved-Arduino-Rotary-Encoder-Reading/
// ****************************************************************************
void PinA() {

  if (rotaryDisabled) return;

  cli(); //stop interrupts happening before we read pin values
       // read all eight pin values then strip away all but pinA and pinB's values
  reading = PIND & 0xC;

  //check that both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
  if (reading == B00001100 && aFlag) {
    rotaryRight();
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  //signal that we're expecting pinB to signal the transition to detent from free rotation
  else if (reading == B00000100) bFlag = 1;
  sei(); //restart interrupts
}

// ****************************************************************************
// PinB() - Called by the Interrupt pin when the Rotary Encoder Turned
//    Routine based on Simon Merrett - Improved Arduino Rotary Encoder Reading  
//    https://www.instructables.com/id/Improved-Arduino-Rotary-Encoder-Reading/
// ****************************************************************************
void PinB() {

  if (rotaryDisabled) return;

  cli(); //stop interrupts happening before we read pin values
       //read all eight pin values then strip away all but pinA and pinB's values
  reading = PIND & 0xC;
  //check that both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
  if (reading == B00001100 && bFlag) {
    rotaryLeft();
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  //signal that we're expecting pinA to signal the transition to detent from free rotation
  else if (reading == B00001000) aFlag = 1;
  sei(); //restart interrupts
}

// ****************************************************************************
// rotaryRight() - Rotary Encoder is turned 1 detent to the Right (clockwise)
//              Insert your own code here.
// ****************************************************************************
void rotaryRight()
{
  rotaryCount++;
}

// **********************************************************************************
// rotaryLeft() - Rotary Encoder is turned 1 detent to the Left (counter-clockwise)
//                Insert your own code here.
// **********************************************************************************
void rotaryLeft()
{
  rotaryCount--;
}

// ****************************************************************************
// rotaryClick() - Rotary Encoder Select Switch is pressed
//                 Insert your own code here.
// ****************************************************************************
void rotaryClick()
{
  rotaryCount += 100;
}

// ****************************************************************************
// rotaryLongPress() - Rotary Encoder Select Switch is Held Down (Long Press)
//                     Insert your own code here.
// ****************************************************************************
void rotaryLongPress()
{
  rotaryCount = 0;
}

// ****************************************************************************
// initializeRotaryEncoder() - Initialize the pins and interrupt functions
//                             for the Rotary Encoder
// ****************************************************************************
void initializeRotaryEncoder()
{
  // Set the Directions of the I/O Pins
  pinMode(PIN_ROTARY_CLK, INPUT_PULLUP);
  pinMode(PIN_ROTARY_DAT, INPUT_PULLUP);
  pinMode(PIN_ROTARY_SW, INPUT_PULLUP);
  pinMode(PIN_ROTARY_GND, OUTPUT);
  pinMode(PIN_ROTARY_5V, OUTPUT);

  // Set the 5V and GND pins for the Rotary Encoder
  digitalWrite(PIN_ROTARY_GND, LOW);
  digitalWrite(PIN_ROTARY_5V, HIGH);

  // set an interrupt on PinA and PinB, looking for a rising edge signal and
  // executing the "PinA" and "PinB" Interrupt Service Routines
  attachInterrupt(0, PinA, RISING);
  attachInterrupt(1, PinB, RISING);

  // Define the functions for Rotary Encoder Click and Long Press
  btnRot.attachClick(&rotaryClick);
  btnRot.attachLongPressStart(&rotaryLongPress);
  btnRot.setPressTicks(2000);

  rotaryDisabled = 0;
}

// ****************************************************************************
// initializeLcd() - Initialize the LCD
// ****************************************************************************
void initializeLcd()
{
  lcd.begin(16,2);
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(F("Test LCD & Rotry"));
}

// ****************************************************************************
// updateLcd() - Update the LCD with current Rotary Encoder detent count
// ****************************************************************************
void updateLcd()
{
  rotaryDisabled = 1;
  lcd.setCursor(0, 1);
  lcd.print(F("Count = "));
  lcd.print(rotaryCount);
  lcd.print(F("       "));
  rotaryDisabled = 0;
}

// ****************************************************************************
// setup() - Initialization Function
// ****************************************************************************
void setup()
{
  initializeRotaryEncoder();
  initializeLcd();
}

// ****************************************************************************
// loop() - Main Program Loop Function
// ****************************************************************************
void loop()
{
  updateLcd();
  btnRot.tick();
  delay(50);
}

// ***********************************
 

 
Links
 
 
 
 ---------------------------------
-------------------------------------

Saturday, 1 April 2017

I2C LCD display - arduino

 

 This is not your usual LCD.
It's a 1602 16X2 LCD Display IIC/I2C/TWI/SPI Serial Interface Module For Arduino
 It has a backlight and is ideal for displaying text characters... 32 ASCII in 2 rows.
There is room for 16 characters in each row.
Each character is made up of a tiny grid of 5x8 pixels.

There is also a 20 character by 4 lines version

The pin resources of many Arduinos are limited.
I'm using a Uno.
Sometimes the project may be not able to use a normal LCD once it's  connected with sensors, etc. Sometimes, you use up to 8 pins.
 
However, with this I2C interface LCD module, you can display data with only 2 wires..... I2C (and of course Vcc & Gnd)

The I2C is a type of serial bus developed by Philips, which uses two bidirectional lines, called SDA (Serial Data Line) and SCL (Serial Clock Line).  
 
 
The supply voltage is 5V 


Many adapters use a 8-bit PCF8574 chip from Texas Instruments which does the i2c data conversions.
Here is the datasheet:
 
This particular adapter is from NXP Semiconductors.
Here is their datasheet.
 

 There are 4 connections
GND
Vcc - 5V
SDA - serial data  (A4 on the Uno)
SCL - serial clock (A5 on the Uno)

The trimpot adjusts the LCD contrast.




Note the jumper on the left of the board. This supplies power to the backlight. 
 
If you wish to control the intensity of the backlight,  remove the jumper and apply a control voltage to the pin that is marked as ‘LED’.
 
 
Before you can use it you need to find the i2c address of the display
Here is a simple piece of code to upload to the IDE

scanner code
********************************************
Thanks to Nick Gammon.
 
  1. // --------------------------------------
  2. // i2c_scanner
  3. //
  4. // Version 1
  5. //    This program (or code that looks like it)
  6. //    can be found in many places.
  7. //    For example on the Arduino.cc forum.
  8. //    The original author is not know.
  9. // Version 2, Juni 2012, Using Arduino 1.0.1
  10. //     Adapted to be as simple as possible by Arduino.cc user Krodal
  11. // Version 3, Feb 26  2013
  12. //    V3 by louarnold
  13. // Version 4, March 3, 2013, Using Arduino 1.0.3
  14. //    by Arduino.cc user Krodal.
  15. //    Changes by louarnold removed.
  16. //    Scanning addresses changed from 0...127 to 1...119,
  17. //    according to the i2c scanner by Nick Gammon
  18. //    https://www.gammon.com.au/forum/?id=10896
  19. // Version 5, March 28, 2013
  20. //    As version 4, but address scans now to 127.
  21. //    A sensor seems to use address 120.
  22. // Version 6, November 27, 2015.
  23. //    Added waiting for the Leonardo serial communication.
  24. //
  25. //
  26. // This sketch tests the standard 7-bit addresses
  27. // Devices with higher bit address might not be seen properly.
  28. //
  29.  
  30. #include <Wire.h>
  31.  
  32.  
  33. void setup()
  34. {
  35.   Wire.begin();
  36.  
  37.   Serial.begin(9600);
  38.   while (!Serial);             // Leonardo: wait for serial monitor
  39.   Serial.println("\nI2C Scanner");
  40. }
  41.  
  42.  
  43. void loop()
  44. {
  45.   byte error, address;
  46.   int nDevices;
  47.  
  48.   Serial.println("Scanning...");
  49.  
  50.   nDevices = 0;
  51.   for(address = 1; address < 127; address++ )
  52.   {
  53.     // The i2c_scanner uses the return value of
  54.     // the Write.endTransmisstion to see if
  55.     // a device did acknowledge to the address.
  56.     Wire.beginTransmission(address);
  57.     error = Wire.endTransmission();
  58.  
  59.     if (error == 0)
  60.     {
  61.       Serial.print("I2C device found at address 0x");
  62.       if (address<16)
  63.         Serial.print("0");
  64.       Serial.print(address,HEX);
  65.       Serial.println("  !");
  66.  
  67.       nDevices++;
  68.     }
  69.     else if (error==4)
  70.     {
  71.       Serial.print("Unknown error at address 0x");
  72.       if (address<16)
  73.         Serial.print("0");
  74.       Serial.println(address,HEX);
  75.     }    
  76.   }
  77.   if (nDevices == 0)
  78.     Serial.println("No I2C devices found\n");
  79.   else
  80.     Serial.println("done\n");
  81.  
  82.   delay(5000);           // wait 5 seconds for next scan
  83. }
 
// ************************************
Another option is to hardwire the address.
This is useful if you have multiple devices. You can set a different I2C address so that
it doesn't conflict  with any other I2C devices.

The back of the module has these solder pads:

A0, A1, A2
Short these jumpers to set the address.
The jumpers take two inputs .. HIGH or LOW
We can create 8 (23) different combinations.
 
By default on Texas chips, all the 3 address inputs are pulled HIGH using onboard pullups, giving their PCF8574 a default I2C address of 0100111Binary or 0x27Hex.

  Ther three address selection bits (A0, A1 and A2) are placed at the end of the 7-bit I2C address register.
X,X,X,X,A0,A1,A2
 
On a NXP chip, 
all the 3 address inputs are pulled HIGH using onboard pullups, giving their PCF8574 a default I2C address of 0111111Binary or 0x3FHex.
 
*************************************** 

My LCD displays address was 0x3F

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
 // Change the 0x3F  i2c address to your i2c 
// LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);



 You need this library:
 
 The LiquidCrystal_I2C library works in combination with the Wire.h library which allows you to communicate with I2C devices.
 
// **********************************************************


 
//code 1..Hello World

// include the library code:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
 // Change the 0x3F  i2c address to your i2c 
// LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.clear();
  // Print a message to the LCD.
  lcd.print("hello, world!");
}

void loop() {
 
}
 
// **************************************************************** 
// Example 3
// hELLO synth nerds
// Moving text on the two lines:

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // set the LCD address to 0x3F for a 16 chars and 2 line display

void setup() {
    lcd.begin(16, 2);
  lcd.clear();         
  lcd.backlight();      // Make sure backlight is on
 
  // Print a message on both lines of the LCD.
  lcd.setCursor(2,0);   //Set cursor to character 2 on line 0
  lcd.print("Hello Synth");
 
  lcd.setCursor(5,1);   //Move cursor to character 5 on line 1
  lcd.print("Nerds !");
}

void loop() {
}
// *****************************************


Example 4 - creating symbols
I love synthesizers 1
 The heart symbol has been made with the online LCD symbol generator:
 https://maxpromer.github.io/LCD-Character-Creator/ 
 
// *********************************
 
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

byte heart[] = {
  0x00,
  0x0A,
  0x1F,
  0x1F,
  0x0E,
  0x04,
  0x00,
  0x00
};
 
void setup() {
 lcd.begin(16, 2);
  lcd.backlight();             // turn backlight on
  lcd.clear();                 // clear the screen

  lcd.createChar(1, heart);    // define a symbol for memory position 1
  lcd.setCursor(0, 1);         // set the cursor to position 1, line 2
  lcd.print("I ");           // write on the screen
  lcd.write(1);                // write symbol from memory position 1
  lcd.print(" Synthesizers"); // write on the screen
}
 
void loop() {}

// ***********************************
I love synthesizers 2
 The EQ symbol has been made with the online LCD symbol generator:
 https://maxpromer.github.io/LCD-Character-Creator/ 
 

 
 
// ********************************* 

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// RS, E, D4, D5, D6, D7

byte customChar[] = {
  0x00,
  0x02,
  0x02,
  0x0A,
  0x0B,
  0x1B,
  0x1F,
  0x1F
};

void setup() {
 lcd.begin(16, 2);
  lcd.backlight();             // turn backlight on
  lcd.clear();                 // clear the screen

  lcd.createChar(1, customChar);    // define a symbol for memory position 1
  lcd.setCursor(0, 1);         // set the cursor to position 1, line 2
  lcd.print("I ");           // write on the screen
  lcd.write(1);                // write symbol from memory position 1
  lcd.print(" Synthesizers"); // write on the screen
}
 
void loop() {}



Links
 

 ---------------------------------
-------------------------------------