Ok so I have done a simple project on this topic.
My Coding has no errors.
But my LED does not randomly blink or it doesn't even blink as it was supposed to do.
Hoping if someone can find out what is wrong with my coding or my hardware wiring.
Below is a picture of my hardware and code.
#include <Arduino.h>
#include <a_i2c.h>
#include <Wire.h>
const int LED1 = 13;
const int LED2 = 12;
const int LED3 = 11;
const int LED4 = 10;
const int LED5 = 9;
const int maxLedPins = 5;
int ledpins[maxLedPins] = {LED1, LED2, LED3, LED4, LED5};
void setup () {
Serial.begin(57600);
i2cInit();
// Set all pins to output
for (int i=0; i < maxLedPins; i++) {
pinMode(ledpins[i], OUTPUT);
}
}
void loop () {
// Turn off all leds
for (int i=0; i < maxLedPins; i++) {
digitalWrite (ledpins[i], LOW);
}
// Turn on random pin
digitalWrite (ledpins[random(maxLedPins)], HIGH);
Serial.println(ledpins[random(maxLedPins)]);
delay (1000);
}
In your loop-code, you're addressing 5 arduino-pins, 9-13, but you have no leds attached to those pins.
You did connect the leds to your PCF8574, you also seem to initialize I2C-communications, but you're not commanding
the IC to do anything after the set-up code.
It doesn't exactly do what you want, but here's an example how you can communicate with an 8574-chip.
/*
Test program for PCF8574 I2C I/O expander
*/
#include <Wire.h>
#define expander B0100000 // Address of 8574-chip when all address-pins are connected with ground
void setup()
{
Wire.begin();
Serial.begin(9600);
}
void loop()
{
Serial.println("Writing B00000000.");
expanderWrite(B00000000);
Serial.print("Read: ");
Serial.println(expanderRead(), BIN);
delay(1000);
Serial.println("Writing B11111111.");
expanderWrite(B11111111);
Serial.print("Read: ");
Serial.println(expanderRead(), BIN);
delay(1000);
}
void expanderWrite(byte _data )
{
Wire.beginTransmission(expander);
Wire.write(_data);
Wire.endTransmission();
}
byte expanderRead()
{
byte _data;
Wire.requestFrom(expander, 1);
if(Wire.available())
{
_data = Wire.read();
}
return _data;
}
You will need to connect the Address-pins of your PCF8574P to ground for this example (or change the address in the code).
Here's a crude version of what you want.
The PCF8574 is a relative simple chip when writing software. It has 8 IO-pins and if you send a byte (8-bits), each
pin will respond accordingly. If you command it to read a byte, you can check which pins are high/low by breaking down the byte in bits.
So, if you send a random byte to the IC , all leds will turn on/off at random.
/*
Test program for PCF8574 I2C I/O expander
*/
#include <Wire.h>
#define expander B0100000 // Address of 8574-chip when all address-pins are connected with ground
byte randomnumber;
void setup()
{
Wire.begin();
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop()
{
Serial.print("Random number for this second : ");
randomnumber = random(0,256);
Serial.println(randomnumber);
expanderWrite(randomnumber);
delay(1000);
}
void expanderWrite(byte _data )
{
Wire.beginTransmission(expander);
Wire.write(_data);
Wire.endTransmission();
}
If you want to light 1 out of 5 leds at random, you will need to send the right number, (1,2,4,8,16,32,etc) to turn on only 1 led.
Here's a crude version to light 1 out of 5 at random.:
#include <Wire.h>
#define expander B0100000 // Address of 8574-chip when all address-pins are connected with ground
byte randomnumber;
void setup()
{
Wire.begin();
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop()
Serial.print("Random pin for this second : ");
randomnumber = random(1,6);
Serial.println(randomnumber);
switch (randomnumber) {
case 1:
expanderWrite(1);
break;
case 2:
expanderWrite(2);
break;
case 3:
expanderWrite(4);
break;
case 4:
expanderWrite(8);
break;
case 5:
expanderWrite(16);
break;
default:
// if nothing else matches, do the default
// default is optional
}
delay(1000);
}
void expanderWrite(byte _data )
{
Wire.beginTransmission(expander);
Wire.write(_data);
Wire.endTransmission();
}
In the setup routine I added the command randomseed(analogRead(0)) by the way. The random function isn't as random as it should be, without seeding your board a random number first it will pick the same numbers every time your arduino is started.
By reading an analog pin (with nothing attached !), you can get such a number.