Hi, i have a little problem i don't know how to make my program to simultaneously make 2 output there the program (i'm a very beginner)
sketch_may25a.ino (657 Bytes)
Hi, i have a little problem i don't know how to make my program to simultaneously make 2 output there the program (i'm a very beginner)
sketch_may25a.ino (657 Bytes)
(deleted)
More members will see your code if you post your code as described in the forum guidelines. Also the guidelines have advice on how to ask a good question.
You need to embrace blink without delay, so that if the first sensor turns its led on you don't get stuck in that first delay() and can immediately read the second one, and so on.
If the first sensor turns its led on, capture the time it did so, and move on.
Then each time through loop() check if the time it needs to be on has elapsed, and if so turn it off.
Ditto for the others, with no delay() anywhere.
Hi bunchyearth23,
if everything is wired properly your code will create high-pulses 65 milliseconds long
and repeat this every 4*65milliseconds = 260 milliseconds.
a famous wording here is: if you want quick success don't do coding do splitting would ;-))
It is very OK to ask any kind of question as a beginner. The "learn to write code" with the help of a forum includes
to describe in detail what you want to do. Just written in normal sentences using normal words.
Uploading a sketch named "sketch_may25a.ino" gives the expression that you are working quick and dirty.
You sould give your file a self-explaining name.
So without a detailed description what your code should do we (the other forum-members) are forced to look into the glas-ball what you might want to do.
If you mean that these 65 milliseconds long high-pulses should occur at the exact same time and not one after the other
you use a variable and set single bits inside this variable inside your if-conditions
after setting up the bits in this variable you write this variable the IO-port.
As you are a beginner the description above arises a lot of new questions:
how do I set a single bit high?
how do I "write" a variable to a "port" etc.
In this posting I don't add this explanations so that you are encountering "having questions" to make very clear
that WE (the other forum-members) have still questions about what you want to do.
We (the other forum-members) are willing to help if you provide your part of the work. a detailed description of what you want to do.
As soon as you have done this and I will look up your thread again I enjoy helping with explanations.
best regards
Stefan
No need to do all that bit stuff.
sorry for not giving more detail so there it is,
so basically i want to do a 4 drum drumkit for clone hero a copy of guitar hero for pc
but i wanted to do it myself i used 4 piezos on the arduino but i din't know that the arduino UNO does not suport natively the keyboard library
so il use the digital pin to another controller that is recognized by the game but due to the structure of my code i can only do 1 at a time and i need multiple input capabilities the delay that i set in my code is the delay needed by the controller to pickup de signal.
finola_marsaili:
You need to embrace blink without delay, so that if the first sensor turns its led on you don't get stuck in that first delay() and can immediately read the second one, and so on.If the first sensor turns its led on, capture the time it did so, and move on.
Then each time through loop() check if the time it needs to be on has elapsed, and if so turn it off.
Ditto for the others, with no delay() anywhere.
This blink without delay-example is better than nothing. But it is hard to understand because everything is compressed to the max. The explanation is compressed to the max too.
That's why I don't like this democode. It should be explained much more in detail what is going on and what pitfalls and traps it provides. A very well explained example should use serial-output that shows the fast execution of the loop and the "delayed" execution of parts of the code that shall be delayed.
A second example should explain how to use multiple and different intervals.
best regards
Stefan
Hi bunchyearth23,
I'm sorry but your description is still not detailed enough. You write about "multiple"input capabilities.
Input to what? input into the arduino or do you mean the Arduino should output these 65 milliseconds high-pulses into the input of this other device?
best regards
Stefan
multiple input into the arduino and multiple output from the arduino to the controller and the output need these 65 milliseconds pulses for the controller to detect
In fact reading your later explanation, what you also need to do, is check for those inputs going over those thresholds, as opposed to being over. When any one of them goes over, save that time, turn the pin on, and move on.
Repeat for all pins.
When the "delay" has elapsed for any led, turn it off.
That will work for as many as you like, ala blink without delay, and each led will stay on for the required time independently of what the other inputs and outputs are doing.
To check the pin going over, rather than being over, check the technique in the state change detect tutorial. That example is for a button reading high or low, but although your pin is analog you're essentially making it digital by checking if it's over or under a threshold.
OK, if the output must be same down to a single the microsecond this can be done using port-manipulation.
If a glitch of some microseconds is acceptable you setup a variable for each output beeing low or high
after that setting all outputs to the variables state and then do one single delay(65) and set all outputs low after that delay.
Doing this with "normal" commands means that there is a short latency between recognising all inputs until outputs are set high (or stay low) and a microseconds short difference between the outputs beeing set high because there are for commands
digitalWrite(IOPinNr,state)
that are executed one after the other
Do you already understand how this can be coded?
best regards
Stefan
Thanks it works !
But it is possible to reduce to maximum possible any latency ?
Hi bunchyearth23,
you are always short on information. by writing thanks it works do you mean you have already coded it and you have checked that all for drumsounds came out of the speakers "at the same time"?
For shortest possible latency you would have to use a microcontroller of the highest frequency available and write code for it in not in c++ (like Arduino-IDE) but in assembler. Or even use an FPGA-chip.
But do you really need to cut down latency to nano-seconds???
You can do a test how long -ehm short the latency is:
define some variables unsigned long and assing the current millis()-value to them
first right before your if conditions
second right before starting to execute digitalWrite(,HIGH)
third right before starting to execute digitalWrite(,LOW)
fourth right after finishing to execute digitalWrite(,LOW)
after that printout always the difference between them
second - first
third - first
fourth - first
best regards
Stefan
Hi stefan no i dont need nano-seconds so it's okay.
it's for a rythm game so no sound come directly from the kit i'm trying to do so all is fine and working
the base problem of my code was that it can output only one signal at a time
so i modified it so il take all input if there is one and then apply the delay and after the delay i pass all the pin to low
here is a codeversion that makes use of constants for each output-pin.
the advantage of using constants is that there is only ONE place to change the number
if this should be nescessary
The code has additional commands for measuring the time it takes to execute the code
const int sens = 450;
int IOState0 = LOW;
int IOState1 = LOW;
int IOState2 = LOW;
int IOState3 = LOW;
const int IOPin0 = 2;
const int IOPin1 = 3;
const int IOPin2 = 4;
const int IOPin3 = 5;
unsigned long StartLoop;
unsigned long ReadingIsDone;
unsigned long DigitalWriteDone;
unsigned long LoopIsOver;
void setup() {
Serial.begin (115200);
pinMode(IOPin0, OUTPUT);
pinMode(IOPin1, OUTPUT);
pinMode(IOPin2, OUTPUT);
pinMode(IOPin3, OUTPUT);
digitalWrite(IOPin0, LOW);
digitalWrite(IOPin1, LOW);
digitalWrite(IOPin2, LOW);
digitalWrite(IOPin3, LOW);
}
void loop() {
StartLoop = millis();
if (analogRead(A0) >= sens)
{ IOState0 = HIGH; }
else
{ IOState0 = LOW; }
if (analogRead(A1) >= sens)
{ IOState1 = HIGH; }
else
{ IOState1 = LOW; }
if (analogRead(A2) >= sens)
{ IOState2 = HIGH; }
else
{ IOState2 = LOW; }
if (analogRead(A3) >= sens)
{ IOState3 = HIGH; }
else
{ IOState3 = LOW; }
ReadingIsDone = millis();
digitalWrite(IOPin0, IOState0);
digitalWrite(IOPin1, IOState1);
digitalWrite(IOPin2, IOState2);
digitalWrite(IOPin3, IOState3);
DigitalWriteDone = millis();
delay(65);
digitalWrite(IOPin0, LOW);
digitalWrite(IOPin1, LOW);
digitalWrite(IOPin2, LOW);
digitalWrite(IOPin3, LOW);
LoopIsOver = millis();
Serial.print("milliseconds to read in:");
Serial.println(ReadingIsDone - StartLoop);
Serial.print("milliseconds until DigitalWrite is Done:");
Serial.println(DigitalWriteDone - StartLoop);
Serial.print("milliseconds until LoopIsOver:");
Serial.println(LoopIsOver - StartLoop);
}
This code produces a lot of serial output very fast (every 65 milliseconds) the next three lines
If your PC gets a hickup from this pull out the USB-plug
best regards
Stefan