Project /
ArduinoSerialShiftRegister
/*
Serial Event example
When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it.
A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences.
Created 9 May 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SerialEvent
*/
#include <Time.h>
#include <Wire.h>
#include <TimeAlarms.h>
#define TIME_HEADER "T"
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
const int ledPin = 12; // the pin number of the LED
const int ledPin2 = 13; // the pin number of the LED2
const int tPin = 11; // the pin for temperature TMP36
int SER_Pin = 8; //pin 14 on the 75HC595
int RCLK_Pin = 9; //pin 12 on the 75HC595
int SRCLK_Pin = 10; //pin 11 on the 75HC595
//How many of the shift registers - change this
#define number_of_74hc595s 1
//do not touch
#define numOfRegisterPins number_of_74hc595s * 8
boolean TimeIsSynced = false;
boolean registers[numOfRegisterPins];
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
boolean offline = true;
void setup() {
const unsigned long DEFAULT_TIME = 1382036603 + (60 * 60 * 2); // Jan 1 2013
setTime(DEFAULT_TIME);
digitalClockDisplay();
// led1
pinMode(ledPin, OUTPUT);
// led2
pinMode(ledPin2, OUTPUT);
Serial.begin(9600); // Initialize serial port
while (!Serial) ; // wait until Arduino Serial Monitor opens
if (Serial.available()) {
Serial.println("Waiting for sync message");
}
Alarm.alarmRepeat(21,5,0, MorningAlarm); // 8:30am every day
Alarm.timerRepeat(60 * 1, AllOff);
Alarm.timerRepeat(60 * 5, AllOn);
// shift register
pinMode(SER_Pin, OUTPUT);
pinMode(RCLK_Pin, OUTPUT);
pinMode(SRCLK_Pin, OUTPUT);
//reset all register pins
clearRegisters();
writeRegisters();
}
void loop()
{
serialReader();
Alarm.delay(1000);
}
void serialReader(){
int makeSerialStringPosition;
int inByte;
char serialReadString[50];
const int terminatingChar = 13; //Terminate lines with CR
inByte = Serial.read();
makeSerialStringPosition=0;
if (inByte > 0 && inByte != terminatingChar) { //If we see data (inByte > 0) and that data isn't a carriage return
delay(100); //Allow serial data time to collect (I think. All I know is it doesn't work without this.)
while (inByte != terminatingChar && Serial.available() > 0){ // As long as EOL not found and there's more to read, keep reading
serialReadString[makeSerialStringPosition] = inByte; // Save the data in a character array
makeSerialStringPosition++; //Increment position in array
if (inByte > 0) Serial.println(inByte); // Debug line that prints the charcodes one per line for everything recieved over serial
inByte = Serial.read(); // Read next byte
}
if (inByte == terminatingChar) //If we terminated properly
{
serialReadString[makeSerialStringPosition] = 0; //Null terminate the serialReadString (Overwrites last position char (terminating char) with 0
if (strcmp(serialReadString, "synctime") == 0) {
Serial.println("reset time synced");
TimeIsSynced = false;
}
if (strcmp(serialReadString, "temp") == 0) {
float temperature = getVoltage(tPin);
temperature = (temperature - .5) * 100;
Serial.println(temperature);
}
if (strcmp(serialReadString, "h") == 0) {
digitalClockDisplay();
}
if (strcmp(serialReadString, "ledon") == 0) digitalWrite(ledPin, HIGH);
if (strcmp(serialReadString, "ledoff") == 0) digitalWrite(ledPin, LOW);
if (strcmp(serialReadString, "l0on") == 0) {
digitalWrite(ledPin, HIGH);
setRegisterPin(0, HIGH);
}
if (strcmp(serialReadString, "l0off") == 0) {
digitalWrite(ledPin, LOW);
setRegisterPin(0, LOW);
}
if (strcmp(serialReadString, "l7on") == 0) {
digitalWrite(ledPin, HIGH);
setRegisterPin(7, HIGH);
}
if (strcmp(serialReadString, "l7off") == 0) {
digitalWrite(ledPin, LOW);
setRegisterPin(7, LOW);
}
if (strcmp(serialReadString, "l2on") == 0) digitalWrite(ledPin2, HIGH);
if (strcmp(serialReadString, "l2off") == 0) digitalWrite(ledPin2, LOW);
if (strcmp(serialReadString, "allon") == 0){
setRegisterPin(0, HIGH);
setRegisterPin(1, HIGH);
setRegisterPin(2, HIGH);
setRegisterPin(3, HIGH);
setRegisterPin(4, HIGH);
setRegisterPin(5, HIGH);
setRegisterPin(6, HIGH);
setRegisterPin(7, HIGH);
}
if (strcmp(serialReadString, "alloff") == 0){
setRegisterPin(0, LOW);
setRegisterPin(1, LOW);
setRegisterPin(2, LOW);
setRegisterPin(3, LOW);
setRegisterPin(4, LOW);
setRegisterPin(5, LOW);
setRegisterPin(6, LOW);
setRegisterPin(7, LOW);
}
writeRegisters(); //MUST BE CALLED TO DISPLAY CHANGES
digitalClockDisplay();
}
}
}
// use for temperature TMP36
float getVoltage(int pin){
return (analogRead(pin) * .004882814);
}
// REGISTER function above
//set an individual register pin HIGH or LOW
void setRegisterPin(int index, int value){
registers[index] = value;
}
//Set and display registers
//Only call AFTER all values are set how you would like (slow otherwise)
void writeRegisters(){
digitalWrite(RCLK_Pin, LOW);
for(int i = numOfRegisterPins - 1; i >= 0; i--){
digitalWrite(SRCLK_Pin, LOW);
int val = registers[i];
digitalWrite(SER_Pin, val);
digitalWrite(SRCLK_Pin, HIGH);
}
digitalWrite(RCLK_Pin, HIGH);
}
// set all register pins to LOW
void clearRegisters(){
for(int i = numOfRegisterPins - 1; i >= 0; i--){
registers[i] = LOW;
}
}
////// Time management
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
// functions to be called when an alarm triggers:
void MorningAlarm(){
Serial.println("Alarm: - turn lights on");
setRegisterPin(0, HIGH);
setRegisterPin(1, HIGH);
setRegisterPin(2, HIGH);
setRegisterPin(3, HIGH);
setRegisterPin(4, HIGH);
setRegisterPin(5, HIGH);
setRegisterPin(6, HIGH);
setRegisterPin(7, HIGH);
writeRegisters();
}
void AllOn(){
setRegisterPin(0, HIGH);
setRegisterPin(1, HIGH);
setRegisterPin(2, HIGH);
setRegisterPin(3, HIGH);
setRegisterPin(4, HIGH);
setRegisterPin(5, HIGH);
setRegisterPin(6, HIGH);
setRegisterPin(7, HIGH);
writeRegisters();
}
void AllOff(){
setRegisterPin(0, LOW);
setRegisterPin(1, LOW);
setRegisterPin(2, LOW);
setRegisterPin(3, LOW);
setRegisterPin(4, LOW);
setRegisterPin(5, LOW);
setRegisterPin(6, LOW);
setRegisterPin(7, LOW);
writeRegisters();
}
void processSyncMessage() {
unsigned long pctime;
const unsigned long DEFAULT_TIME = 1382035561 + (60 * 60 * 2); // Jan 1 2013
setTime(DEFAULT_TIME);
Serial.println("processSyncMessage default");
digitalClockDisplay();
}
time_t requestSync()
{
Serial.write(TIME_REQUEST);
return 0; // the time will be sent later in response to serial mesg
}
Serial Event example
When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it.
A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences.
Created 9 May 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SerialEvent
*/
#include <Time.h>
#include <Wire.h>
#include <TimeAlarms.h>
#define TIME_HEADER "T"
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
const int ledPin = 12; // the pin number of the LED
const int ledPin2 = 13; // the pin number of the LED2
const int tPin = 11; // the pin for temperature TMP36
int SER_Pin = 8; //pin 14 on the 75HC595
int RCLK_Pin = 9; //pin 12 on the 75HC595
int SRCLK_Pin = 10; //pin 11 on the 75HC595
//How many of the shift registers - change this
#define number_of_74hc595s 1
//do not touch
#define numOfRegisterPins number_of_74hc595s * 8
boolean TimeIsSynced = false;
boolean registers[numOfRegisterPins];
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
boolean offline = true;
void setup() {
const unsigned long DEFAULT_TIME = 1382036603 + (60 * 60 * 2); // Jan 1 2013
setTime(DEFAULT_TIME);
digitalClockDisplay();
// led1
pinMode(ledPin, OUTPUT);
// led2
pinMode(ledPin2, OUTPUT);
Serial.begin(9600); // Initialize serial port
while (!Serial) ; // wait until Arduino Serial Monitor opens
if (Serial.available()) {
Serial.println("Waiting for sync message");
}
Alarm.alarmRepeat(21,5,0, MorningAlarm); // 8:30am every day
Alarm.timerRepeat(60 * 1, AllOff);
Alarm.timerRepeat(60 * 5, AllOn);
// shift register
pinMode(SER_Pin, OUTPUT);
pinMode(RCLK_Pin, OUTPUT);
pinMode(SRCLK_Pin, OUTPUT);
//reset all register pins
clearRegisters();
writeRegisters();
}
void loop()
{
serialReader();
Alarm.delay(1000);
}
void serialReader(){
int makeSerialStringPosition;
int inByte;
char serialReadString[50];
const int terminatingChar = 13; //Terminate lines with CR
inByte = Serial.read();
makeSerialStringPosition=0;
if (inByte > 0 && inByte != terminatingChar) { //If we see data (inByte > 0) and that data isn't a carriage return
delay(100); //Allow serial data time to collect (I think. All I know is it doesn't work without this.)
while (inByte != terminatingChar && Serial.available() > 0){ // As long as EOL not found and there's more to read, keep reading
serialReadString[makeSerialStringPosition] = inByte; // Save the data in a character array
makeSerialStringPosition++; //Increment position in array
if (inByte > 0) Serial.println(inByte); // Debug line that prints the charcodes one per line for everything recieved over serial
inByte = Serial.read(); // Read next byte
}
if (inByte == terminatingChar) //If we terminated properly
{
serialReadString[makeSerialStringPosition] = 0; //Null terminate the serialReadString (Overwrites last position char (terminating char) with 0
if (strcmp(serialReadString, "synctime") == 0) {
Serial.println("reset time synced");
TimeIsSynced = false;
}
if (strcmp(serialReadString, "temp") == 0) {
float temperature = getVoltage(tPin);
temperature = (temperature - .5) * 100;
Serial.println(temperature);
}
if (strcmp(serialReadString, "h") == 0) {
digitalClockDisplay();
}
if (strcmp(serialReadString, "ledon") == 0) digitalWrite(ledPin, HIGH);
if (strcmp(serialReadString, "ledoff") == 0) digitalWrite(ledPin, LOW);
if (strcmp(serialReadString, "l0on") == 0) {
digitalWrite(ledPin, HIGH);
setRegisterPin(0, HIGH);
}
if (strcmp(serialReadString, "l0off") == 0) {
digitalWrite(ledPin, LOW);
setRegisterPin(0, LOW);
}
if (strcmp(serialReadString, "l7on") == 0) {
digitalWrite(ledPin, HIGH);
setRegisterPin(7, HIGH);
}
if (strcmp(serialReadString, "l7off") == 0) {
digitalWrite(ledPin, LOW);
setRegisterPin(7, LOW);
}
if (strcmp(serialReadString, "l2on") == 0) digitalWrite(ledPin2, HIGH);
if (strcmp(serialReadString, "l2off") == 0) digitalWrite(ledPin2, LOW);
if (strcmp(serialReadString, "allon") == 0){
setRegisterPin(0, HIGH);
setRegisterPin(1, HIGH);
setRegisterPin(2, HIGH);
setRegisterPin(3, HIGH);
setRegisterPin(4, HIGH);
setRegisterPin(5, HIGH);
setRegisterPin(6, HIGH);
setRegisterPin(7, HIGH);
}
if (strcmp(serialReadString, "alloff") == 0){
setRegisterPin(0, LOW);
setRegisterPin(1, LOW);
setRegisterPin(2, LOW);
setRegisterPin(3, LOW);
setRegisterPin(4, LOW);
setRegisterPin(5, LOW);
setRegisterPin(6, LOW);
setRegisterPin(7, LOW);
}
writeRegisters(); //MUST BE CALLED TO DISPLAY CHANGES
digitalClockDisplay();
}
}
}
// use for temperature TMP36
float getVoltage(int pin){
return (analogRead(pin) * .004882814);
}
// REGISTER function above
//set an individual register pin HIGH or LOW
void setRegisterPin(int index, int value){
registers[index] = value;
}
//Set and display registers
//Only call AFTER all values are set how you would like (slow otherwise)
void writeRegisters(){
digitalWrite(RCLK_Pin, LOW);
for(int i = numOfRegisterPins - 1; i >= 0; i--){
digitalWrite(SRCLK_Pin, LOW);
int val = registers[i];
digitalWrite(SER_Pin, val);
digitalWrite(SRCLK_Pin, HIGH);
}
digitalWrite(RCLK_Pin, HIGH);
}
// set all register pins to LOW
void clearRegisters(){
for(int i = numOfRegisterPins - 1; i >= 0; i--){
registers[i] = LOW;
}
}
////// Time management
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
// functions to be called when an alarm triggers:
void MorningAlarm(){
Serial.println("Alarm: - turn lights on");
setRegisterPin(0, HIGH);
setRegisterPin(1, HIGH);
setRegisterPin(2, HIGH);
setRegisterPin(3, HIGH);
setRegisterPin(4, HIGH);
setRegisterPin(5, HIGH);
setRegisterPin(6, HIGH);
setRegisterPin(7, HIGH);
writeRegisters();
}
void AllOn(){
setRegisterPin(0, HIGH);
setRegisterPin(1, HIGH);
setRegisterPin(2, HIGH);
setRegisterPin(3, HIGH);
setRegisterPin(4, HIGH);
setRegisterPin(5, HIGH);
setRegisterPin(6, HIGH);
setRegisterPin(7, HIGH);
writeRegisters();
}
void AllOff(){
setRegisterPin(0, LOW);
setRegisterPin(1, LOW);
setRegisterPin(2, LOW);
setRegisterPin(3, LOW);
setRegisterPin(4, LOW);
setRegisterPin(5, LOW);
setRegisterPin(6, LOW);
setRegisterPin(7, LOW);
writeRegisters();
}
void processSyncMessage() {
unsigned long pctime;
const unsigned long DEFAULT_TIME = 1382035561 + (60 * 60 * 2); // Jan 1 2013
setTime(DEFAULT_TIME);
Serial.println("processSyncMessage default");
digitalClockDisplay();
}
time_t requestSync()
{
Serial.write(TIME_REQUEST);
return 0; // the time will be sent later in response to serial mesg
}
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Communicate with Arduino via serial,
# sample to send message to arduino.
# messages are read and arduino performs
# correspondant action.
# We need stuff to access devices
import os, sys
# We need time to sleep :p
import time
# We need serial to communicate
import serial
# Ugly connection from Arduino to PC via USB ...
#
# 1 - we sometime lose serial device
# so /dev/ttyACM0 becomes /dev/ttyACM1
s = serial.Serial(port='/dev/ttyACM1', baudrate=9600)
# 2 - we need to give time to arduino after opening port
# 2 s. seems to do the job although it's a bit long
#
# This topic helped me :
# cf : http://forum.arduino.cc/index.php?topic=156248.0
#
time.sleep(2)
# Now we should be able to write to Arduino via serial.
# The message below is: allon
#
# 3 - we need to append \r character to our message.
# Unless arduino could not know that our message is
# ended.
s.write('allon\r')
# Now we close the communication with serial port.
s.close()
# -*- coding: utf-8 -*-
#
# Communicate with Arduino via serial,
# sample to send message to arduino.
# messages are read and arduino performs
# correspondant action.
# We need stuff to access devices
import os, sys
# We need time to sleep :p
import time
# We need serial to communicate
import serial
# Ugly connection from Arduino to PC via USB ...
#
# 1 - we sometime lose serial device
# so /dev/ttyACM0 becomes /dev/ttyACM1
s = serial.Serial(port='/dev/ttyACM1', baudrate=9600)
# 2 - we need to give time to arduino after opening port
# 2 s. seems to do the job although it's a bit long
#
# This topic helped me :
# cf : http://forum.arduino.cc/index.php?topic=156248.0
#
time.sleep(2)
# Now we should be able to write to Arduino via serial.
# The message below is: allon
#
# 3 - we need to append \r character to our message.
# Unless arduino could not know that our message is
# ended.
s.write('allon\r')
# Now we close the communication with serial port.
s.close()