I am hoping someone might have an idea of what is going wrong.
The code below, will allow the HM10 access my iPhone Arduino Manager app, but it will send the data, but any data from the widgets on the app, ie the servo, or the light switch, will not work. It appears it's an issue with the processIncomingMessages() function. I am at a loss.
I am not a programmer, but it seems to be a problem in the library, as the hardware works fine if i code a simple sketch to send and receive a text through the serial monitors. The library for the R4 BLE works fine, as I was able to make everything function, but want to be able to use the ATmega 328 in a project, rather than the R4.
Any input or ideas is welcome.
#include <SPI.h>
#include <Servo.h>
#include <SD.h>
#include <AM_HM10.h>
//#include <AM_macOSSerial.h>
//#include <SoftwareSerial.h>
//SoftwareSerial BT(7, 8); // RX, TX
/**
*
- Other initializations
*/
#define CONNECTIONPIN 2
#define GREENLEDPIN 6
#define YELLOWLEDPIN 3
int yellowLed = HIGH;
#define TEMPERATUREPIN 0
float temperature;
#define LIGHTPIN 1
int light;
#define POTENTIOMETERPIN 2
int pot;
#define SERVOPIN 9
Servo myservo;
int servoPos;
/*
*
- Prototypes of AMController’s callbacks
*/
void doWork();
void doSync();
void processIncomingMessages(char *variable, char *value);
void processOutgoingMessages(char *variable, char *value);
void processAlarms(char *variable);
void deviceConnected(char *variable, char *value);
void deviceDisconnected(char *variable, char *value);
/*
*
- AMController Library initialization
*/
#ifdef ALARMS_SUPPORT
AMController amController(&doWork,&doSync,&processIncomingMessages,&processOutgoingMessages,&processAlarms,&deviceConnected,&deviceDisconnected);
#else
AMController amController(&doWork,&doSync,&processIncomingMessages,&processOutgoingMessages,&deviceConnected,&deviceDisconnected);
#endif
void setup() {
Serial.begin(9600);
#if defined(SD_SUPPORT) || defined(SDLOGGEDATAGRAPH_SUPPORT)
if (!SD.begin(5)) {
Serial.println("SD initialization failed!");
return;
}
Serial.println("SD initialized");
#endif
/**
*
- Other initializations
*/
// Yellow LED on
pinMode(YELLOWLEDPIN,OUTPUT);
digitalWrite(YELLOWLEDPIN,yellowLed);
// Connection LED off
pinMode(CONNECTIONPIN,OUTPUT);
digitalWrite(CONNECTIONPIN,LOW);
// Green LED off
pinMode(GREENLEDPIN,OUTPUT);
digitalWrite(GREENLEDPIN,LOW);
// Servo position at 90 degrees
myservo.attach(SERVOPIN);
servoPos = 90;
myservo.write(servoPos);
Serial.println("Setup completed");
}
/**
*
- Standard loop function
*/
void loop() {
amController.loop(10);
}
/**
*
*
- This function is called periodically and its equivalent to the standard loop() function
*/
void doWork() {
temperature = getVoltage(TEMPERATUREPIN); //getting the voltage reading from the temperature sensor
temperature = (temperature - 0.5) * 100; // converting from 10 mv per degree with 500 mV offset
// to degrees ((voltage – 500mV) times 100
digitalWrite(YELLOWLEDPIN,yellowLed);
myservo.write(servoPos);
light = analogRead(LIGHTPIN);
pot = analogRead(POTENTIOMETERPIN);
}
/**
*
*
- This function is called when the ios device connects and needs to initialize the position of switches and knobs
*/
void doSync () {
Serial.print("doSync");
digitalWrite(CONNECTIONPIN,HIGH);
amController.writeMessage("Knob1", (float)map(myservo.read(), 0, 180, 0, 1023));
amController.writeMessage("S1",digitalRead(YELLOWLEDPIN));
amController.writeTxtMessage("Msg","Hello, I'm your Arduino board");
}
/**
*
*
- This function is called when a new message is received from the iOS device
*/
void processIncomingMessages(char *variable, char *value) {
if (strcmp(variable,"S1")==0) {
yellowLed = atoi(value);
Serial.println(yellowLed);
}
if (strcmp(variable,"Knob1")==0) {
servoPos = atoi(value);
servoPos = map(servoPos, 0, 1023, 0, 180);
Serial.println(servoPos);
}
if (strcmp(variable,"Push1")==0 && atoi(value) == 1) {
amController.logLn("Push1");
digitalWrite(GREENLEDPIN,HIGH);
}
if (strcmp(variable,"Push1")==0 && atoi(value) == 0) {
amController.logLn("Push1");
digitalWrite(GREENLEDPIN,LOW);
}
if (strcmp(variable,"Cmd_01")==0) {
amController.log("Command: ");
amController.logLn(value);
}
if (strcmp(variable,"Cmd_02")==0) {
amController.log("Command: ");
amController.logLn(value);
}
}
/**
*
*
- This function is called periodically and messages can be sent to the iOS device
*/
void processOutgoingMessages() {
amController.writeMessage("T",temperature);
amController.writeMessage("L",light);
amController.writeMessage("Led13",yellowLed);
amController.writeMessage("Pot",pot);
}
/**
*
*
- This function is called when an Alarm is fired
*/
void processAlarms(char *alarm) {
Serial.print("Alarm ");
Serial.print(alarm);
Serial.println(" fired");
servoPos = 0;
}
/**
*
*
- This function is called when the iOS device connects
*/
void deviceConnected() {
digitalWrite(CONNECTIONPIN,HIGH);
Serial.println("Device connected");
}
/**
*
*
- This function is called when the iOS device disconnects
*/
void deviceDisconnected() {
digitalWrite(CONNECTIONPIN,LOW);
Serial.println("Device disconnected");
}
/**
*
- Auxiliary functions
*/
/*
- getVoltage() – returns the voltage on the analog input defined by pin
*/
float getVoltage(int pin) {
return (analogRead(pin) * .004882814); // converting from a 0 to 1023 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}