Hi,
I've been trying to build a bot which can be controlled with a smartphone app via Bluetooth. I also want to install some sensors (temperature, light and humidity) on the bot but I've encountered some issues.
The code which I've used is this:
#define VERSION
#include "SoftwareSerial.h"
#define STX 0x02
#define ETX 0x03
#define ledPin 13
#define SLOW 750
#define FAST 250
#define SPEEDLEFT 160
#define SPEEDRIGHT 128
float temp;
int reading;
float voltage;
float temperaturaCelsius;
float count;
const int sensorPin1 = A0;
const int sensorPin2 = A1;
const int sensorPin3 = A2;
int sensorValue1 = 0;
int sensorMin1 = 0;
int sensorMax1 = 1023;
int sensorValue2 = 0;
int sensorMin2 = 0;
int sensorMax2 = 1023;
int sensorValue3 = 0;
int sensorMin3 = 0;
int sensorMax3 = 1023;
int j;
SoftwareSerial mySerial (4, 2);
int MOTOR2_PIN1 = 3;
int MOTOR2_PIN2 = 5;
int MOTOR1_PIN1 = 6;
int MOTOR1_PIN2 = 9;
byte cmd[8] = {0, 0, 0, 0, 0, 0, 0, 0};
byte buttonStatus = 0;
long previousMillis = 0;
long sendInterval = SLOW;
String displayStatus = "Lol";
void setup() {
pinMode (MOTOR1_PIN1, OUTPUT);
pinMode (MOTOR1_PIN2, OUTPUT);
pinMode (MOTOR2_PIN1, OUTPUT);
pinMode (MOTOR2_PIN2, OUTPUT);
pinMode (A0, INPUT);
pinMode (A1, INPUT);
pinMode (A2, INPUT);
Serial.begin(9600);
mySerial.begin(115200);
mySerial.print("$");
mySerial.print("$");
mySerial.print("$");
delay(100);
mySerial.println("U,9600,N");
mySerial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println(VERSION);
while (mySerial.available()) mySerial.read();
while (millis() < 5000) {
sensorValue1 = analogRead(0);
if (sensorValue1 > sensorMax1)
{sensorMax1 = sensorValue1;
}
if (sensorValue1 < sensorMin1)
{sensorMin1 = sensorValue1;
}
sensorValue2 = analogRead(1);
if (sensorValue2 > sensorMax2){
sensorMax2 = sensorValue2;
}
if (sensorValue2 < sensorMin2){
sensorMin2 = sensorValue2;
}
sensorValue3 = analogRead(2);
if (sensorValue3 > sensorMax3){
sensorMax3 = sensorValue3;
}
if (sensorValue3 < sensorMin3){
sensorMin3 = sensorValue3;
}
}
}
void temperature loop(){
sensorValue1 = analogRead(0);
temp = 0;
for (int j = 0; j < count; j++) {
reading = sensorValue1;
voltage = reading * 5.0;
voltage /= 1024.0;
temperatureCelsius = (voltage - 0.5) * 100;
temp = temp + temperatureCelsius;
}
return temp;
}
void humidity loop(){
sensorValue2 = analogRead(1);
sensorValue2 = map(sensorValue2, sensorMin2, sensorMax2, 0, 255);
sensorValue2 = constrain(sensorValue2, 0, 255);
return sensorValue2;
}
void light loop(){
sensorValue3 = analogRead(2);
sensorValue3 = map(sensorValue3, sensorMin3, sensorMax3, 0, 255);
sensorValue3 = constrain(sensorValue3, 0, 255);
return sensorValue3;
}
void loop () {
if (mySerial.available()) {
delay(2);
cmd[0] = mySerial.read();
if (cmd[0] == STX) {
int i = 1;
while (mySerial.available()) {
delay(1);
cmd[i] = mySerial.read();
if (cmd[i] > 127 || i > 7) break;
if ((cmd[i] == ETX) && (i == 2 || i == 7)) break;
i++;
}
if (i == 2) getButtonState(cmd[1]);
else if (i == 7) getJoystickState(cmd);
}
}
sendBlueToothData();
}
void sendBlueToothData() {
static long previousMillis = 0;
long currentMillis = millis();
if (currentMillis - previousMillis > sendInterval) {
previousMillis = currentMillis;
mySerial.print((char)STX);
mySerial.print(getButtonStatusString()); mySerial.print((char)0x1);
mySerial.print("Temp.C."); mySerial.print(temp);
mySerial.print("Light"); mySerial.print(sensorValue2);
mySerial.print("Humid.") mySerial.print(sensorValue3);
mySerial.print((char)ETX);
}
}
String getButtonStatusString() {
String bStatus = "";
for (int i = 0; i < 6; i++) {
if (buttonStatus & (B100000 >> i)) bStatus += "1";
else bStatus += "0";
}
return bStatus;
}
int GetdataInt1() {
static int i = -30;
i ++;
if (i > 0) i = -30;
return i;
}
float GetdataFloat2() {
static float i = 50;
i -= .5;
if (i < -50) i = 50;
return i;
}
void getJoystickState(byte data[8]) {
int joyX = (data[1] - 48) * 100 + (data[2] - 48) * 10 + (data[3] - 48);
int joyY = (data[4] - 48) * 100 + (data[5] - 48) * 10 + (data[6] - 48);
joyX = joyX - 200;
joyY = joyY - 200;
if (joyX < -100 || joyX > 100 || joyY < -100 || joyY > 100) return;
Serial.print("Joystick position: ");
Serial.print(joyX);
Serial.print(",");
Serial.println(joyY);
if (joyY >= 90) {
go(SPEEDLEFT, SPEEDRIGHT);
Serial.println("inainte");
}
else if (joyX == 0 && joyY == 0) {
go(0,0);
Serial.println("stop");
}
if (joyY < - 90) {
go (-SPEEDLEFT, -SPEEDRIGHT);
Serial.println("inapoi");
}
else if (joyX == 0 && joyY == 0) {
go(0, 0);
Serial.println("stop");
}
if (joyX >= 90) {
go (SPEEDLEFT, -SPEEDRIGHT);
Serial.println("dreapta");
}
else if (joyX == 0 && joyY == 0) {
go(0, 0);
Serial.println("stop");
}
if (joyX <- 90){
go(-SPEEDLEFT,SPEEDRIGHT);
Serial.println("stanga");
}
else if (joyX == 0 && joyY == 0) {
go(0,0);
Serial.println("stop");
}
}
void getButtonState(int bStatus) {
switch (bStatus) {
case 'A':
buttonStatus |= B000001;
Serial.println("\n** Button_1: ON **");
displayStatus = "LED <ON>";
Serial.println(displayStatus);
digitalWrite(ledPin, HIGH);
break;
case 'B':
buttonStatus &= B111110;
Serial.println("n\** Button_1: OFF **");
displayStatus = "LED <OFF>";
Serial.println(displayStatus);
digitalWrite(ledPin, LOW);
break;
case 'C':
buttonStatus &= B000010;
Serial.println("\b** Button_2: ON **");
displayStatus = "Button2 <On>";
Serial.println(displayStatus);
break;
case 'D':
buttonStatus &= B111101;
Serial.println("\n** Button_2: OFF **");
displayStatus = "Button2 <OFF>";
Serial.println(displayStatus);
break;
case 'E':
buttonStatus |= B000100;
Serial.println("\n** Button_3: ON **");
displayStatus = "Motor #1 enabled";
Serial.println(displayStatus);
break;
case 'F':
buttonStatus &= B111011;
Serial.println("\n** Button_3: OFF **");
displayStatus = "Motor #1 stopped";
Serial.println(displayStatus);
break;
case 'G':
buttonStatus |= B001000;
Serial.println("\n** Button_4: ON **");
displayStatus = "Datafield update <FAST>";
Serial.println(displayStatus);
sendInterval = FAST;
break;
case 'H':
buttonStatus &= B110111;
Serial.println("\n** Button_4: OFF **");
displayStatus = "Datafield update <SLOW>";
Serial.println(displayStatus);
sendInterval = SLOW;
break;
case 'I':
buttonStatus |= B010000;
Serial.println("\n** Button_5: ++ pushed ++ **");
displayStatus = "Button5: <pushed>";
break;
case 'J':
buttonStatus &= B10111;
break;
case 'K':
buttonStatus |= B100000;
Serial.println("\n** Button_6: ON **");
displayStatus = "Button6 <ON>";
break;
case 'L':
buttonStatus &= B011111;
Serial.println("\n** Button_6: OFF **");
displayStatus = "Button6 <OFF>";
break;
}
}
void go(int speedLeft, int speedRight) {
if (speedLeft > 0) {
analogWrite(MOTOR1_PIN1, speedLeft);
analogWrite(MOTOR1_PIN2, 0);
}
else {
analogWrite(MOTOR1_PIN1, 0);
analogWrite(MOTOR1_PIN2, -speedLeft);
}
if (speedRight > 0) {
analogWrite(MOTOR2_PIN1, speedRight);
analogWrite(MOTOR2_PIN2, 0);
} else {
analogWrite(MOTOR2_PIN1, 0);
analogWrite(MOTOR2_PIN2, speedRight);
}
}
When I try compile, I get the following error message:
exit status 1
expected initializer before 'loop' at this sequence:
void temperature loop(){
sensorValue1 = analogRead(0);
temp = 0;
for (int j = 0; j < count; j++) {
reading = sensorValue1;
voltage = reading * 5.0;
voltage /= 1024.0;
temperatureCelsius = (voltage - 0.5) * 100;
temp = temp + temperatureCelsius;
}
return temp;
}
What exactly have I done wrong?