Using Arduino + Sparkfun SM5100B + Cellular Shield.
I need the following code to loop trough a set of mobile numbers with about a minute delay for each. If the Arduino receives a positive/'yes' message I need this to break the loop and end it. If it receives a 'no' or a negative message I need it to continue its loop.
Right now, I have it successfully compiling, but while it delays correctly in the loop and prints the correct messages in the serial monitor, it actually only sends a test message to the first number along with the AT commands to send to the next number (in the text message received by the first number) in this code I have included the receive message code yet, as I would like to get the loop working first before I try to break it.
//THis code is sending both messages to first number
#include <GSM.h>
#define PINNUMBER ""
const int buttonPin = 4;
const int ledPin1 = 13;
const int ledPin2 = 12;
int buttonState = 0;
// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;
// char array of the telephone number to send SMS
const char* number1 = "xxxxxxxxxxxxx"; // enter any two mobile numbers here in international format
const char* number2 = "xxxxxxxxxxxxx";
// char array of the message
char txtMsg[200]="test";
// connection state
boolean notConnected = true;
void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT);
// initialize serial communications
Serial.begin(9600);
Serial.println("SMS Messages Sender");
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop()
{
buttonState = digitalRead(buttonPin);
{
sendSMS();
}
}
void sendSMS()
{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
if(buttonState == HIGH) {
buttonState = 1;
};
if (buttonState == 1) {
digitalWrite(ledPin2, HIGH);
delay(1000);
Serial.print("Message to mobile number: ");
Serial.println(number1);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(number1);
sms.print(txtMsg);
Serial.println("\nFirst Message Sent\n");
digitalWrite(ledPin2, LOW);
delay (60000);
digitalWrite(ledPin2, HIGH);
delay(1000);
Serial.print("Message to mobile number: ");
Serial.println(number2);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(number2);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nSecond Message Sent\n");
digitalWrite(ledPin2, LOW);
}
}
Change GSM_sms;
to
GSM_smsone;
GSM_smstwo;
use
smsone.beginSMS(number1);
smsone.print(txtMsg);
smsone.endSMS();
smstwo.beginSMS(number2);
smstwo.print(txtMsg);
smstwo.endSMS();
for sending the sms.
#include <GSM.h>
#define PINNUMBER ""
const int buttonPin = 4;
const int ledPin1 = 13;
const int ledPin2 = 12;
int buttonState = 0;
// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS smsone;
GSM_SMS smstwo;
// char array of the telephone number to send SMS
const char* number1 = "xxxxxxxxxxxxx"; // enter any two mobile numbers here in international format
const char* number2 = "xxxxxxxxxxxxx";
// char array of the message
char txtMsg[200] = "test";
// connection state
boolean notConnected = true;
void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT);
// initialize serial communications
Serial.begin(9600);
Serial.println("SMS Messages Sender");
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while (notConnected)
{
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop(){
buttonState = digitalRead(buttonPin);
{
sendSMS();
}
}
void sendSMS()
{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
if (buttonState == HIGH) {
buttonState = 1;
};
if (buttonState == 1) {
digitalWrite(ledPin2, HIGH);
delay(1000);
Serial.print("Message to mobile number: ");
Serial.println(number1);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the first message
smsone.beginSMS(number1);
smsone.print(txtMsg);
smsone.endSMS();
Serial.println("\nFirst Message Sent\n");
digitalWrite(ledPin2, LOW);
delay (60000);
digitalWrite(ledPin2, HIGH);
delay(1000);
Serial.print("Message to mobile number: ");
Serial.println(number2);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the second message
smstwo.beginSMS(number2);
smstwo.print(txtMsg);
smstwo.endSMS();
Serial.println("\nSecond Message Sent\n");
digitalWrite(ledPin2, LOW);
}
}
Related
I'm trying to stream voice to the MQTT broker. I'm using esp8266(NodeMCU1.0) and it's ADC to sampling the audio signal at 4KHz and 8-bit PCM audio format. I used Pubsubclient.h library to publish audio packets to the broker but on the other side when I receive packets and play them I have the interrupted voice and I have a delay between playing packets as long as my buffer size. please help me to have continues voice.
I have 2 main problems:
1) delay between playing packets as long as my buffer size
example of what i received: ;D ;D ;D .....|||||..........|||||||...........|||||||||............||||||||
2) Quality of voice: I receive a noisy voice.
Please help me! Thanks.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "Knight";
const char* password = "****";
const char* mqtt_server = "104.21.218.224";
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (4096)
int i;
byte voice[MSG_BUFFER_SIZE];
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ashkan";
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
if (!client.publish("doorphone/connected", "true")) {
Serial.println("publish failed, either connection lost, or message too large");
}
else {
Serial.println("publish succeeded");
}
// ... and resubscribe
// client.subscribe("doorphone/open");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
for (i = 0; i < MSG_BUFFER_SIZE; i++) {
voice[i] = analogRead(A0);
// client.write(voice[i]);
delayMicroseconds(125);
}
client.publish("doorphone/connected", voice, MSG_BUFFER_SIZE);
}
This is my code
#include <WiFi.h>
//#include "StepperMotor.h"
#define SDA_PIN 4
#define SCL_PIN 5
const char* ssid = "StepperMotorWireless";
const char* password = "";
WiFiServer server(9090);
WiFiClient client;
char buffer[50] = {0};
int buffercounter = 0;
TaskHandle_t StepperHandler;
TaskHandle_t WifiHandler;
void setup()
{
Serial.begin(9600);
SetWifi(ssid, password);
pinMode(2, OUTPUT);
xTaskCreatePinnedToCore(
StepperLoop, /* Task function. */
"StepperHandler", /* name of task. */
4000, /* Stack size of task */
NULL, /* parameter of the task */
2, /* priority of the task */
&StepperHandler, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
}
void SetWifi(const char* name, const char* password)
{
WiFi.disconnect();
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(name, password);
delay(2000);
IPAddress IP = WiFi.softAPIP();
Serial.print("Server IP : ");
Serial.println(IP);
server.begin();
server.setNoDelay(true);
Serial.println("Server Started");
}
void availableMessage() {
if (client && client.connected() && client.available()) {
while (client.available()) {
String message = client.readStringUntil('\n');
Serial.println(message);
char temp[50];
message.toCharArray(temp, 50);
client.flush();
}
}
}
void connectClient() {
if (server.hasClient())
{
if (client = server.available()) {
Serial.println("connected");
}
}
}
void StepperLoop( void * pvParameters ) {
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
}
void loop()
{
connectClient();
availableMessage();
}
The problem is that my blinking LED is not 1 second long, so something is interrupting it (appearantly wifi is because that is the only other thing running).
That is weird because every on the internet you look, the Wifi task is pinned to Core 0, and I am running my blink code on Core 1, so that should not be interfering.
I cant seem to fix this, could someone help me out?
BTW I am using arduino IDE
What does not blinking 1 second long mean? Have you measured the time or could you see it directly?
Your LED/Stepper loop is not a loop! If you what the LED to blink more than once:
void StepperLoop( void * pvParameters ) {
while (1) {
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
}
}
I am currently involved in a project which needs to have 4 led's and one stepper motor to be turned on/off independently over wifi.
Just to mention Im using an Uno r3, official Wifi shield and official motor shield.
I have the 4 led's working on pins A2-A5 or in my code pins (16-19) leaving free all digital pins and have set up my stepper motor to pins 9,8,6,5. When i go to the ip address i can turn off/on each LED independently but with the stepper motor it either judders and does not move or nothing at all.
I have posted my code so far below, would really appreciate any help or advice. Thanks
/*
WiFi Web Server LED Blink
A simple web server that lets you blink an LED via the web.
This sketch will print the IP address of your WiFi Shield (once connected)
to the Serial monitor. From there, you can open that address in a web browser
to turn on and off the LED on pin 9.
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
Circuit:
* WiFi shield attached
* LED attached to pin 9
created 25 Nov 2012
by Tom Igoe
*/
#include <SPI.h>
#include <WiFi.h>
#include <Stepper.h>
char ssid[] = "username"; // your network SSID (name)
char pass[] = "password"; // your network password
int keyIndex = 0;
int delaylegnth = 7;
int state = 0;
int x = 0;
// your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(16, OUTPUT); // set the LED pin mode
pinMode(17, OUTPUT); // set the LED pin mode
pinMode(18, OUTPUT); // set the LED pin mode
pinMode(19, OUTPUT); // set the LED pin mode
//establish motor direction toggle pins
pinMode(5, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
pinMode(6, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
//establish motor brake pins
pinMode(9, OUTPUT); //brake (disable) CH A
pinMode(8, OUTPUT); //brake (disable) CH B
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while(true); // don't continue
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin(); // start the web server on port 80
printWifiStatus(); // you're connected now, so print out the status
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
String currentLine2 = "";
String currentLine3 = "";
String currentLine4 = "";
String currentLine5 = "";
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click here turn the LED on pin 4 on<br>");
client.print("Click here turn the LED on pin 4 off<br>");
client.print("Click here turn the LED on pin 5 on<br>");
client.print("Click here turn the LED on pin 5 off<br>");
client.print("Click here turn the LED on pin 6 on<br>");
client.print("Click here turn the LED on pin 6 off<br>");
client.print("Click here turn the LED on pin 2 on<br>");
client.print("Click here turn the LED on pin 2 off<br>");
client.print("Click here turn the Stepper<br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
currentLine2 = "";
currentLine3 = "";
currentLine4 = "";
currentLine5 = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c;
currentLine2 += c;
currentLine3 += c;
currentLine4 += c;
currentLine5 += c;
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(16, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(16, LOW); // GET /L turns the LED off
}
if (currentLine2.endsWith("GET /A")) {
digitalWrite(17, HIGH); // GET /A turns the LED on
}
if (currentLine2.endsWith("GET /B")) {
digitalWrite(17, LOW); // GET /B turns the LED off
}
if (currentLine3.endsWith("GET /C")) {
digitalWrite(18, HIGH); // GET /A turns the LED on
}
if (currentLine3.endsWith("GET /D")) {
digitalWrite(18, LOW); // GET /B turns the LED off
}
if (currentLine4.endsWith("GET /E")) {
digitalWrite(19, HIGH); // GET /A turns the LED on
}
if (currentLine4.endsWith("GET /F")) {
digitalWrite(19, LOW); // GET /B turns the LED off
}
}
if (currentLine5.endsWith("GET /G")) { //stepper motor
// for(int x; x < 500; x++) {
// state=digitalRead(0);
// if (state == LOW){
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(5, HIGH); //Sets direction of CH A
analogWrite(A0, 255); //Moves CH A
// }
// else{
delay(delaylegnth);
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(6, LOW); //Sets direction of CH B
analogWrite(A1, 255); //Moves CH B
delay(delaylegnth);
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(5, LOW); //Sets direction of CH A
analogWrite(A0, 255); //Moves CH A
delay(delaylegnth);
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(6, HIGH); //Sets direction of CH B
analogWrite(A1, 255); //Moves CH B
delay(delaylegnth);
// }
// }
}
}
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
So I hacked at a getFlow function for the Arduino sketch I am working on, and I am getting output of the function in the console of the iPhone, but when I hook up the computer to the Arduino, and open the serial monitor I don't see any output from the getFlow4 function. I am using a Swiss flow SF800 flowmeter / flowsensor.
/*
* kegboard-clone-4-KegCop
* This code is public domain
*
* This sketch sends a receives a multibyte String from the iPhone
* and performs functions on it.
*
* This Arduino sketch is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Public License
* along with this sketch. If not, see <http://www.gnu.org/licenses/>.
*
* Examples:
* http://arduino.cc/en/Tutorial/SerialEvent
* http://arduino.cc/en/Serial/read
* http://stackoverflow.com/questions/16532586/arduino-sketch-that-responds-to-certain-commands-how-is-it-done/
* http://davebmiller.wordpress.com/2011/01/18/arduino-flowmeter/
* http://forum.arduino.cc/index.php?topic=52003.0
* http://arduino.cc/en/Reference/AttachInterrupt
*
* TODO:
* - eventually get code working with the SF800 flow sensor / flowmeter
*
*/
// flow_A LED
int led = 4;
// relay_A
const int RELAY_A = A0;
// string / serial event variables
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
boolean valve_open = false;
// FLOWMETER SHIT
// flowmeter 0 pulse (input) = digital pin 2
// https://github.com/Kegbot/kegboard/blob/master/arduino/kegboard/kegboard_config.h
// which pin to use for reading the sensor? kegboard-mini shield has digital pin 2 allocated
// the SF800 outputs 5400 pulses per litre
// The hall-effect flow sensor (SF800) outputs approximately 5400 pulses per second per litre/minute of flow
int sensorInterrupt = 0; // changed from byte
int sensorPin = 2; // changed from byte
int sensorPinState = 0; // variable for storing state of sensor pin
// read RPM
int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
void setup() {
// initialize serial
// Serial.flush(); // flush the serial buffer on setup.
Serial.begin(115200); // open serial port, sets data rate to 9600bps
Serial.println("Power on test");
inputString.reserve(200);
valve_open = false;
// relay for solenoid cut off valve
pinMode(RELAY_A, OUTPUT);
// flowmeter shit
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH); // Need to set these HIGH so they won't just tick away
// The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
// Configured to trigger on a RISING state change (transition from HIGH
// state to LOW state)
attachInterrupt(0, rpm_fan, RISING);
}
void open_valve() {
digitalWrite(RELAY_A, HIGH); // turn RELAY_A on
valve_open = true;
}
void close_valve() {
digitalWrite(RELAY_A, LOW); // turn RELAY_A off
valve_open = false;
}
void flow_A_blink() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for one second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void flow_A_blink_stop() {
digitalWrite(led, LOW);
}
void flow_A_on() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
}
void flow_A_off() {
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
}
// flowmeter shit
void getFlow4() {
// Serial.println("im here");
// Serial.println(sensorPin);
sensorPinState = digitalRead(sensorPin);
// Serial.println(sensorPinState);
if (millis() - lastmillis >= 1000){ //Uptade every one second, this will be equal to reading frecuency (Hz).
detachInterrupt(0);//Disable interrupt when calculating
rpm = rpmcount * 60; // Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.
Serial.print("RPM =\t"); //print the word "RPM" and tab.
Serial.print(rpm); // print the rpm value.
Serial.print("\t Hz=\t"); //print the word "Hz".
Serial.println(rpmcount); //print revolutions per second or Hz. And print new line or enter.
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Uptade lasmillis
attachInterrupt(0, rpm_fan, RISING); //enable interrupt
}
if(sensorPinState == LOW) {
flow_A_off();
Serial.println("don't blink");
}
if(sensorPinState == HIGH) {
flow_A_on();
Serial.println("blink damnit");
}
if(stringComplete) {
if(inputString.equals("{close_valve}\n")) {
// Serial.println("close vavle.");
close_valve();
}
return;
}
}
void rpm_fan(){
rpmcount++;
}
/*
* Main program loop, runs over and over repeatedly
*/
void loop() {
if(stringComplete) {
// Serial.println(inputString);
if(inputString.equals("{open_valve}\n")) {
// Serial.println("inputString equates :)");
open_valve();
}
if(inputString.equals("{close_valve}\n")) {
// Serial.println("close vavle.");
close_valve();
}
if(valve_open) {
// Serial.println("valve_open = true");
inputString = "";
stringComplete = false;
getFlow4();
}
// clear the string:
inputString = "";
stringComplete = false;
}
//Serial.println("over and over");
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while(Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
// Serial.println(inputString.length());
}
}
So I'm trying to count the pulses of a swiss flow SF800 flow sensor / meter in my current Arduino project, but I'm currently not getting any data printed out to the console. My Arduino sketch looks like the following,
// global variables should be identified with _
// flow_A LED
int led = 4;
// relay_A
const int RELAY_A = A0;
// variables from sketch example
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
// FLOWMETER SHIT
// flowmeter 0 pulse (input) = digital pin 2
// https://github.com/Kegbot/kegboard/blob/master/arduino/kegboard/kegboard_config.h
// which pin to use for reading the sensor? kegboard-mini shield has digital pin 2 allocated
# define FLOWSENSORPIN 2
int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
void setup() {
// initialize serial
Serial.begin(9600); // open serial port, sets data rate to 115200bps
inputString.reserve(200);
pinMode(RELAY_A, OUTPUT);
// flowmeter shit
pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH); // Need to set these HIGH so they won't just tick away
attachInterrupt(0, rpm_fan, FALLING); // interrupt is attached, is on pin two(2).
}
void open_valve() {
digitalWrite(RELAY_A, HIGH); // turn RELAY_A on
// Serial.println("Valve Open");
Serial.write("{valve_open}");
}
void close_valve() {
digitalWrite(RELAY_A, LOW); // turn RELAY_A off
// Serial.println("Vavle Closed");
Serial.write("{valve_close}");
}
void flow_A_blink() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for one second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void flow_A_blink_stop() {
digitalWrite(led, LOW);
}
// flowmeter shit
void getFlow() {
Serial.println("reached getFlow function");
if(millis() - lastmillis == 1000) { // Update every one second, this will be equal to reading frequency (Hz).
detachInterrupt(0); // Disable interrupt when calculating
rpm = rpmcount * 60; // Convert frequency to RPM, note: this works for one interruption per full rotation.
Serial.print("RPM =\t"); // print the word "RPM and tabl.
Serial.print(rpm); // print the rpm value
Serial.print("\t Hz=\t"); // print the word "Hz".
Serial.println(rpmcount); // print revolutions per second or Hz. And print new line or enter.
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Update lastmillis
attachInterrupt(0, rpm_fan, FALLING); // enable interrupt
}
}
void rpm_fan() { // this code will be executed every time the interrupt 0 (pin2) gets low.
rpmcount++;
}
/*
* Main program loop, runs over and over repeatedly
*/
void loop() {
if(stringComplete) {
// Serial.println(inputString);
if(inputString.equals("{open_valve}\n")) {
// Serial.println("opening valve.");
open_valve();
getFlow();
}
if(inputString.equals("{close_valve}\n")) {
// Serial.println("close vavle.");
close_valve();
}
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while(Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
// Serial.println(inputString.length());
}
}
The following code changes seem to be printing the RPM and Hz values now.
// flow_A LED
int led = 4;
// relay_A
const int RELAY_A = A0;
// variables from sketch example
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
// FLOWMETER SHIT
// flowmeter 0 pulse (input) = digital pin 2
// https://github.com/Kegbot/kegboard/blob/master/arduino/kegboard/kegboard_config.h
// which pin to use for reading the sensor? kegboard-mini shield has digital pin 2 allocated
# define FLOWSENSORPIN 2
int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
void setup() {
// initialize serial
Serial.begin(9600); // open serial port, sets data rate to 115200bps
inputString.reserve(200);
pinMode(RELAY_A, OUTPUT);
// flowmeter shit
pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH); // Need to set these HIGH so they won't just tick away
attachInterrupt(0, rpm_fan, FALLING); // interrupt is attached, is on pin two(2).
}
void open_valve() {
digitalWrite(RELAY_A, HIGH); // turn RELAY_A on
// Serial.println("Valve Open");
Serial.write("{valve_open}");
}
void close_valve() {
digitalWrite(RELAY_A, LOW); // turn RELAY_A off
// Serial.println("Vavle Closed");
Serial.write("{valve_close}");
}
void flow_A_blink() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for one second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void flow_A_blink_stop() {
digitalWrite(led, LOW);
}
// flowmeter shit
void getFlow() {
// Serial.println("reached getFlow function");
if(millis() - lastmillis >= 1000) { // Update every one second, this will be equal to reading frequency (Hz). Using >= should be safter
// Serial.println("reached inside if statement");
detachInterrupt(0); // Disable interrupt when calculating
rpm = rpmcount * 60; // Convert frequency to RPM, note: this works for one interruption per full rotation.
Serial.print("RPM =\t"); // print the word "RPM and tabl.
Serial.print(rpm); // print the rpm value
Serial.print("\t Hz=\t"); // print the word "Hz".
Serial.println(rpmcount); // print revolutions per second or Hz. And print new line or enter.
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Update lastmillis
attachInterrupt(0, rpm_fan, FALLING); // enable interrupt
}
}
void rpm_fan() { // this code will be executed every time the interrupt 0 (pin2) gets low.
rpmcount++;
}
/*
* Main program loop, runs over and over repeatedly
*/
void loop() {
if(stringComplete) {
// Serial.println(inputString);
if(inputString.equals("{open_valve}\n")) {
// Serial.println("opening valve.");
open_valve();
}
if(inputString.equals("{close_valve}\n")) {
// Serial.println("close vavle.");
close_valve();
}
// clear the string:
inputString = "";
stringComplete = false;
}
getFlow();
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while(Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
// Serial.println(inputString.length());
}
}