#include <Wire.h>
#include "DFRobot_LCD.h"
DFRobot_LCD lcd(16, 2); //16 characters and 2 lines of show
#define PH_PIN 25
float voltage,phvalue,temperature = 25;
float acidVoltage = (1990); //buffer solution at 4.o
float neutralVoltage = (1389); //buffer solution at 7.o
const int Sensor_MINvalue1 = 3.8; // +-0.5 from ph 4.0
const int Sensor_MAXvalue1 = 4.1;
const int Sensor_MINvalue2 = 6.8; // +-0.5 from ph 7.0
const int Sensor_MAXvalue2 = 7.1;
const int Sensor_MINvalue3 = 9.8; // +-0.5 from ph 10.0
const int Sensor_MAXvalue3 = 10.1;
void breath(unsigned char color){
for(int i=0; i<255; i++){
lcd.setPWM(color, i);
delay(5);
}
delay(500);
for(int i=254; i>=0; i--){
lcd.setPWM(color, i);
delay(1000);
}
delay (300);
}
void setup()
{
Serial.begin(11520);
// LCD Begin //
lcd.init();
lcd.setCursor(4,0);
lcd.print("Welcome");
delay(3000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Reading PH");
delay(5000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Please Wait");
delay(5000);
lcd.clear();
}
void loop()
{
static unsigned long timepoint = millis();
if(millis()-timepoint>1000U){
timepoint = millis();
// timepoint = read temperature
voltage = analogRead (PH_PIN)/4095.0*3300;
float slope = (7.0-4.0)/((neutralVoltage-1500)/30 - (acidVoltage-1500)/3.0);
float intercept = 7.0 - slope*(neutralVoltage-1500)/3.0;
phvalue = slope*(voltage-1500)/3.0 + intercept; // y=k*x + b [formula]
if( phvalue < Sensor_MAXvalue1 && phvalue > Sensor_MINvalue1 ){ //if( phvalue < 4.1 && phvalue > 3.8 )
Serial.print("Voltage:");
Serial.print(voltage,1);
Serial.print("PH:");
Serial.println(phvalue,2);
//LCD setting 16x2
lcd.setCursor(0,0);
lcd.print("PH : ");
lcd.print(phvalue,2);
lcd.setCursor(0,1);
lcd.print("Volt : ");
lcd.print(voltage/1000,2);
if( phvalue < Sensor_MINvalue1 && phvalue > Sensor_MAXvalue1 ){
lcd.setCursor(3,0);
lcd.print("Dosing is required");
delay(5000);
lcd.clear();
}
}
else if (phvalue < Sensor_MAXvalue2 && phvalue > Sensor_MINvalue2){ //if( phvalue < 7.1 && phvalue > 6.8 )
Serial.print("Voltage:");
Serial.print(voltage,1);
Serial.print("PH:");
Serial.println(phvalue,2);
//LCD setting 16x2
lcd.setCursor(0,0);
lcd.print("PH : ");
lcd.print(phvalue,2);
lcd.setCursor(0,1);
lcd.print("Volt : ");
lcd.print(voltage/1000,2);
if( phvalue < Sensor_MINvalue2 && phvalue > Sensor_MAXvalue2 ){
lcd.setCursor(3,0);
lcd.print("Dosing is required");
delay(5000);
lcd.clear();
}
}
else (phvalue < Sensor_MAXvalue3 && phvalue > Sensor_MINvalue3); { //if( phvalue < 10.1 && phvalue > 9.8 )
Serial.print("Voltage:");
Serial.print(voltage,1);
Serial.print("PH:");
Serial.println(phvalue,2);
//LCD setting 16x2
lcd.setCursor(0,0);
lcd.print("PH : ");
lcd.print(phvalue,2);
lcd.setCursor(0,1);
lcd.print("Volt : ");
lcd.print(voltage/1000,2);
if( phvalue < Sensor_MINvalue3 && phvalue > Sensor_MAXvalue3 ){
lcd.setCursor(3,0);
lcd.print("Dosing is required");
delay(5000);
lcd.clear();
}
}
}
}
that is my code, after applying the code it is not working, I am trying so that it reads a range of values so that it reads the ph value and show me the values for example if the ph = 6.8-7.1 it will show the ph, if the value is below or above it will print 'dosing is required but now it is not showing it. i am unsure if i should create a main so that i can seperate the if, else if, esle statemant
if( phvalue < Sensor_MINvalue1 && phvalue > Sensor_MAXvalue1 )
How can phvalue be smaller than your min and larger than your max at the same time?
You should use logical or || here.
Same for
if( phvalue < Sensor_MINvalue2 && phvalue > Sensor_MAXvalue2 )
and
if( phvalue < Sensor_MINvalue3 && phvalue > Sensor_MAXvalue3 )
Related
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:
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");
I've connected arduino to 4 different modules :
RTC
GPRS shield ( SARA G350 )
Wind vane
Anemometer
Everytime I launch my system the GPRS doesn't send anything unless I disconnect my anemometer.
I've tried the anemometer code on standalone and it works, I think the GPRS doesn't get enough power to send data when the anemometer is connected but how I'm not sure.
#include <SoftwareSerial.h>
#include <Wire.h>
#include <ds3231.h>
struct ts t;
String Angle;
int serial_in;
double x = 0;
double y = 0;
double a = 0;
double b = 0;
const int sensorPin = A5;
const int numReadings = 10;
int readings[numReadings];
int readIndex = 0;
int totalWind= 0;
int averageWind = 0;
int inputPin = A5;
int sensorValue = 0;
float sensorVoltage = 0;
float sensorVoltage2 = 0;
float windSpeed = 0;
float voltageConversionConstant = .004882814;
int sensorDelay = 2000;
float voltageMin = .4;
float windSpeedMin = 0;
float voltageMax = 2.0;
float windSpeedMax = 32;
SoftwareSerial mySerial(2, 3); //Tx & Rx sont connectés aux broches Arduino #7 et #8
void setup()
{
delay(15000);
Wire.begin();
DS3231_init(DS3231_INTCN);
//Commence la communication Serie
//Commence la communication Serie Arduino-Shield GPRS
mySerial.begin(9600);
delay(1000);
mySerial.println("AT"); //Handshaking
updateSerial();
mySerial.println("AT+UPSDA=2,0"); //Reset connexion
updateSerial();
delay(2000);
mySerial.println("AT+UPSD=2,1,\"sl2sfr\""); //Establissement de connexion avec l'APN
updateSerial();
delay(2000);
mySerial.println("AT+UPSDA=2,3");
updateSerial();
delay(2000);
mySerial.println("AT+UPSND=2,0");
updateSerial();
delay(2000);
}
void loop()
{
mySerial.println("AT+UPSDA=2,0"); //Reset connexion
updateSerial();
delay(2000);
mySerial.println("AT+UPSD=2,1,\"sl2sfr\""); //Establissement de connexion avec l'APN
updateSerial();
delay(2000);
mySerial.println("AT+UPSDA=2,3");
updateSerial();
delay(2000);
mySerial.println("AT+UPSND=2,0");
updateSerial();
delay(2000);
String Equipement = "STAINS";
mySerial.println("AT+UHTTP=0");
updateSerial();
delay(2000);
mySerial.println("AT+UHTTP=2,1,\"www.projetwmr.site\""); // Parametrage URL d'acces
updateSerial();
delay(2000);
String command = "AT+UHTTPC=2,5,\"/add.php\",\"post.ffs\",\"vite="; // Commande d'envoi des donnes via POST sur PHP
anemometre(); // Recuperation des données ANALOGIQUES
// convertion valeurs en String - Chaîne de caractères
command += String(windSpeed);
// or convertion précise
// command += String(Windspeed, 2);
command += "&equipement=";
command += String(Equipement);
DS3231_get(&t);
String heure = String(t.hour);
heure += ":";
heure += String(t.min);
heure += ":";
heure += String(t.sec);
String Date = String(t.year);
Date += "-";
Date += String(t.mon);
Date += "-";
Date += String(t.mday);
command += "&time=";
command += String(heure);
command += "&date=";
command += String(Date);
command += "&dire=";
girouette();
command += String(Angle);
command += "\",0"; //Fin de la commande PHP POST
mySerial.println(command);
updateSerial();
delay(45000);
}
void updateSerial()
{
delay(500);
while (Serial.available())
{
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(mySerial.available())
{
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
}
}
void girouette()
{
int sensorValue = analogRead(A1);
if (sensorValue >= 89 && sensorValue<=95 )
{ Angle="0°"; }
if (sensorValue >=60 && sensorValue<=70 )
{ Angle="22.5°"; }
if (sensorValue >=180 && sensorValue<=190 )
{ Angle="45°"; }
if (sensorValue >= 124 && sensorValue<=130 )
{ Angle="67.5°"; }
if (sensorValue >= 285 && sensorValue<=292 )
{ Angle="90°"; }
if (sensorValue >=240 && sensorValue<=250 )
{ Angle="115.5°"; }
if (sensorValue >= 630 && sensorValue<=640 )
{ Angle="135°"; }
if (sensorValue >= 600 && sensorValue<=610 )
{ Angle="157.5°"; }
if (sensorValue >=940 && sensorValue<=952 )
{ Angle="180°"; }
if (sensorValue >=825 && sensorValue<= 840 )
{ Angle="202.5°"; }
if (sensorValue >= 880 && sensorValue<= 898 )
{ Angle="225°"; }
if (sensorValue >= 700 && sensorValue<= 712 )
{ Angle="247.5°"; }
if (sensorValue >= 785 && sensorValue<= 795 )
{ Angle="270°"; }
if (sensorValue >= 405 && sensorValue<= 415)
{ Angle="292.5°"; }
if (sensorValue >= 460 && sensorValue<= 470 )
{ Angle="315°"; }
if (sensorValue >= 78 && sensorValue<=87 )
{ Angle="337.5°"; }
if (sensorValue >= 1000 && sensorValue<=50 )
{ Angle="Error"; }
delay(100);
}
void anemometre()
{
sensorValue = analogRead(A5);
totalWind = totalWind - readings[readIndex];
readings[readIndex] = sensorValue;
totalWind = totalWind + readings[readIndex];
readIndex = readIndex + 1;
sensorVoltage2 = sensorValue * voltageConversionConstant;
if (readIndex >= numReadings) {
readIndex = 0;
averageWind = totalWind / numReadings;
sensorVoltage = averageWind * voltageConversionConstant;
if (sensorVoltage <= voltageMin) {
windSpeed = 0;
} else {
windSpeed = ((sensorVoltage - voltageMin) * windSpeedMax / (voltageMax - voltageMin))*1.55;
}
}
x = windSpeed;
if (x >= y) {
y = x;
} else {
y = y;
}
a = sensorVoltage;
if (a >= b) {
b = a;
} else {
b = b;
}
}
If you don't have a reason to use it, you should avoid SoftwareSerial, it is known to be quite inefficient and it's most likely leading to issues with the other libraries you're loading.
Always use hardware serial ports if you have them available. See here for alternatives.
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;
}
}
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.