delay() line not allowing my Arduino code to run - arduino

The code is below
// initialize lights and sensors and valueHolders
int redN = 13;
int yellowN = 12;
int greenN = 11;
const int sensorN = A0;
int analogValueN = 0;
int redW = 10;
int yellowW = 9;
int greenW = 8;
const int sensorW = A1;
int analogValueW = 0;
int redS = 7;
int yellowS = 6;
int greenS = 5;
const int sensorS = A2;
int analogValueS = 0;
int redE = 4;
int yellowE = 3;
int greenE = 2;
const int sensorE = A3;
int analogValueE = 0;
//set thresholdValue;
const int threshValue = 200;
//initialize booleanSet for sectors
boolean bSet[] = {false, false, false, false};
void setBSet(){
if(analogValueN > threshValue) bSet[0] = false;
if(analogValueN < threshValue) bSet[0] = true;
if(analogValueW > threshValue) bSet[1] = false;
if(analogValueW < threshValue) bSet[1] = true;
if(analogValueS > threshValue) bSet[2] = false;
if(analogValueS < threshValue) bSet[2] = true;
if(analogValueE > threshValue) bSet[3] = false;
if(analogValueE < threshValue) bSet[3] = true;
// if (analogValueN > threshValue){
// bSet[0] = false;
// } else {
// bSet[0] = true;
// }
}
//setup outputs
void start(){
pinMode(redN, OUTPUT);
pinMode(yellowN, OUTPUT);
pinMode(greenN, OUTPUT);
analogValueN = analogRead(sensorN);
pinMode(redW, OUTPUT );
pinMode(yellowW, OUTPUT);
pinMode(greenW, OUTPUT);
analogValueW = analogRead(sensorW);
pinMode(redS, OUTPUT);
pinMode(yellowS, OUTPUT);
pinMode(greenS, OUTPUT);
analogValueS = analogRead(sensorS);
pinMode(redE, OUTPUT );
pinMode(yellowE, OUTPUT);
pinMode(greenE, OUTPUT);
analogValueE = analogRead(sensorE);
}
// set method for active sector
// ROS ==> red of sector
void active(int ROS){
//set sector as go
digitalWrite(ROS, LOW);
digitalWrite(ROS-1, LOW);
digitalWrite(ROS-2, HIGH);
Serial.println('Pin' + (ROS-2) + "Active High");
}
//set method for light action
void action(int ROS){
//set active for sector...
active(ROS);
//...and its complimentary sector
if (ROS>8){
active(ROS - 6);
} else {
active(ROS + 6);
}
}
void runTraffic(){
setBSet();
for(int i=0;i<4;i++){
if (bSet[i]){
action((3*i)+4);
// delay(8000);
}
}
}
void setup() {
// put your setup code here, to run once:
start();
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
runTraffic();
}
When i try to compile, it fails and error message is just "Error Compiling" and no reference to which line the error is coming from. But upon debugging i found the error to be in the delay line of the runTraffic() method. When i comment the line, the code compiles but it doesn't otherwise. But the delay works fine on other codes of mine.
What might be wrong with it?

There's lot of people have same problems with same IDE (probably that version is unstable).
I used the 1.6.4 to compile and no problem appear.
My advice is to unistall your Arduino IDE version (1.6.3) and install the 1.6.4 or something older and more stable.
Here you can download it and try which IDE works for you.
For now, i don't suggest the 1.6.5 (I've never tried it) but it could be a good candidate.

Related

switch block not executing correctly on arduino C++

I have a function that performs a switch on a single variable against some constants.
However, it's not executing correctly.
Using this code, the actionType is 4 (LED_ACTION) so falls into the "LED action" block.
const int ledPin = LED_BUILTIN;
// Actions
const int NULL_ACTION = 0;
const int SERVO_ACTION = 1;
const int SERVOOP_ACTION = 2;
const int BUZZER_ACTION = 3;
const int LED_ACTION = 4;
void setup() {
Serial.begin(9600);
int actionType = 4;
Serial.print("Action type: ");
Serial.println(actionType);
switch (actionType) {
case BUZZER_ACTION:
Serial.println("Buzzer action");
// int buzzerPitch = 123;
// int buzzerDuration = 1000;
// tone(buzzerPin, buzzerPitch, buzzerDuration);
break;
case LED_ACTION:
Serial.println("LED action");
int ledState = HIGH;
digitalWrite(ledPin, ledState);
break;
default:
Serial.println("Unknown action");
// Do Nothing
break;
}
int actionSleep = 1000;
Serial.print("Action sleeping for: ");
Serial.println(actionSleep);
delay(actionSleep);
}
void loop() {
}
This can be seen in the log output (and the LED flashing):
Action type: 4
LED action
Action sleeping for: 1000
The problem comes when I add anything else to the other statements.
Uncommenting a single assignment in the BUZZER_ACTION block causes it to... It seems to just skip the entire thing, not logging any of them, or the default action, or flashing the LED.
Action type: 4
Action sleeping for: 1000
Am I doing anything stupid here? What's going on?
This is running on an Arduino Uno r3, with the sketch uploaded from the Arduino IDE, and VSCode.
Thanks!
Arduino C++ does not like declarations inside the switch block, even if they're not conflicting.
Moving them outside works without issue.
const int ledPin = LED_BUILTIN;
// Actions
const int NULL_ACTION = 0;
const int SERVO_ACTION = 1;
const int SERVOOP_ACTION = 2;
const int BUZZER_ACTION = 3;
const int LED_ACTION = 4;
void setup() {
Serial.begin(9600);
int actionType = 4;
Serial.print("Action type: ");
Serial.println(actionType);
int buzzerPitch;
int buzzerDuration;
int ledState;
switch (actionType) {
case BUZZER_ACTION:
Serial.println("Buzzer action");
buzzerPitch = 123;
buzzerDuration = 1000;
tone(buzzerPin, buzzerPitch, buzzerDuration);
break;
case LED_ACTION:
Serial.println("LED action");
ledState = HIGH;
digitalWrite(ledPin, ledState);
break;
default:
Serial.println("Unknown action");
// Do Nothing
break;
}
int actionSleep = 1000;
Serial.print("Action sleeping for: ");
Serial.println(actionSleep);
delay(actionSleep);
}
void loop() {
}

expected unqualified-id before '{' token arduino led

I have looked at previous posts and can't find an answer to my question, here is my code:
const int buttonPin3 = 4;
const int ledPin3 = 11;
int buttonState3 = 0;
{
pinMode(ledPin3, OUTPUT);
pinMode(buttonPin3, INPUT);
}
{
buttonState3 = digitalRead(buttonPin3);
if (buttonState3 == HIGH) {
digitalWrite(ledPin3, HIGH);
} else {
digitalWrite(ledPin3, LOW);
}
}
This isn't all of my code, but only the part where the error shows up
just before the buttonState3 = digitalRead(buttonPin3); there is a { that is where the error shows for me.
In Arduino land, you generally need to provide an setup function which is called once when the program starts, and a loop function which will be called continuously while the program is running.
Your code appears to be missing the actual function declaration parts, instead opting for code blocks at file level, which causes that exact error:
testprog.cpp:6:5: error: expected unqualified-id before ‘{’ token
{
^
You'll need something like the following to get it to compile:
const int buttonPin3 = 4;
const int ledPin3 = 11;
int buttonState3 = 0;
void setup() { // << note here
pinMode(ledPin3, OUTPUT);
pinMode(buttonPin3, INPUT);
}
void loop() { // << and here
buttonState3 = digitalRead(buttonPin3);
if (buttonState3 == HIGH) {
digitalWrite(ledPin3, HIGH);
} else {
digitalWrite(ledPin3, LOW);
}
}

DISABLING VOIDS IN ARDUINO

Ok, basically I'm trying to enable and disable voids in the code. Here is the code:
what I'm trying to create here is a dimmer. I want to enable and disable some of the voids to create that. Also when the value of "brightness" changes in one of the "for" loops, I can't transfer that value to other loops. Please help
int sensorPin = A0;
int sensorValue = 0;
int LED = 10;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
if ( analogRead < 30){
Down() = false;
}
if ( analogRead > 31) {
Up() = false;
}
}
void Up () {
for ( int brightness = 0; sensorValue < 30; brightness = brightness + 5 ) {
analogWrite(LED, brightness);
delay(100);
}
}
void Down () {
for ( int brightness = brightness; sensorValue > 31; brightness = brightness - 5 ) {
analogWrite(LED, brightness);
delay(100);
}
}
Still does not know what you want do.
But if I undestand it right (from the comments), the code should be:
int sensorPin = A0;
int sensorValue = 0;
uint8_t lightBrightness = 0;
int LED = 10;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
// Serial.println(sensorValue);
if ( sensorValue < 30){
if(lightBrightness < 255) lightBrightness++;
analogWrite(LED, lightBrightness );
}
if ( sensorValue > 31) {
if(lightBrightness > 0) lightBrightness--;
analogWrite(LED, lightBrightness );
}
delay(100); // << just to see fading the light
}

why my audio sound is not playing in my arduino code but if i play it separately it started working good

I am trying to set the timer after which the audio will automatically played once but it is not playing anything just a noise but when i run the audio program separately it work perfectly good
This is my code please help me out where i am doing wrong.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include <TMRpcm.h>
#include <SPI.h>
int timer1_counter;
#define SD_ChipSelectPin 4
TMRpcm tmrpcm;
unsigned long time_now = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ledPin = 13;
const byte interruptPin1 = 2;
const byte interruptPin2 = 3;
int counter2 = 0;
volatile byte state = LOW;
int count = 0;
int limit = 0;
bool TimerFlag = false;
int deviceTime = 0;
int set = 5;
bool soundplayflag = false;
void setup()
{
lcd.begin();
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(interruptPin1, INPUT_PULLUP);
pinMode(interruptPin2, INPUT_PULLUP);
pinMode(set, INPUT_PULLUP);
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("Please Select:");
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 31250; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
// initialize timer1
}
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
if (soundplayflag == true)
{
counter2 = counter2+1;
Serial.println(counter2/2);
}
}
void loop()
{
//unsigned long currentMillis = millis();
int up = digitalRead(interruptPin1);
int down = digitalRead(interruptPin2);
int setbutton = digitalRead(set);
delay(230);
if (up == 0)
{
Serial.println("Entering up");
count++;
if (count >= 0) {
lcdprint(count);
}
}
else if (down == 0)
{
Serial.println("Entering down");
count--;
if (count >= 0) {
lcdprint(count);
}
}
else if (setbutton == 0)
{
Serial.println("Entering set");
soundplayflag=true;
deviceTime = count;
}
if (deviceTime > 0)
{
if (deviceTime == counter2)
{
soundplayflag=false;
Serial.println("Hello world");
PlaySound();
counter2 = 0;
}
}
}
int lcdprint(int a)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Please Select:");
lcd.setCursor (7,1);
lcd.print(a);
Serial.println(a);
lcd.setCursor (10,1);
lcd.print("Min");
}// end of lcdprint(int a)
void PlaySound()
{
for (int i = 0; i < 10; i++)
{
tmrpcm.setVolume(5);
tmrpcm.play("3.wav");
delay(1000);
}// end of for loop
}// end of void PlaySound()
my expected output is it should play the sound when i set the time
It seems TMRpcm uses Timer1, which conflicts with your TIMER1_COMPA_vect. That would explain why it's working if you run nothing else.
Maybe try to use #define USE_TIMER2?

Arduino Combination Button?

I am trying to set up an Arduino Uno plus a RelayShield along with 6 push buttons(standard), and 3 LED's(red, yellow, green). Now what I have code to do is to take in a button push combination that is 6 pushes long(order only matters for what is set in the array). I have everything wired up to a breadboard and all that other jazz. My problem is, none of the LED's are working(I followed detailed instructions from instructables - minus the code), and it doesn't matter how many button pushes there are, only button 3 triggers the relay.... Now, for simplicities sake, I didn't wire up all 6 buttons, and I just shortened the array down to 3 and adjusted accordingly(I didn't want to hook up 6 buttons right now). Can someone verify this code for me? Or tell me what is wrong with it? Thanks in advance!
//button pins
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
const int button5 = 6;
const int button6 = 7;
const int grnLed = 9;
const int redLed = 10;
const int yellowLed = 11;
const int openRelay = 8;
// how long is our code, kinda personal
const int codelen = 3;
//pin code values must match button pin values
char PIN[codelen] = {
'1', '2', '3'
};
// attempted combo
char attempt[codelen] = {
'0', '0', '0'
};
// attempt count
int z = 0;
void setup() {
// you've been setup
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(button6, INPUT);
pinMode(openRelay, OUTPUT);
// set pullup resistor for buttons
digitalWrite(button1, HIGH);
digitalWrite(button2, HIGH);
digitalWrite(button3, HIGH);
digitalWrite(button4, HIGH);
digitalWrite(button5, HIGH);
digitalWrite(button6, HIGH);
// set openRelay state to open or closed
digitalWrite(openRelay, LOW);
}
void correctPIN()
{
pulseLED(grnLed, 3000);
digitalWrite(openRelay, HIGH);
delay(2000);
digitalWrite(openRelay, LOW);
delay(2000);
z = 0;
}
void incorrectPIN()
{
pulseLED(redLed, 3000);
z = 0;
}
void checkPIN()
{
int correct = 0;
int i;
for (i = 0; i < codelen; i++)
{
if (attempt[i] == PIN[i])
{
correct++;
}
}
if (correct == codelen)
{
correctPIN();
}
else
{
incorrectPIN();
}
for (int zz = 0; zz < codelen; zz++)
{
attempt[zz] = '0';
}
}
void checkButton(int button){
if (digitalRead(button) == LOW)
{
while (digitalRead(button) == LOW) { } // do nothing
//convert int to string for tracking/compare
char buttStr = button + '0';
attempt[z] = buttStr;
z++;
//light up led so we know btn press worked
pulseLED(yellowLed, 500);
}
}
void pulseLED(int ledpin, int msec) {
digitalWrite(ledpin, HIGH);
delay(msec);
digitalWrite(ledpin, LOW);
}
void loop() {
// check buttons
checkButton(button1);
checkButton(button2);
checkButton(button3);
checkButton(button4);
checkButton(button5);
checkButton(button6);
//if number of buttons pressed, z, matches code/pin length then check
if (z >= codelen)
{
checkPIN();
}
}

Resources