Hot answers tagged

12

Instead of using the high level digitalRead() function access the low level port registers. See this documentation. A port register is one byte and each bit represents one of the digital inputs on the arduino. Do something like this: pin_status = PIND; //Input port D (pins 0-7) button_1 = bitRead(pin_status, 2); // digital pin 2 button_2 = bitRead(...


11

There is a very easy solution to your problem, and it's called "interrupts". The processes inside loop method are executed synchronously, so always one read will be done before second one, and always one check will be done before a second one (even if they are inside one if statement) Fortunately there is a way of simulating asynchronous behavior in ATmega....


8

"Arduino" is just plain old C++ with (in some cases) bits removed and some helper functions and classes. For the smaller (lower powered AVR for example) there is no full STL in the C++ library, and some functions are of a reduced complexity (such as no floating point support in the printf family of functions by default). For OOP you don't need to ...


6

change >= and <= to > and < respectively. You want it to stop at 0, but your do-while will do another round, since 0>=0 is true. So your code only stops at -1 and 256. When legThreeBrightness is 256 it should be counting down, but your if (legThreeBrightness == 255){ doesn't detect this, so the code will continue counting up. I'd probably ...


5

First, I will give you some unsolicited advise about your coding style. Instead of writing: PORTB &= B11111110; ... PORTB |= B00000001; you could write: PORTB &= ~_BV(PB0); ... PORTB |= _BV(PB0); These are standard AVR idioms, and they make it clearer that you are toggling the pin PB0. Then, to answer your actual question, your delay loop does ...


4

As pointed out by Ignacio Vazquez-Abrams and jsotola, you need to use a state machine, like the one from the Blink Without Delay Arduino tutorial. Yours would be slightly more complicated because you need a way to turn it on and off, and because you use different on and off periods. Here is an example code that abstract the blinking logic into three ...


4

Serial.read() takes the first byte out of the serial buffer. Once it's executed, this byte is gone, so the Serial.read() in the next if statement will fetch the next byte (if available). What you can do is assign the result of Serial.read() to some variable before the first if statement, and then use that variable instead of Serial.read() in the if ...


4

The problem is that you add 15, and than if it's >= 240, you decrease 15, so it will not change (I wouldn't consider it haywire though). You can better use a direction sign. Below is only the relevant code (not the GPIO functionality). What it does in the loop is, in case up is true, it adds fifteen, otherwise it subtracts 15. When 255 is reached, it ...


3

Consider the sequence of values of "fadeIn". It starts at 8 and in incremented by 16. So: 8, 24, ... 248, 264, ... . It is never exactly 255. So you never break out of your "for" loop. There is only 1 delay in your example code. It is difficult to associate the described behavior in your question and the location of the delay. Arduino programming is ...


3

delay(0) apparently[*] acts like delay(2^32) or about 10^10 milliseconds. That may not be what you had in mind ... :) Try reducing the delay to 1 or use delayMicroseconds() to get something shorter. That should at least get you some repeatable results. (10^10 mSec is ~ 16.5 weeks, for those without a calculator handy). [*] I didn't read the code, nor ...


3

There are many, many problems with this code. The most fundamental flaw is that it uses linear buffers, where you should be using circular buffers instead. I'll come to this later. Now, if we look at the details, in the header file: #define Buf_OF 0x0200 This, and a few other macros, serve no purpose. Do not put "TODO" items in the code: put ...


3

I think you want after a random time, a 'Start' LED to be lit, and whoever presses his/her button first wins. There are some problems in your sketch: It seems even when a button is pressed to early, the button is still taken into account. You should check for this 'cheating'. So check directly after the 'start' LED is lit, that no buttons are pressed. If ...


3

I was not sure about posting this as an answer, since it is basically some more considerations on the three existing answers (for reference, they are Michel Keijzers's one - using digitalRead -, Craig's one - read the PIND variable at once -, Filip Franik's one - use interrupts), but it became quite long and so a single comment could not fit all of this. ...


3

Let's assume the switch is on pin 9, that the pinMode() has already been set, and that the switch reads HIGH for loop operation. #define MODE_SWITCH 9 #define DO_LOOP HIGH // This loop will execute once if the mode-switch is off, or // will execute continously if the mode-switch is on: do { servo_things(); stepper_things(); motor_things(); } while( ...


3

On a multiprocess system, like your typical desktop, you usually have way more processes than logical CPUs. When programming such systems you therefore have to avoid burning more CPU cycles than needed. Instead, when your program has nothing to do, it should just sleep, and leave the CPU available for other processes. On an Arduino, you only have one single ...


3

A microcontroller's execution path is a tight loop whether you like it or not. If you take a CPU and connect it to empty memory the CPU will execute each empty cell in turn and loop back to the beginning of memory when it reaches the end (assuming empty memory cells equate to NOP and covers the whole memory space). With no operating system, no scheduler, ...


3

Have you programmed OOP before? For alot of projects you can get away with not knowing it. I'll also point out that if you are just a beginner, I don't recommend starting off with OOP. Stick to simpler scripts (Trust me, OOP is a pain sometimes). If you still INSIST on starting your arduino knowledge with OOP, look up "C++ classes" or "C++ OOP&...


2

What you are lacking is the else concept: If a touch pad is pressed, then Turn the relay on else Turn the relay off Rather than turn the relay off every iteration, you only turn it off if the touch pad is not pressed. That can be written as: val_1 = digitalRead(touchPin1); if(val_1 ==1) { Serial.println("Pad 1 Touched"); digitalWrite(relayPin1, ...


2

The key will be to split up playCarMusicStart and instead create a playCarMusicStep that will do just one iteration of the loop and returns. Call it every time in loop() akin to blinkWithoutDelay. In playCarMusicStart you then setup the playing state so it starts playing correctly. And when you need to stop it you then stop the music by setting the correct ...


2

The % is the modulo operator in C/C++, which basically means: Divide the value to the left of the % by the value to the right of it Discard the quotient and keep only the remainder. For instance, 10 % 5 = 0, because 10/5 = 2 with no remainder. Whereas 11 % 5 = 1, because the remainder of the division is 1. In your case, the modulo operator is likely being ...


2

When I ran your code as you presented it in the chat, lp stayed at 128. When I commented-out the "light_L1()" call in "runfor()", lp decremented by 1/2 its value per step as you would expect. The issue is that lp is global and therefore, accessible by any other function. One of your lower functions is resetting it.


2

It will be created every iteration. Assuming, you are not using it to add to a list (e.g. to use in later loops), what you better can do is make it in a global, you also don't need to use 'new', thus: Object object; Also, if you do not delete/remove the object at the end of each while condition (with the delete keyword), you will lose the memory (and this ...


2

This is doing an assignment, not the comparison you are expecting if (led = 144 Need == for the comparison


2

I have to concur with frarugi87 here. The few microseconds taken by digitalRead() are completely inconsequential for your application. But let me add that, even if your players were super-humans capable of sub-millisecond reaction times, you are not giving an advantage to any of them if you alternatively check button 1, then button 2, then button&...


2

There is a standard C function for this, memcmp(). It performs a memory block compare and can be used like this: if (!memcmp(bt, rfid.serNum, sizeof(bt))) { lcd.clear(); lcd.print("card accepted"); digitalWrite(green_led,HIGH); digitalWrite(red_led,LOW); } else { ... } Ref. http://www.cplusplus.com/reference/cstring/memcmp/


2

Short answer: Yes, it is possible to update global variables from interrupt routines, but it comes also with problems. Long answer: First you have to understand, how interrupts work. You are using a timer interrupt and an external (pin) interrupt. The compiled program lies in the flash of the chip. At the start there are some special addresses for the ...


2

Putting all other possible issues aside such as pin and timing conflicts. Rename the sketch setup() and loop() functions, for instance, rgb_led_setup() and rgb_led_loop(), and peizo_setup() and peizo_loop(), then rewrite: void setup() { rgb_led_setup(); peizo_setup(); } void loop() { rgb_led_loop(); peizo_loop(); } Cheers!


2

your for() loop should end with with either "i=i+1" or "i++" or "i +=1". In your code you are just saying i+1, which doesn't actually affect "i". So the first loop continues endlessly, and the "voltage" variable (which should not exceed 255) continues to go into the "illegal" range. The arduino likely translates the illegal values to 0. Eventually, since '...


2

I added int btnPress which gets set on each button press. Then an if statement runs according to btnPress number. Thanks jsotola for the point in the right direction! #include "FastLED.h" int inPin = 8; // the number of the input pin int inPinTwo = 9; int inPinThree = 10; int outPin = 13; // the number of the output pin int state = HIGH; ...


2

Your code doesn't make sense. You have a while (1) statement, which will run forever. Inside the body of that statement, you never change the value of temp. An Arduino sketch's loop() function runs continuously. You should not have a while (1) loop inside your loop function. You should have a loop function something like this: void loop() { temp = //read ...


Only top voted, non community-wiki answers of a minimum length are eligible