Hi can anyone read this code and correct the 2 errors I get ? I would really appreciate a quick response thanks in advance.
#define sol_channel 12
int command; //Int to store app command state.
boolean sol = false;
#define start_Channel 13
int command; //Int to store app command state. // ERROR :o
boolean start = false;
#include <SoftwareSerial.h> // TX RX software library for bluetooth
#include <Servo.h> // servo library
Servo myservo; // servo naam
void setup() {
pinMode(sol_Channel, OUTPUT);
Serial.begin(9600);
pinMode(start_Channel, OUTPUT);
Serial.begin(9600);
myservo.attach(9); // attach servo signal wire to pin 9
//Setup usb serial verbinding naar pc
Serial.begin(9600);
//Setup Bluetooth connective naar android
bluetooth.begin(9600);
}
void loop(){
if (Serial.available() > 0) {
command = Serial.read();
if (start) {digitalWrite(start_Channel, HIGH);}
else {digitalWrite(start_Channel, LOW);}
38
switch (command) {
case 'L':start = true;break;
case 'l':start = false;break;
if (Serial.available() > 0) {
command = Serial.read();
if (sol) {digitalWrite(sol_Channel, HIGH);}
else {digitalWrite(sol_Channel, LOW);}
switch (command) {
case 'S':sol = true;break;
case 's':sol = false;break;
if(bluetooth.available()> 0 ) // sol ( solenoid )
{
int servopos = bluetooth.read(); // save the received number to servopos
Serial.println(servopos); // serial print servopos current number received from
bluetooth
myservo.write(servopos); // servo roteert naar gewenste hoek
}
}
}
}
You have more than 2 errors and if you post ALL the errors here it will be easier to explain them.
But you'll be getting at least some errors because you have never defined anything called bluetooth or said what pins it's connected to but you keep trying to use it.
Here it is, formatted, with the compile errors either fixed or commented out:
#define sol_channel 12
int command; // Int to store app command state.
boolean sol = false;
#define start_Channel 13
// int command; //Int to store app command state. // ERROR :o.
boolean start = false;
#include <SoftwareSerial.h> // TX RX software library for bluetooth
#include <Servo.h> // servo library
Servo myservo; // servo naam
void setup()
{
pinMode(sol_channel, OUTPUT);
Serial.begin(9600);
pinMode(start_Channel, OUTPUT);
Serial.begin(9600);
myservo.attach(9); // attach servo signal wire to pin 9
//Setup usb serial verbinding naar pc
Serial.begin(9600);
//Setup Bluetooth connective naar android
// bluetooth.begin(9600);
}
void loop()
{
if (Serial.available() > 0)
{
command = Serial.read();
if (start)
{
digitalWrite(start_Channel, HIGH);
}
else
{
digitalWrite(start_Channel, LOW);
}
// 38
switch (command)
{
case 'L': start = true; break;
case 'l': start = false; break;
if (Serial.available() > 0)
{
command = Serial.read();
if (sol)
{
digitalWrite(sol_channel, HIGH);
}
else
{
digitalWrite(sol_channel, LOW);
}
switch (command)
{
case 'S': sol = true; break;
case 's': sol = false; break;
// if (bluetooth.available() > 0 ) // sol ( solenoid )
// {
// int servopos = bluetooth.read(); // save the received number to servopos
// Serial.println(servopos); // serial print servopos current number received from
// bluetooth
// myservo.write(servopos); // servo roteert naar gewenste hoek
// }
}
}
}
}
}
You have included SoftwareSerial.h with a comment that says you're using it for Bluetooth. But you have never said what pins it is using nor have you declared a thing called bluetooth that uses it. Somewhere you should have a line something like
SoftwareSerial blueTooth(rxPin, txPin);
You must know what pins you have connected the bluetooth module to so tell the program!
BTW why do you think that you need to do THREE Serial.begins in setup()?
I want to start and stop a generator with two relays using mit app inventor, and have it regulate its rpm with a servo on the throttle and an hall sensor. I found this code for a similar project :
#include <Servo.h>
Servo myservo; // create servo object to control a servo
volatile int rpmcount;
unsigned int rpm;
unsigned long timeold;
//++++++++++++++++++++++++
int sensorValue = 0;
const int analoginPin = A8; // Analog input pin that the main speed control potentiometer is attached to
int relay = 19; //relay on digital pin 19
void setup()
{
digitalWrite(relay, HIGH);
//RELAY
pinMode(relay, OUTPUT);
// Main Speed Pot
pinMode(analoginPin, INPUT);
//STEPPER
myservo.attach(10); // attaches the servo on pin 10 to the servo object
Serial.begin(9600);
attachInterrupt(5, rpm_fun, RISING);
rpmcount = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
rpmCal();
sensorValue = analogRead(analoginPin); // reads the value of the potentiometer (value between 0 and 1023)
sensorValue = map(sensorValue, 1023, 0, 20, 200); // scale it to use it with the servo (value between 0 and 180)
myservo.write(sensorValue);
}
void rpmCal(){
if (rpmcount >= 20) {
//Update RPM every 20 counts, increase this for better RPM resolution,
//decrease for faster update
rpm = 30*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
Serial.println(rpm);
}
}
void rpm_fun()
{
rpmcount++;// this code will be executed every time the interrupt 5 (pin18) gets low.
}
I want to start and stop a generator with two relays using mit app inventor, and have it regulate its rpm with a servo on the throttle and an hall sensor. I found this code for a similar project :
#include <Servo.h>
Servo myservo; // create servo object to control a servo
volatile int rpmcount;
unsigned int rpm;
unsigned long timeold;
//++++++++++++++++++++++++
int sensorValue = 0;
const int analoginPin = A8; // Analog input pin that the main speed control potentiometer is attached to
int relay = 19; //relay on digital pin 19
void setup()
{
digitalWrite(relay, HIGH);
//RELAY
pinMode(relay, OUTPUT);
// Main Speed Pot
pinMode(analoginPin, INPUT);
//STEPPER
myservo.attach(10); // attaches the servo on pin 10 to the servo object
Serial.begin(9600);
attachInterrupt(5, rpm_fun, RISING);
rpmcount = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
rpmCal();
sensorValue = analogRead(analoginPin); // reads the value of the potentiometer (value between 0 and 1023)
sensorValue = map(sensorValue, 1023, 0, 20, 200); // scale it to use it with the servo (value between 0 and 180)
myservo.write(sensorValue);
}
void rpmCal(){
if (rpmcount >= 20) {
//Update RPM every 20 counts, increase this for better RPM resolution,
//decrease for faster update
rpm = 30*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
Serial.println(rpm);
}
}
void rpm_fun()
{
rpmcount++;// this code will be executed every time the interrupt 5 (pin18) gets low.
}
I need to combine the code above which is supposed to regulate the rpm with a hall sensor and a servo.
And the first code i wrote poorly to start and stop the generator
Solving problems a bit at a time is the way to go.
Have you tested the last code you posted and seen that it does what you need apart from using a potentiometer instead of input from Bluetooth to set rpm? That's obviously the first thing to test. Until that's right there's no point combining it with something else.
Then you want to write/find some code that simply receives data from your AppInventor app (which I'm assuming you already have) and prints it. There are loads of examples of that around.
When you have those two programs working then they are the ones you need to combine.
something like this ? : #include <SoftwareSerial bluetooth(rxPin, txPin)>;
but the i get this error : code_deel_een_revisie:8:10: fatal error: SoftwareSerial bluetooth(rxPin, txPin): No such file or directory #include <SoftwareSerial bluetooth(rxPin, txPin)>;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
SoftwareSerial bluetooth(rxPin, txPin): No such file or directory
I’m sure i’m making noob mistakes and i appreciate the help.
thanks for the help and taking the time, I’ve got this so far :
#define sol_channel 12
int command; // Int to store app command state.
boolean sol = false;
#define start_Channel 13
boolean start = false;
#define bluetooth(rxPin, txPin)
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(10, 11); // RX, TX - can be variables if you want
#include <Servo.h> // servo library
Servo myservo; // servo naam
void setup()
{
pinMode(sol_channel, OUTPUT);
Serial.begin(9600);
pinMode(start_Channel, OUTPUT);
Serial.begin(9600);
myservo.attach(9); // attach servo signal wire to pin 9
//Setup usb serial verbinding naar pc
Serial.begin(9600);
//Setup Bluetooth connective naar android
bluetooth.begin(9600);
}
void loop()
{
if (Serial.available() > 0)
{
command = Serial.read();
if (start)
{
digitalWrite(start_Channel, HIGH);
}
else
{
digitalWrite(start_Channel, LOW);
}
switch (command)
{
case 'L': start = true; break;
case 'l': start = false; break;
if (Serial.available() > 0)
{
command = Serial.read();
if (sol)
{
digitalWrite(sol_channel, HIGH);
}
else
{
digitalWrite(sol_channel, LOW);
}
switch (command)
{
case 'S': sol = true; break;
case 's': sol = false; break;
if (bluetooth.available() > 0 ) // sol ( solenoid )
{
int servopos = bluetooth.read(); // save the received number to servopos
Serial.println(servopos); // serial print servopos current number received from
bluetooth
myservo.write(servopos); // servo roteert naar gewenste hoek
}
}
}
}
}
}
I’m confused about another thing, which is better to state the servo pin, :
“#define servo_Channel 9 " before the void setup or " myservo.attach(9); // attach servo signal wire to pin 9” in the void setup
I’m getting another error with bleutooth : if (bluetooth.available() > 0 ) // sol ( solenoid )
C:\Users\Lenovo\Documents\Arduino\code_deel_een_revisie\code_deel_een_revisie.ino:8:1: warning: declaration does not declare anything [-fpermissive]
SoftwareSerial bluetooth(10, 11); // RX, TX - can be variables if you want
^~~~~~~~~~~~~~
C:\Users\Lenovo\Documents\Arduino\code_deel_een_revisie\code_deel_een_revisie.ino: In function 'void setup()':
code_deel_een_revisie:23:3: error: 'bluetooth' was not declared in this scope
bluetooth.begin(9600);
^~~~~~~~~
C:\Users\Lenovo\Documents\Arduino\code_deel_een_revisie\code_deel_een_revisie.ino: In function 'void loop()':
code_deel_een_revisie:61:19: error: 'bluetooth' was not declared in this scope
if (bluetooth.available() > 0 ) // sol ( solenoid )
^~~~~~~~~
code_deel_een_revisie:66:17: error: expected ';' before 'myservo'
myservo.write(servopos); // servo roteert naar gewenste hoek
^~~~~~~
exit status 1
'bluetooth' was not declared in this scope