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() {
}
Related
I'm working on a circuit that has two separate 4-bit binary counters with LEDs. I press a button and one counter begins counting to 15 in binary. I press a second button and the first counter pauses where it is and the second group of LEDs begin counting to 15 in binary. I got both counters working, but I can't get the first group to pause and the second to begin. I've tried using if statements with a boolean flag, but it messes up the first group of LEDs. How can I get ledPins1[] to pause when button2 is pressed, then resume when ledPins2[] finish?
int ledPin1[] = {2,3,4,5};
int ledPin2[] = {7,8,9,10};
int button1 = 11;
int button2 = 12;
boolean button1Last = LOW;
boolean button1Current = LOW;
boolean button2Last = LOW;
boolean button2Current = LOW;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
for(int i=0; i<4; i++)
{
pinMode(ledPin1[i], OUTPUT);
}
for(int i=0; i<4; i++)
{
pinMode(ledPin2[i], OUTPUT);
}
}
boolean waitForButtonPush1 (boolean lastStartSwitchState1)
{
boolean currentStartSwitchState1 = digitalRead(button1);
if(lastStartSwitchState1 != currentStartSwitchState1) delay(20);
currentStartSwitchState1 = digitalRead(button1);
return currentStartSwitchState1;
}
boolean waitForButtonPush2 (boolean lastStartSwitchState2)
{
boolean currentStartSwitchState2 = digitalRead(button2);
if(lastStartSwitchState2 != currentStartSwitchState2) delay(20);
currentStartSwitchState2 = digitalRead(button2);
return currentStartSwitchState2;
}
void loop()
{
button1Current = waitForButtonPush1(button1Last);
if(button1Last == LOW && button1Current == HIGH)
{
for (byte counter =0;counter<=15; counter++)
{
displayBinary(counter);
delay(500);
}
}
button2Current = waitForButtonPush2(button2Last);
if(button2Last == LOW && button2Current == HIGH)
{
for (byte counter =0;counter<=15; counter++)
{
displayBinary2(counter);
delay(500);
}
}
}
void displayBinary(byte numToShow)
{
for (int i =0;i<4;i++)
{
if (bitRead(numToShow, i)==1)
{
digitalWrite(ledPin1[i], HIGH);
}
else
{
digitalWrite(ledPin1[i], LOW);
}
}
}
void displayBinary2(byte numToShow)
{
for (int i =0;i<4;i++)
{
if (bitRead(numToShow, i)==1)
{
digitalWrite(ledPin2[i], HIGH);
}
else
{
digitalWrite(ledPin2[i], LOW);
}
}
}
Welcome to the world of embedded devices!
Getting a small microprocessor to do several things at the same time is a bit tricky.
The key is to never block. No calls to delay(), no sending large buffers on the serial port at 9600 bauds in one go, etc...
There are some simple techniques to do it, one of the most commonly used is finite state machines.
Let's analyse your app a bit.
2 similar dssplay counters, with delay
2 buttons, buttons usually need to be debounced, that also involves a delay.
Some code, for you to tinker with:
// ****************************
// pinout
static const byte ledPin1[] = { 2, 3, 4, 5 };
static const byte ledPin2[] = { 7, 8, 9, 10 };
constexpr byte button1 = 11; // using constexpr for these saves 2 bytes of RAM.
constexpr byte button2 = 12;
// ****************************
// Counter data
static constexpr unsigned int led_delay = 500; // 500 ms, for all counters.
// constexpr ?? arduino supports c++17. Not all features in the main .ino
// module and all features in .cpp modules.
// Hint: you could have a member variable in the structure below for delay,
// this would allow for counters running at different speeds, or add buttons
// to increase/decrease speed.
// we have only 2 states, but you could add more, like running
// backwards, or run a different chase pattern maybe?
enum class led_counter_state : byte
{
stopped,
running,
};
struct led_counter_data_t
{
led_counter_state state; // STATE
byte counter; // counter current value
unsigned int timestamp; // used for timing.
const byte* leds; // LED pins.
};
static led_counter_data_t led_counter[2];
void led_display_init()
{
for (byte i = 0; i < 2; ++i)
{
led_counter[i].state = led_counter_state::stopped;
led_counter[i].counter = 0;
led_counter[i].timestamp = 0;
}
led_counter[0].leds = ledPin1;
led_counter[1].leds = ledPin2;
}
// ****************************
// LED cotrrol
static void leds_display_value(const led_counter_data_t& cntr)
{
for (byte i = 0, val = cntr.counter; i < 4; ++i, val >>= 1)
digitalWrite(cntr.leds[i], val & 0x01);
}
static void leds_control(led_counter_data_t& cntr)
{
const auto now = (unsigned int)millis(); // keep track of time.
switch(cntr.state)
{
default: // something is wrong.. stop.
cntr.state = led_counter_state::stopped;
// fall through ...
case led_counter_state::stopped:
return; // if not running, do nothing
case led_counter_state::running:
if (now - cntr.timestamp >= led_delay) // check delay
{
if (++cntr.counter > 15) // advance counter.
cntr.counter = 0;
leds_display_value(cntr); // show value.
cntr.timestamp = now; // keep track of time.
}
break;
}
}
static void leds_start(led_counter_data_t& cntr)
{
if (cntr.state != led_counter_state::stopped)
return;
cntr.state = led_counter_state::running;
if (++cntr.counter > 15) // advance counter.
cntr.counter = 0;
led_display_value(cntr); // show value.
cntr.timestamp = (unsigned int)millis();
}
static void leds_stop(led_counter_data_t& cntr)
{
cntr.state = led_counter_state::stopped;
}
// ****************************
// switch inputs data
static constexpr byte switch_debounce_delay = 30; // 30ms is a good value for
// debouncing
struct switch_data_t
{
byte sw1_state : 1; // no need to waste more than 1 bit per switch
byte sw2_state : 1;
byte timestamp; // we'll only count to 30 ms, so 1 byte timestamp will do
};
static switch_data_t switch_data;
// ****************************
// switch inputs code
static void control_inputs()
{
const auto now = (byte)millis();
if (now - switch_data.timestamp < switch_debounce_delay)
return;
switch_data.timestamp = now;
// All switch control logic is regrouped here, and isolated
// form other control code, this makes the logic easier to
// write, read, and debug.
bool b = digitalRead(button1);
if (b & !switch_data.sw1_state) // button was pushed right now.
{
if (led_counter[0].state == led_counter_state::stopped)
{
leds_start(led_counter[0]); // start counter 1
leds_stop(led_counter[1]); // stop counter 2
}
else
{
leds_stop(led_counter[0]); // stop counter 1
}
}
switch_data.sw1_state = b;
b = digitalRead(button2);
if (b & !switch_data.sw2_state) // button was pushed right now.
{
if (led_counter[1].state == led_counter_state::stopped)
{
leds_start(led_counter[1]); // start counter 2
leds_stop(led_counter[0]); // stop counter 1
}
else
{
leds_stop(led_counter[1]); // stop counter 2
}
}
switch_data.sw2_state = b;
}
// ****************************
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
for (byte i = 0; i < 4; ++i)
{
digitalWrite(ledPin1[i], LOW);
pinMode(ledPin1[i], OUTPUT);
digitalWrite(ledPin2[i], LOW);
pinMode(ledPin2[i], OUTPUT);
}
led_display_init();
}
// ****************************
// The goal, always, is to exit loop() as fast as possible, so
// everything will run smoothly, and appear to run simultaneously.
void loop()
{
control_inputs();
leds_control(led_counter[0]);
leds_control(led_counter[1]);
}
I do not have an arduino with me, so I did not comppile nor ran this, but it should be pretty close. Let me know if you're having issues or have any questions.
i need to add millis in my code but i dont know exactly where should be ok to add millis or where is it acctually needed.Because i have used only delay till now can you help me and tell me where can millis be implemented in this piece of code.If it is possible to add millis somewhere in or after the button modes or switch case because it would be nicer if the modes changed properly because for now their changing either when the button is pressed normal or somtimes you need to pres the button twice to change to the next mode.I would apriciate all the help.
const int BUTTON_SWITCH = 8;
const int BUTTON_ALARM = 9;
const int KNOB = A0;
const int TEMP = A1;
const int tempRES = 10000; // the resistance of the NTC at 25'C is 10k ohm
const int NTC_MATERIAL_CONSTANT = 3950;
const int RED_LED = 4;
const int BUZZER = 3;
int index = 1;
double value;
int state = 0;
unsigned long time_now = 0;
int period = 1000;
char incomingOption;
#include "Display.h"
void setup() {
Serial.begin(9600);
pinMode(BUTTON_SWITCH, INPUT_PULLUP);
pinMode(BUTTON_ALARM, INPUT_PULLUP);
pinMode(RED_LED, OUTPUT);
}
float get_temperature()
{
float temperature, resistance;
int value;
value = analogRead(TEMP);
resistance = (float)value * tempRES / (1024 - value); // Calculate resistance
/* Calculate the temperature according to the following formula. */
temperature = 1 / (log(resistance / tempRES) / NTC_MATERIAL_CONSTANT + 1 / 298.15) - 273.15;
return temperature;
}
void loop() {
if (digitalRead(BUTTON_SWITCH) == LOW) { // button for switching the 3 modes
index = index + 1;
}
if (digitalRead(BUTTON_ALARM) == LOW) { // button for the alarm
displayAlarm();
}
if (Serial.available()) {
// Read entire buffer up to newline character
// Since on the C# side, serialPort1.WriteLine appends a newline character
String respond = Serial.readStringUntil('\n');
if (respond == "RESET") {
digitalWrite(RED_LED, LOW);
digitalWrite(BUZZER, LOW);
}
}
if (index > 3) { // when the code is on the last mode press the button to turn back to the first mode
index = 1;
}
get_temperature();
float celcius;
celcius = get_temperature();
if(celcius<16 || celcius>27){ // if the temperature becomes less than 16 degrees or goes higher than 27 degrees turn on the alarm
displayAlarm();
}
switch (index) {
case 1: displayTime(); break; // switch between the 3 different modes
case 2: displayTemp(); break;
case 3: displayAngle(); break;
}
}
void displayTime() {
float timer = Serial.parseFloat(); // take the current time from the c# application and display it on the arduino board
Display.show(timer);
}
void displayTemp() {
float celcius;
celcius = get_temperature(); // mode for displaying the current temperature
Display.show(celcius);
}
void displayAngle() {
int value = analogRead(KNOB); // read and save analog value from the potentionmeter
value = map(value, 0, 1023, 0, 30); // Map value 0-1023 to 0-30
Display.show(value);
}
void displayAlarm() {
Serial.println("Alarm");
digitalWrite(RED_LED, HIGH);
tone(BUZZER, 1500, 700);
}
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();
}
}
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.
I a trying to program an encoder using photo interrupts and a 7474 d flip flop. I believe my wiring is correct. It is simply wiring the photo interrupt to the arduino and having the pin out going through the 7474. Also one of the wires goes to pin 4 to digital read.
Below is my code. For some reason I can't get the photo interrupts to read the turn of the wheel.
const int clock = 0; //pin 2 is interrupt 0
const int dirPin = 4; //the number of the LED pin
//const int ledPin = 13;
int count = 0;
int dir = 0;
//int clockA = 0;
void setup(){
pinMode(dirPin, INPUT);
Serial.begin(9600);
attachInterrupt(clock, program, RISING);
}
void loop()
{
delay(50);
}
void program()
{
dir = digitalRead(dirPin);
if (dir == HIGH)
{
count ++;
}
else
{
count --;
}
Serial.println(count*30);
}
Don't put the Serial.println() in the interrupt handler. Inside a handler other interrupts are disabled. Do as little work as possible and return. The handler should just update the count and can mark a flag to tell the main loop to process.
int count = 0;
int newcounts = 0;
void loop() {
delay(100);
if (newcounts) {
Serial.println(count*30);
newcounts = 0
}
}
void program() {
// ... update count
newcounts = 1;
}