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;
}
}
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");
sketch_Show_The_Hat.ino:77:5: error: expected declaration before ‘}’ token
Can some please help figure where this error is coming from?
Here is my code:
#include "LPD8806.h"
#include "SPI.h"
//#define HOLIDAY_MODE
#define MOMENTARY_SWITCH
// Example to control LPD8806-based RGB LED Modules in a strip!
int dataPin = 2;
int clockPin = 3;
int onOffPin = 4;
// Set the first variable to the NUMBER of pixels. 32 = 32 pixels in a row
// The LED strips are 32 LEDs per meter but you can extend/cut the strip
LPD8806 strip = LPD8806(32, dataPin, clockPin);
void setup() {
// Start up the LED strip
strip.begin();
// Update the strip, to start they are all 'off'
strip.show();
randomSeed(analogRead(A0));
// On/Off switch
pinMode(onOffPin, INPUT_PULLUP);
}
int x = 0;
int dim = 0;
#ifdef MOMENTARY_SWITCH
int tdim = 0;
#endif
void loop();
int r, g, b, y, z;
#ifdef MOMENTARY_SWITCH
} if (digitalRead(onOffPin) == LOW) {
if ((dim == 0) && (tdim == 0)
tdim = !0;
} else if ((dim == 128) && (tdim != 0))
tdim = 0;
if (tdim) {
if (dim < 128) {
dim++;
} else {
}
if (dim > 0) {
dim--;
}
} if (digitalRead(onOffPin) == LOW) {
}
}
} if (dim < 128) {
dim++;
} else {
if (dim > 0) {
dim--;
}
}
#endif
for (int i = 0; i < strip.numPixels(); i++) {
y = (x + (i << 5)) & 511;
#ifdef HOLIDAY_MODE
// Holiday
if (y >= 256) {
y = 511 - y;
}
z = 255 - y;
r = (y <= 128) ? (y - 128) << 1 : 0;
g = (z <= 128) ? (z - 128) << 1 : 0;
b = 0;
# else
// Normal
if (y < 128) {
r = y;
g = 0;
b = 255 - y;
} else if (y < 256) {
r = 127;
g = y - 128;
b = 0;
} else if (y < 384) {
r = 127;
g = 127 - (y - 256);
b = 0;
} else {
r = 127 - (y - 384);
g = 0;
b = y - 384;
}
#endif
if (dim == 0) {
r = 0;
g = 0;
b = 0;
} else if (dim < 128) {
r = (r * dim) / 128;
g = (g * dim) / 128;
b = (b * dim) / 128;
}
strip.setPixelColor(i, r & 127, g & 127, b & 127);
}
strip.show();
x += 2;
x &= 511;
delay(5);
// for(int j=0; j < random(100,200); j++ ) {
// int i = random(0,strip.numPixels()-2) + 1;
// strip.setPixelColor(i-1, 43,54,219);
// strip.setPixelColor(i, 255,255,0);
// strip.setPixelColor(i+1, 255,69,0);
// strip.setPixelColor(i-1, 0,0,255);
// strip.setPixelColor(i, 255,0,0);
// strip.setPixelColor(i+1, 0,255,0);
// strip.show();
// delay(5);
// strip.setPixelColor(i-1, 255,128,0);
// strip.setPixelColor(i-4, 255,128,0);
// strip.setPixelColor(i+1, 4,0,8);
// strip.setPixelColor(i-4, 104,10,80);
// strip.setPixelColor(i-10, 43,54,219);
// strip.setPixelColor(i-30, 255,165,0);
// strip.setPixelColor(i+5, 219,125,45);
// strip.setPixelColor(i, 100,100,127);
// strip.setPixelColor(i+1, 104,100,198);
// strip.show();
// delay(1);
// strip.setPixelColor(i+32, 219,112,147);
//
// strip.setPixelColor(i+1, 255,69,0);
// strip.setPixelColor(i-2, 255,127,75);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i+5, 255,128,0);
// strip.setPixelColor(i+2, 10,227,100);
// strip.show();
// delay(0);
// strip.setPixelColor(i-1, 255,69,0);
// strip.setPixelColor(i, 255,69,0);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i-12, 148,0,211);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i-3, 148,0,211);
// strip.setPixelColor(i-1, 148,0,211);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i+2, 148,0,211);
// strip.show();
// delay(random(10,100));
// }
// for(int j=0; j < random(10,15); j++ ) {
// for (int i=32; i < strip.numPixels(); i++) {
// strip.setPixelColor(i, 0,0,255);
// }
// strip.show();
// delay(15);
// for (int i=2; i < strip.numPixels(); i++) {
// strip.setPixelColor(i, 255,110,0);
// }
// strip.show();
// delay(15);
// }
First of all I have corrected your formatting with an edit. The error is saying that in 77. line 5. character there is a mistake related with } character. The mistake is you did not declared any { before }.
When I tried to correct your formatting (on my version of your file) 77. line is an empty line. But the problem is probably in 76. line } if (digitalRead(onOffPin) == LOW) { so try to remove { character. I mean try to replace the line with if (digitalRead(onOffPin) == LOW) { and let's see the error persists or not.
Curly Braces: https://www.arduino.cc/en/reference/braces
Reminding: Please always pay attention to your formatting on StackOverflow while asking a question. Formatting the text especially in the question is the key to properly interchange the knowledge. While sharing one of your code file in here, formatting is a necessity. Because if you did not format your text (your codes actually) we can not test them.
I am using an Arduino Mega to implement a Rectangle pattern movement of a device which mean the length and breadth should decrease each time it reaches the end edges. I have written the code but it is not working
//servo header
#include <Servo.h>
#include <Math.h>
Servo myservo;//servo Object
//motor pin config
int m1r = 9;
int m1f = 10;
int m2f = 12;
int m2r = 11;
//infrared counter
int ir1val=0;//pulse count from irval
int ir2val=0;//pulse count from irval
int len=1;
int bred=2;
//Different sensors used
int temp;
int buzz = 8;
const float radpulse = 0.05;//1 pulse = 0.05 meter
double pulsecount1;//pulse counter;
double pulsecount2;//pulse counter;
int pc1;//integer
int pc2;
void setup() {
// initialize the Motor pin as an output:
pinMode(m1f, OUTPUT);
pinMode(m1r, OUTPUT);
pinMode(m2f, OUTPUT);
pinMode(m2r, OUTPUT);
pinMode(buzz,OUTPUT);
myservo.attach(A0);//BLDC motor attachment
Serial.begin(9600);// standard serial bandwidth of all modules
}
void loop()
{
pulsecount1=len/radpulse;
pc1=(int)round(pulsecount1);
pulsecount2=bred/radpulse;
pc2=(int)round(pulsecount2);
pc1=3;
pc2=2;
myservo.write(30);
delay(5000);
labelL1:
if((pc2 == 0)&&(pc1 == 0))
{
goto Stop;
}
while(pc1 > ir1val)
{
temp=((5.0*analogRead(A3)*100)/1024);
if(temp >60)
{
// goto hibernate;
}
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
digitalWrite(m2f,HIGH);
digitalWrite(m1f,HIGH);
//digitalWrite(m2f,HIGH);
if(digitalRead(A2))
{
ir1val++;
}
}
if(pc1 == ir1val)
{
/*if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
pc1--;
ir1val=0;
digitalWrite(m1f, HIGH);
digitalWrite(m2f,LOW);
delay(5000);
goto labelB1;
}
labelB1:
while(pc2 != ir1val)
{
temp=((5.0*analogRead(A3)*100)/1024);
if(temp >60)
{
// goto label2;
}
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
digitalWrite(m1f,HIGH);
digitalWrite(m2f,HIGH);
if(digitalRead(A2))
{
ir2val++;
}
}
if(pc2 == ir2val)
{
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
pc2=pc2--;
ir2val=0;
digitalWrite(m1f, HIGH);
digitalWrite(m2f,LOW);
delay(5000);
goto labelL1;
}
Stop:
myservo.write(0);// stop servo
digitalWrite(m1f,LOW);// stop motor
digitalWrite(m2f,LOW);//stop motor
digitalWrite(buzz,HIGH);//Buzzer
delay(5000);
goto Stop;
/*
hibernate:
temp=((5.0*analogRead(A2)*100)/1024);
if(temp >40)
{
delay(1000);
goto hibernate;
}
*/}
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.