Arduino Uno R3 resets when trying to print to 16x2 LCD - arduino

I'm using an Arduino Uno R3 connected to a 16x2 LCD through I2C. Also using the LiquidCrystal_I2C library to print to the LCD. I recently changed a bunch of my code and this new version of my code makes my Arduino reset as soon as it hits an lcd.print() statement outside void setup(). The code is a work in progress so please bear with any spaghetti code. Here it is:
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <Wire.h>
#include "Time.h"
#include "WiFly.h" //include the WiFly experimental library
#include "Credentials.h"
#define I2C_ADDR 0x27 // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
char msg[320];
char * ptr = &msg[0];
int letterCount = 0;
int countdownFlag = 0;
int cstopFlag = 0;
int lpcount = 0;
void(* resetFunc)(void) = 0; //pointer to reset function # address 0
long days, hrs, mins, sec, rem1, rem2;
long gametime, timediff, new_timediff, current_time;
int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
int flag4 = 0;
int flag5 = 0;
int flag6 = 0;
int flag7 = 0;
char hmTeam[25];
char awTeam[25];
char dateTime[25];
WiFlyClient client("api.football-data.org", 80);
void setup(){
//Begin LCD
lcd.begin (16,2);
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.print("Loading...");
//Begin WiFly and Serial
WiFly.begin();
Serial.begin(9600);
Serial.println("Serial begun :D");
current_time = WiFly.getTime();
Serial.print("connecting to server...");
if(client.connect()){
Serial.println("connected");
client.print("GET http://api.football-data.org/teams/61/fixtures/?timeFrame=n20");
Serial.print("GET http://api.football-data.org/teams/61/fixtures/?timeFrame=n20");
client.println(" HTTP/1.1");
Serial.println(" HTTP/1.1");
client.println("Host: api.football-data.org");
Serial.println("Host: api.football-data.org");
client.println("X-Auth-Token: 4f02cc524412487989ee61aed27503d5");
Serial.println("X-Auth-Token: 4f02cc524412487989ee61aed27503d5");
client.println("Connection: close");
Serial.println("Connection: close");
client.println();
} else{
Serial.println("connection failed");
}
}
void loop(){
if (client.available()) {
char c = client.read();
Serial.print(c);
if(flag4 == 1 && letterCount < 321){
recordMessage(c);
flag5 = 1;
}
if(c == '\n' && !client.available() && flag5 == 1){
char * ptr2; //declare a second pointer to change reference from start of msg string to start of "homeTeam" - i.e. "h"
int count = 0; //this is to count for hmTeam character array
int i;
for(i = 0; i < 321; i++){
if(*(ptr+i) == 'h' && *(ptr+i+4) == 'T' && *(ptr+i+7) == 'm'){ //once the sequence "h T m" is found, we know we have reached the word "homeTeam" in the string
ptr2 = ptr+i+11;
while(*ptr2 != '"'){
hmTeam[count] = *ptr2;
ptr2++;
count++;
}
}
count = 0;
if(*(ptr+i) == 'a' && *(ptr+i+3) == 'y' && *(ptr+i+4) == 'T' && *(ptr+i+7) == 'm'){
ptr2 = ptr+i+11;
while(*ptr2 != '"'){
awTeam[count] = *ptr2;
ptr2++;
count++;
}
}
count = 0;
if(*(ptr+i) == 'd' && *(ptr+i+2) == 't' && *(ptr+i+3) == 'e'){
ptr2 = ptr+i+7;
while(*ptr2 != '"'){
dateTime[count] = *ptr2;
ptr2++;
count++;
}
}
}
}
if(c == '{'){
flag1 = 1;
}
if(flag1 == 1){
if(c == '"'){
flag2 = 1;
}
}
if(flag2 == 1){
if(c == '_'){
flag3 = 1;
}
}
if(flag3 == 1){
if(c == 'l'){
flag4 = 1;
}
}
}
if(!client.connected() && cstopFlag == 0){
delay(100);
client.flush();
client.stop();
Serial.println();
Serial.println("Disconnected.");
delay(500);
Serial.println();
cstopFlag = 1;
flag7 = 1;
}
if(flag7 == 1){
checkAction();
countdownFlag = 1;
}
if(countdownFlag == 1){
//lcd.setCursor(0,0);
//lcd.setBacklight(LOW);
//teamNamePrint(hmTeam);
//lcd.print(" vs ");
//teamNamePrint(awTeam);
countdown();
}
}
void recordMessage(char message){
msg[letterCount] = message;
letterCount++;
}
/*
void teamNamePrint(){
char team[25];
strcpy(team,hmTeam);
if(team == "Arsenal FC")
lcd.print("Arsenal");
else if (team == "Aston Villa FC")
lcd.print("AVFC");
else if (team =="Burnley FC")
lcd.print("Burnley");
else if (team =="Chelsea FC")
lcd.print("Chelsea");
else if (team =="Crystal Palace FC")
lcd.print("CPFC");
else if (team =="Everton FC")
lcd.print("Everton");
else if (team =="Hull City")
lcd.print("Hull");
else if (team =="Leiceter City FC")
lcd.print("Lcity");
else if (team =="Liverpool FC")
lcd.print("Lpool");
else if (team =="Manchester City FC")
lcd.print("ManCity");
else if (team =="Manchester United")
lcd.print("ManU");
else if (team =="Newcastle FC")
lcd.print("NCFC");
else if (team =="Queens Park Rangers")
lcd.print("QPR");
else if (team =="FC Southampton")
lcd.print("Sthtn");
else if (team =="Stoke FC")
lcd.print("Stoke");
else if (team =="Sunderland FC")
lcd.print("Sndlnd");
else if (team =="Swansea FC")
lcd.print("SwnFC");
else if (team =="Tottenham Hotspur")
lcd.print("Spurs");
else if (team =="West Bromwich Albion")
lcd.print("WBrom");
else if (team =="West Ham Utd")
lcd.print("WHU");
}
*/
void checkAction(){
if(lpcount < 60){
gametime_calc();
timediff = gametime - current_time;
days = timediff / 86400;
rem1 = timediff % 86400;
hrs = rem1 / 3600;
rem2 = rem1 % 3600;
mins = rem2 / 60;
sec = rem2 % 60;
} else if(lpcount == 60){
lpcount = 0;
current_time = WiFly.getTime();
new_timediff = gametime - current_time;
if((new_timediff - timediff) != 0){
lcd.setBacklight(HIGH);
lcd.clear();
lcd.print("Resetting...");
delay(1500);
days = new_timediff / 86400;
rem1 = new_timediff % 86400;
hrs = rem1 / 3600;
rem2 = rem1 % 3600;
mins = rem2 / 60;
sec = rem2 % 60;
lcd.clear();
}
}
}
void countdown(){
lcd.setCursor(0,1);
lcd_print(days,0); //print days
lcd_print(hrs,3); //print hours
lcd_print(mins,6); //print minutes
lcd_print(sec,9); //print seconds
if(sec == 0 && mins == 0 && hrs == 0 && days == 0){
lcd.setBacklight(HIGH);
lcd.setCursor(0,0);
lcd.print("Game begins now!");
lcd.setCursor(0,1);
lcd.print(" ");
delay(30000);
resetFunc(); //call reset
} else if(sec == 0 && mins != 0){
mins--;
lpcount++;
sec = 60;
} else if(sec == 0 && mins == 0 && hrs != 0){
hrs--;
mins = 59;
sec = 60;
} else if(sec == 0 && mins == 0 && hrs == 0 && days != 0){
days--;
hrs = 23;
mins = 59;
sec = 60;
}
if(lpcount == 60){
checkAction();
lcd.setCursor(0,1);
lcd_print(days,0); //print days
lcd_print(hrs,3); //print hours
lcd_print(mins,6); //print minutes
lcd_print(sec,9); //print seconds
}
delay(1000);
sec--;
}
void lcd_print(long period, int col){
if(period < 10){
lcd.setCursor(col,1);
lcd.print("0");
lcd.setCursor(col+1,1);
lcd.print(period);
} else{
lcd.print(period);
}
if(col != 9){
lcd.print(":");
}
}
void gametime_calc(){
TimeElements tm; //create a TimeElements variable
//for conversion to time_t variable
//Fill in the elements of tm with those from the msg array
tm.Second = (dateTime[17] - '0')*10 + (dateTime[18] - '0');
tm.Minute = (dateTime[14] - '0')*10 + (dateTime[15] - '0');
tm.Hour = (dateTime[11] - '0')*10 + (dateTime[12] - '0');
tm.Day = (dateTime[8] - '0')*10 + (dateTime[9] - '0');
tm.Month = (dateTime[5] - '0')*10 + (dateTime[6] - '0');
tm.Year = ((dateTime[0] - '0')*1000 + (dateTime[1] - '0')*100 + (dateTime[2] - '0')*10 + (dateTime[3] - '0')) - 1970;
gametime = makeTime(tm); //game time as time_t variable
}
What could be causing this issue? Possibly hardware?

Related

Bop it Arduino Circuit Playground

I'm trying to create a bop it game with 2 players where:
Green LED – Gyro Sensor (Z-axis)
Red LED – Temperature sensor
Blue LED – Sound sensor
Yellow LED – Light Sensor
However when I press the left button to start the game, only the yellow light above GND goes on and doesn't respond to anything, what can be wrong with my code?
long randNumber;
int temp;
int val;
int number;
int score;
boolean state;
int light;
uint8_t pixeln = 0;
int sound;
int shake;
unsigned long lastmillis = 0;
boolean game;
const byte numPins = 7;
byte pins[] = {0, 1, 2, 3, 4, 5, 6, 7};
int player1 = 0;
int player2 = 0;
#include "Adafruit_CircuitPlayground.h"
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
CircuitPlayground.begin();
CircuitPlayground.clearPixels();
//timer();
}
void loop() {
if (digitalRead(4) == HIGH && digitalRead(19) == LOW){
Serial.println("Button Pressed");
lastmillis = millis();
for (int i=0; i < 10; i ){
randNumber = random(4);
//Serial.println("Loop");
switch (randNumber){
//Serial.println("Switch");
case 0:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 255,0,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
temp = analogRead(A0);
//Serial.println(temp);
while (state == true) {
val = analogRead(A0);
number = val - temp;
if (number > 10){
score ;
state = false;
//Serial.println("Temperature");
delay(500);
}
}
break;
case 1:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 127,127,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
light = analogRead(A5);
//Serial.println(light);
while (state == true) {
val = analogRead(A5);
number = light - val;
if (number > 20){
score ;
state = false;
//Serial.println(number);
delay(500);
}
}
break;
case 2:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 0,0,255);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
sound = analogRead(A4);
//Serial.println(sound);
while (state == true) {
val = analogRead(A4);
number = val - sound;
if (number > 50){
score ;
state = false;
//Serial.println(number);
delay(500);
}
}
break;
case 3:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 0,255,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
val = CircuitPlayground.motionZ();
//Serial.println(val);
state = true;
while (state == true) {
shake = CircuitPlayground.motionZ();
number = val - shake;
//Serial.println(number);
if (number > 15 or number < -15){
score ;
state = false;
//Serial.println("shake");
delay(500);
}
}
break;
case 4:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(0, 0,255,0);
CircuitPlayground.setPixelColor(1, 0,255,0);
CircuitPlayground.strip.show();
delay(3000);
break;
case 5:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(3, 255,0,0);
CircuitPlayground.setPixelColor(4, 255,0,0);
CircuitPlayground.strip.show();
delay(3000);
break;
case 6:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(5, 127,0,255);
CircuitPlayground.setPixelColor(6, 127,0,255);
CircuitPlayground.strip.show();
delay(3000);
break;
case 7:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(8, 255,128,0);
CircuitPlayground.setPixelColor(9, 255,128,0);
CircuitPlayground.strip.show();
delay(3000);
}
}
if (CircuitPlayground.slideSwitch()) {
player1 = (millis() - lastmillis) / 1000;
Serial.println(player1);
CircuitPlayground.clearPixels();
byte num = player1;
for (byte i=0; i < numPins; i ){
byte state = bitRead(num, i);
if (state == 1){
CircuitPlayground.setPixelColor(pins[i], 255,255,255);
CircuitPlayground.strip.show();
}
}
} else {
player2 = (millis() - lastmillis) /1000;
Serial.println(player2);
CircuitPlayground.clearPixels();
byte num = player2;
for (byte i=0; i < numPins; i ){
byte state = bitRead(num, i);
if (state == 1){
CircuitPlayground.setPixelColor(pins[i], 255,255,255);
CircuitPlayground.strip.show();
}
}}
CircuitPlayground.playTone(330, 500);
}
while (digitalRead(19) == HIGH && digitalRead(4) == LOW){
if (player1 < player2){
CircuitPlayground.clearPixels();
delay(250);
for (int i =0; i < 5; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
else if (player1 > player2){
CircuitPlayground.clearPixels();
delay(250);
for (int i =5; i < 10; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
else {
CircuitPlayground.clearPixels();
delay(250);
for (int i =0; i < 10; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
}
if (digitalRead(19) == HIGH && digitalRead(4) == HIGH){
player1 = 0;
player2 = 0;
for (int a =0; a < 10; a ){
CircuitPlayground.clearPixels();
delay(100);
for (int i =0; i < 10; i ){
CircuitPlayground.setPixelColor(i, CircuitPlayground.colorWheel(25 * i));
CircuitPlayground.strip.show();
}
delay(100);
CircuitPlayground.clearPixels();
}
}
}
visual after pressing left button:

Would love for you to help some bugs and improved code

I wrote a code designed to automatically turn on and off a generator based on whether the battery is full or empty.
There are some bugs in lcd.begin() and lcd.clear() (They both don't work).
error : invalid use of non-static member function.
Thank you!
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool Settings = false;
unsigned long time1;
#define RELAY_PORT 10
float Voltage = 0.00;
int VoltOn = 47;
int VoltOff = 56;
int upbutton = 1;
int downbutton = 2;
int okbutton = 3;
int backbutton = 4;
void setup() {
pinMode(upbutton, INPUT);
pinMode(downbutton, INPUT);
pinMode(okbutton, INPUT);
pinMode(backbutton, INPUT);
pinMode(RELAY_PORT, OUTPUT);
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.setCursor(0, 1);
lcd.print ("generator: off");
}
void loop() {
int analog_value = analogRead(A0);
Voltage = ((analog_value * 5.0) / 1020) * 12;
lcd.setCursor(0, 0);
lcd.print("Volt: ");
lcd.print(Voltage);
lcd.print("V");
if (Voltage <= VoltOn && digitalRead(RELAY_PORT) == LOW)
{
digitalWrite(RELAY_PORT, HIGH);
lcd.setCursor(0, 1);
lcd.print ("generator: on");
}
if (Voltage >= VoltOff && digitalRead(RELAY_PORT) == HIGH)
{
digitalWrite(RELAY_PORT, LOW);
lcd.setCursor(0, 1);
lcd.print ("generator: off");
}
if ((digitalRead(upbutton) == HIGH && digitalRead(downbutton) == HIGH && digitalRead(okbutton) == HIGH && digitalRead(backbutton) == HIGH) || (Settings = true))
{
lcd.clear;
lcd.print("Settings:");
delay(2000);
time1 = millis();
bool Setting = false;
while (digitalRead(upbutton) == LOW && digitalRead(downbutton) == LOW && digitalRead(okbutton) == LOW && digitalRead(backbutton) == LOW)
{
if (Setting == false)
{
lcd.clear;
lcd.scrollDisplayLeft();
lcd.print("press Up to set turn on");
lcd.setCursor(0, 1);
lcd.print("press down to set turn off");
Setting = true;
}
if (millis() > time1 + 60000)
{
loop();
}
}
time1 = millis();
while (millis() > time1 + 60000)
{
if (upbutton == HIGH)
{
time1 = millis();
//lcd.clear;
lcd.scrollDisplayLeft();
lcd.print("press Up/ Down to up/ Down Voltage turn on");
while (digitalRead(upbutton) == LOW && digitalRead(downbutton) == LOW && digitalRead(okbutton) == LOW && digitalRead(backbutton) == LOW)
{
if (millis() > time1 + 60000)
{
loop();
}
}
time1 = millis();
while (millis() > time1 - 60000)
{
if (upbutton == HIGH)
{
int xdelay = 1000;
time1 = millis();
(VoltOn) = (VoltOn) + 1;
lcd.setCursor(0, 1);
lcd.print("Voltage-on: ");
lcd.print(VoltOn);
lcd.print("V");
delay(xdelay);
xdelay = xdelay / 1.5;
}
if (downbutton == HIGH)
{
int xdelay = 1000;
time1 = millis();
(VoltOn) = (VoltOn) - 1;
lcd.setCursor(0, 1);
lcd.print("Voltage-on: ");
lcd.print(VoltOn);
lcd.print("V");
delay(xdelay);
xdelay = xdelay / 1.5;
}
if (okbutton == HIGH)
{
loop();
}
if (backbutton == HIGH)
{ Settings = true;
}
if (millis() > time1 + 60000)
{
loop();
}
}
}
if (downbutton == HIGH)
{
time1 = millis();
lcd.clear;
lcd.scrollDisplayLeft();
lcd.print("press Up/ Down to up/ Down Voltage turn off");
while (digitalRead(upbutton) == LOW && digitalRead(downbutton) == LOW && digitalRead(okbutton) == LOW && digitalRead(backbutton) == LOW)
{
if (millis() > time1 + 60000)
{
loop();
}
}
time1 = millis();
while (millis() > time1 - 60000)
{
if (upbutton == HIGH)
{
int xdelay = 1000;
time1 = millis();
(VoltOff) = (VoltOff) + 1;
lcd.setCursor(0, 1);
lcd.print("Voltage-off: ");
lcd.print(VoltOff);
lcd.print("V");
delay(xdelay);
xdelay = xdelay / 1.5;
}
if (downbutton == HIGH)
{
int xdelay = 1000;
time1 = millis();
(VoltOff) = (VoltOff) - 1;
lcd.setCursor(0, 1);
lcd.print("Voltage-off: ");
lcd.print(VoltOff);
lcd.print("V");
delay(xdelay);
xdelay = xdelay / 1.5;
}
if (okbutton == HIGH)
{
loop();;
}
if (backbutton == HIGH)
{
Settings = true;
}
if (millis() > time1 + 60000)
{
loop();
}
}
}
if (backbutton == HIGH)
{
loop();
}
if (okbutton == HIGH)
{
loop();
}
}
}
}
I think you shouldn't ask on stackoverflow for someone just to do your work. People don't like unspecific questions here, that show little to no effort of own research..
That said, one thing that stands out is, that all your lcd.clear are missing brackets, they should be lcd.clear();
Try to split up your code in smaller sections and test individual components, if something doesn't work.
1.How to make this code (at the beginning of the loop) only run every half a second without rivet the rest of the code?
lcd.setCursor(0, 0);
lcd.print("Volt: ");
lcd.print(Voltage);
lcd.print("V");
2.The LCD displays (that the current coming to Arduino from the accumulator is lower than its starting power) "generator: onf" and not "generator: off":
lcd.print ("generator: off");
if (Voltage <= VoltOn && digitalRead(RELAY_PORT) == LOW)
{
digitalWrite(RELAY_PORT, HIGH);
lcd.setCursor(0, 1);
lcd.print ("generator: on");
}
if (Voltage >= VoltOff && digitalRead(RELAY_PORT) == HIGH)
{
digitalWrite(RELAY_PORT, LOW);
lcd.setCursor(0, 1);
lcd.print ("generator: off");

Sending message to the extracted number

Please someone let me know why this code is not working. I want to extract message sender's number and then forward message to it using AT commands. It extracts the number of sender and stores it in a variable but why won't it send a message to that number?
#include <GSM.h>
GSM_SMS sms;
char RcvdMsg[200] = "";
int RcvdCheck = 0;
int RcvdConf = 0;
int index = 0;
int RcvdEnd = 0;
char MsgMob[15];
char MsgTxt[50];
int MsgLength = 0;
char number1[12] = "xxxxxxxxxx";
String number;
char inchar;
char outString[22];
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial1.print("ATE0\r");
Serial1.print("AT\r");
Serial1.print("AT+CMGF=1\r");
Serial1.print("AT+CNMI=1,2,0,0,0\r");
delay(1000);
}
void loop()
{
recSms();
}
void recSms()
{
if(Serial1.available())
{
char data = Serial1.read();
if(data == '+'){RcvdCheck = 1;}
if((data == 'C') && (RcvdCheck == 1)){RcvdCheck = 2;}
if((data == 'M') && (RcvdCheck == 2)){RcvdCheck = 3;}
if((data == 'T') && (RcvdCheck == 3)){RcvdCheck = 4;}
if(RcvdCheck == 4){RcvdConf = 1; RcvdCheck = 0;}
if(RcvdConf == 1)
{
if(data == '\n'){RcvdEnd++;}
if(RcvdEnd == 3){RcvdEnd = 0;}
RcvdMsg[index] = data;
index++;
if(RcvdEnd == 2){RcvdConf = 0;MsgLength = index-2;index = 0;}
if(RcvdConf == 0)
{
Serial.print("Mobile Number is: ");
for(int x = 4;x < 17;x++)
{
number+=RcvdMsg[x];
MsgMob[x-4] = RcvdMsg[x];
}
Serial.print(number);
Serial.println();
Serial.print("Message Text: ");
for(int x = 46; x < MsgLength; x++)
{
MsgTxt[x-46] = RcvdMsg[x];
inchar=MsgTxt[x-46];
}
Serial.print(inchar);
Serial.println();
RcvdCheck = 0;
RcvdConf = 0;
index = 0;
RcvdEnd = 0;
MsgMob[15];
MsgTxt[50];
MsgLength = 0;
Serial.flush();
Serial1.flush();
if(inchar == '#')
{
sendInfo();
}
}
}
}
}
void sendInfo()
{
Serial1.print("AA");
delay(1000); //delay of 1
Serial1.println("AT");
delay(1000);
Serial1.write("AT+CMGF=1\r\n"); //set GSM to text mode
delay(1000);
Serial1.write("AT+CPMS=\"SM\"\r\n"); //Preferred SMS Message Storage
delay(1000);
Serial1.print("AT+CMGS=\"");
Serial1.print(number1);
Serial1.print("\"");
delay(1000);
Serial1.print("HI");
delay(1000);
Serial1.write(0x1A); // sends ctrl+z end of message
delay(1000);
Serial.println("sms sent ");
} //end sendInfo()
Okay so the problem seems to be with these lines:
Serial1.print("AT+CMGS=\"");
Serial1.print(number1);
Serial1.print("\"");
But if we write the lines written below, program works just fine!
Serial1.write("AT+CMGS=\"");
Serial1.print(number);
Serial1.write("\"\r");

Arduino and processing controlling servo

I am trying to control a servo using processing.
Basically, what I want is to press a button on processing and make the servo go from 0 to 180 degrees and back every two seconds and if I press another button on processing I should be able to move the servo using a potentiometer, however I am not able to make it work properly.
For example, when I pressed the POT button, the servo goes to the desired position, however if I move the potentiometer, the servo doesn't move unless I click the pot button again.
This is the relevant code for Arduino:
void loop ()
{
if(Serial.available()){
val= Serial.read();
while(val == 1){
digitalWrite(ledCPM, HIGH);
digitalWrite(ledPot, LOW);
digitalWrite(ledFSR,LOW);
cpmMovement();
val = Serial.read();
}
while(val == 2){
digitalWrite(ledPot, HIGH);
digitalWrite(ledCPM, LOW);
digitalWrite(ledFSR,LOW);
potMovement();
val = Serial.read();
}
while(val == 3){
digitalWrite(ledPot, LOW);
digitalWrite(ledCPM, LOW);
digitalWrite(ledFSR,HIGH);
fsrMovement();
val = Serial.read();
}
}
}
void cpmMovement(){unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval){
previousMillis = currentMillis;
if(positionservo == 0){
positionservo = 179;
myservo1.write(positionservo);
}
else{
positionservo = 0;
myservo1.write(positionservo);
}
}
}
void potMovement(){
int analogValuePot = analogRead(Pot)/4;
Serial.print("Potentiometer reading= ");
Serial.println(analogValuePot); // This will print the raw force value
int valuePot = map(analogValuePot, 0, 255, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo1.write(valuePot); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
}
void fsrMovement(){
force = analogRead(FSR); // Reads the FSR
Serial.print("Force sensor reading = ");
Serial.println(force); // This will print the raw force value
int pos = map(force, 0, 1023, 0, 175); // Scales the force reading to degrees for servo control
Serial.print("servomotor degrees = ");
Serial.println(pos); // This will print the adjusted servo reading (an angle)
myservo1.write(pos); // Write the new angle to the servo
delay(150); // Delay 150 milliseconds before taking another reading
}
And here you can see the relevant processing code as everything else works ok
import processing.serial.*;
void mousePressed(){
println("Coordinates: " + mouseX + "," + mouseY);
if(pressedCPMButton && currentColorCPM==butColorCPM){ //Changing color CPM to pressed button color
currentColorCPM = butpreColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butColorFSR;
myPort.write(1); //Send 1 to port
}else if(pressedCPMButton && currentColorCPM==butpreColorCPM){
currentColorCPM = butColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butColorFSR;
}
if(pressedPOTButton && currentColorPOT==butColorPOT){
currentColorCPM = butColorCPM;
currentColorPOT = butpreColorPOT;
currentColorFSR = butColorFSR;
myPort.write(2); //Send 2 to port
}else if(pressedPOTButton && currentColorPOT==butpreColorPOT){
currentColorCPM = butColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butColorFSR;
}
if(pressedFSRButton && currentColorFSR==butColorFSR){
currentColorCPM = butColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butpreColorFSR;
myPort.write(3); //Send 3 to port
}else if(pressedFSRButton && currentColorFSR==butpreColorFSR){
currentColorCPM = butColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butColorFSR;
}
}
boolean pressedButtonCPM(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
boolean pressedButtonPOT(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
boolean pressedButtonFSR(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}

Initializer-string for array of chars is too long error on Arduino

I am trying to run a code below on Arduino but when I verify the code, it shows,
'Initializer-string for array of chars is too long'.
Although I have read previous questions regarding similar issues, I couldn't know where to begin in the code I am trying now. Yes,,, I am very new to C++ world,,, If you can give me a clue to teach myself or a direct answer, it would be amazingly appreciated.
Best,
/**** SET YOUR MAC ADDRESS HERE ****/
char mac[13] = "74-E5-43-BE-42-10";
/***********************************/
#define LED 13
#define BLUESMIRFON 2
#define FACTORYRESETBAUD 57600
#define DEFAULTBAUD 115200
char str[3];
char passkey[5] = "0000";
boolean success = false;
int failOuts[10] = {3,4,5,6,7,8,9,10,11,12};
void setup()
{
//Initialize pins
pinMode(LED, OUTPUT);
pinMode(BLUESMIRFON, OUTPUT);
for (int i=0; i<10; i++) {
pinMode(failOuts[i], OUTPUT);
}
// First reset to factory defaults
while (!success) {
RunBlueSmirfSetup(true);
}
success = false;
// Then set up with the correct mac address
RunBlueSmirfSetup(false);
}
void loop() {
if(success) {
digitalWrite(LED,LOW);
delay(1000);
digitalWrite(LED,HIGH);
delay(1000);
}
}
void RunBlueSmirfSetup(boolean factoryReset) {
//Initialize serial ports
if (factoryReset) {
Serial.begin(FACTORYRESETBAUD);
} else {
Serial.begin(DEFAULTBAUD);
}
digitalWrite(BLUESMIRFON, LOW);
delay(2000);
digitalWrite(BLUESMIRFON, HIGH);
delay(2000); //Wait for BlueSMIRF to turn on
Serial.print('$'); //Send command to put BlueSMIRF into programming mode
Serial.print('$');
Serial.print('$');
delay(100);
Serial.flush();
//Reset the module
if (factoryReset) {
Serial.print('S');
Serial.print('F');
Serial.print(',');
Serial.print('1');
Serial.print('\r');
while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
success = true;
} else {
success = false;
digitalWrite(failOuts[0],HIGH);
}
delay(100);
Serial.flush();
} else {
//Set the baudrate
Serial.print('S');
Serial.print('U');
Serial.print(',');
Serial.print('5');
Serial.print('7');
Serial.print('\r');
while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
success = true;
} else {
success = false;
digitalWrite(failOuts[1],HIGH);
}
delay(100);
Serial.flush();
//Set the remote MAC address
Serial.print('S');
Serial.print('R');
Serial.print(',');
for(int i = 0; i < 12; i++) {
Serial.print(mac[i]);
}
Serial.print('\r');
while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
success = true;
} else {
success = false;
digitalWrite(failOuts[2],HIGH);
}
delay(100);
Serial.flush();
//Set the passkey
Serial.print('S');
Serial.print('P');
Serial.print(',');
for(int i = 0; i < 4; i++) {
Serial.print(passkey[i]);
}
Serial.print('\r');
while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
success = true;
} else {
success = false;
digitalWrite(failOuts[3],HIGH);
}
delay(100);
Serial.flush();
//Set the BlueSMiRF mode
Serial.print('S');
Serial.print('M');
Serial.print(',');
Serial.print('3');
Serial.print('\r');
while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
success = true;
} else {
success = false;
digitalWrite(failOuts[4],HIGH);
}
delay(100);
Serial.flush();
delay(100);
//Exit command mode
}
Serial.print('-');
Serial.print('-');
Serial.print('-');
Serial.print('\r');
//delay(100);
//Serial.flush();
//delay(100);
//Serial.end();
//digitalWrite(BLUESMIRFON, LOW);
}
CleanProgramBlueSMiRF CleanProgramBlueSMiRF
It would help if you copied the exact error message including the line number.
I assume it is failing on:
char mac[13] = "74-E5-43-BE-42-10";
since it has 17 chars between the quotes and you allocated 13.

Resources