Page 1 of 2 12 LastLast
Results 1 to 25 of 42

Thread: help with arduino visual c/c++ code writing

  1. #1
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    help with arduino visual c/c++ code writing

    I have a very cool Halloween project I am attempting to build this year. For those that don't know, i put up a pretty cool Halloween display every year. I by trade am an electrician so most of my props re done via hard-wired and individual items to manipulate the circuit and control the prop. that unfortunately gets pretty expensive. this year, I decided to try my luck at the arduino stuff.

    If you look on youtube.com and search for casa fear zombie as well as bakersben walker witch this is what I'm building. Instead of being 2 separate props though i want them to be a single prop that randomizes 4 different scenarios. If anyone has good knowledge of writing the code and can help me through let me know. I am having a great time figuring it out and am learning something new every day but i need to make sure this thing works before its needed. I am incorporating music into the prop using an ethernet shield as storage via the on-board sdcard. If its gets completed Ill post a video. Kids already don't like coming to the front door because its a pretty intense scene. These things will set me over the top.

    I've added a jumping spider that also protrudes outward 30 inches as it raises 28. I'm building 2 of these zombie props, one to act like the videos I suggested another that will tear his head off and flail back and forth using pneumatics.



  2. #2
    SEi User gp02a0083's Avatar
    Join Date
    Nov 2004
    Vehicle
    1989 Lx-i Hatchback
    Location
    Jackson, NJ
    Posts
    1,838

    Re: help with arduino visual c/c++ code writing

    as far as the randomization goes.

    say you have 4 props

    you would have a randomization from 1-4 and have something like

    #define Prop1 = 1;
    #define Prop2 = 2;
    #define Prop1 = 3;
    #define Prop2 = 4;


    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    int main()
    {
    srand((unsigned)time(0));
    int random_integer;
    int lowest=1, highest=4;
    int range=(highest-lowest)+;
    random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
    cout << random_integer << endl;
    }

    then you would have conditional if statements like

    if (random_integer == Prop1)
    { activate prop1}
    else

    this should "randomize" an integer, show you the integer, then if the randomized integer is equal to what you defined your prop , then it will activate the prop. add a few delays and loop it back, with a little bit of work this snippet of code will work for yah

    1989 Accord Lx-i hatchback (current DD project)
    1986 Olds Cutlass 442 clone (never ending project)
    3Geez resident body man
    Owner of Wreck-less auto body

  3. #3

    2oodoor's Avatar
    Join Date
    Jun 2006
    Vehicle
    86 LX JDM B20A
    Location
    Georgia-lina
    Posts
    9,062

    Re: help with arduino visual c/c++ code writing

    Ive always wanted to get in to this so Im glad I can observe...

  4. #4
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    Re: help with arduino visual c/c++ code writing

    sweet, well this what i would ultimatly like to happen.
    -When PIR is low(off) the arduino is playing a background sounds track from the ethernet shields sd card (lets call it track 1)
    -The LEDs will be fading random colors slow fade but not at full intesity.

    When the PIR is triggered,
    -The single prop will play one of 3 scripted scerarios in any random order or in the same order doesnt really matter. Each scenario will be timed 20 seconds.
    -The music played will change to the associated tracks, say 2-5 in relation to the scenario being played.
    Scenario 1
    -The Thrasher sequence starts.
    -LEDS on the prop light.
    -Music plays track 2.
    -RGB LEDS change from random fade to solid green.
    -Zombie eyes light up
    Scenario 2
    -Music plays track 3.
    -Zombie eyes light
    -RGB LEDS change to blue
    -Instead of the thrasher program, both soleniod outputs are tied as a single so they work in unison with another. Timing will have to be played with.
    Scenario 3
    -Music plays track 4
    -Zombie eyes light
    -RGB LEDS change to white fast strobe (timing will be played with)
    -evilusions program but faster to seems as zombie has gone from night of the living dead to dawn of the dead style


    This is the original thrasher code but I believe its written in basic and needs to be translated into c/c++
    Thrasher:
    FOR idx = 1 TO 3
    RANDOM lottery
    NEXT
    valves = lottery & %00000011

    IF valves = last THEN Thrasher ' no repeats
    last = valves ' save for next cycle

    PINS = valves
    RANDOM lottery
    delay = lottery // 251 + 100
    POT Adjust, 100, tAdj
    tDelay = delay
    tDelay = tDelay * tAdj / 255
    delay = delay + tDelay
    PAUSE delay
    timer = timer + delay
    IF timer < 10000 THEN Thrasher
    GOTO Reset

    here is the evilusions program but once again written into basic and needs to be changed to work with the arduino
    SYMBOL Trigger = PIN6 ' ULN is pull-down
    SYMBOL Audio = PIN5
    SYMBOL LED = PIN3 ' use V+/OUT2 terminals
    SYMBOL Shoulder2 = PIN1 ' use V+/OUT1 terminals
    SYMBOL Shoulder1 = PIN0 ' use V+/OUT0 terminals


    ' -----[ Constants ]-------------------------------------------------------

    SYMBOL IsOn = 1 ' for active-high in/out
    SYMBOL IsOff = 0 ' put back to low/off

    SYMBOL Yes = 1
    SYMBOL No = 0

    SYMBOL Baud = OT2400 ' baud serial
    SYMBOL ThrashTime = 15000

    ' -----[ Variables ]-------------------------------------------------------

    SYMBOL idx = B2
    SYMBOL valves = B3
    SYMBOL last = B4

    SYMBOL delay = W3
    SYMBOL timer = W4
    SYMBOL lottery = W5


    ' -----[ Initialization ]--------------------------------------------------

    Reset:
    PINS = %00000000 ' preset IO pins
    DIRS = %00000111 ' define IO pins

    PAUSE 10000 ' 10s inter-show delay


    ' -----[ Program Code ]----------------------------------------------------

    Main:
    timer = 0 ' reset timer

    Check_Trigger:
    RANDOM lottery ' randomize lottery value
    PAUSE 5 ' loop pad
    timer = timer + 5 * Trigger ' inc or clear timer
    IF timer < 100 THEN Check_Trigger ' wait for 0.1 sec input

    Start_Audio:
    Audio = IsOn
    PAUSE 100
    Audio = IsOff

    LED = IsOn

    Jump:
    PINS = %00000011 ' both shoulders on
    PAUSE 1000

    Thrasher:
    FOR idx = 1 TO 3 ' big stir
    RANDOM lottery
    NEXT
    valves = lottery & %00000011

    IF valves = last THEN Thrasher ' no repeats
    last = valves ' save for next cycle

    PINS = valves ' update should outputs
    RANDOM lottery ' restir
    delay = lottery // 251 + 100 ' delay 100 to 350 ms
    PAUSE delay
    timer = timer + delay ' update timer
    IF timer < ThrashTime THEN Thrasher ' thrash for 10 seconds
    GOTO Reset

    My next step is to verify all the pins and get them standardized so tracking the program will be easier. Unfortunatly the pins listed in the 2 programs arent going to match mine.

    If I have more time, id like to use another sensor to control another prop, much easier its mostly hardwired.

    if thats all taken care of Ill have a 3rd prop similar to the 1st but much larger in scale. He will pull his head off and bend back and forth at the waist before reseting at the normal position.

  5. #5
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    Re: help with arduino visual c/c++ code writing

    ROODOO, ITS ACTUALLY VERY EXCITING. Eevn with my many years of electrical knowledge and very limited electronics skills, the amount of tutorials and training materials posted on the web is huge. ive spent about a week reviewing code and reading various ways to write the code. Although i can honestly say i dont feel that i could write the code myself, i can say that its pretty easy to follow the sketches that have been scripted on other projects. im really enjoying it. I will also say that you may loose a few hours of sleep to experimentation.

  6. #6
    SEi User gp02a0083's Avatar
    Join Date
    Nov 2004
    Vehicle
    1989 Lx-i Hatchback
    Location
    Jackson, NJ
    Posts
    1,838

    Re: help with arduino visual c/c++ code writing

    you got the idea mykwikcoupe. I would suggest ditching the old code, to me it looks like basic, but its been a long time since i looked at VB.

    I would suggest drawing out a list and then work on a flow chart for each of the different functions that you want for each prop.

    You are correct that not every snippet of code can be used without a problem, changing the micro controller to another one you will have to "re-map" everything to the right ports. The C++ language is good for this as it is object oriented, unlike Java which is still object oriented but is mainly used for GUI's. C++ is fairly easy to learn due to the fact it uses variable definitions that humans can set. Try doing stuff like that in assembly code, my coworker is so stuck on the assembly and its archaic. Takes like 30 lines of code in assembly to do a simple loop that C can do it in one or two lines.

    you will need the data sheet for the micro-controller so that you get the right ports that you want. Most likely your gonna want to use a bunch of analog ports for your application.

    1989 Accord Lx-i hatchback (current DD project)
    1986 Olds Cutlass 442 clone (never ending project)
    3Geez resident body man
    Owner of Wreck-less auto body

  7. #7
    LXi User Buzo's Avatar
    Join Date
    Dec 2010
    Vehicle
    88 Honda Accord EX - Single Port EFI, 4 doors Aut.
    Location
    North of Mexico
    Posts
    836

    Re: help with arduino visual c/c++ code writing

    Did somebody say microcontrollers?


    Power is nothing without control

  8. #8

    Vanilla Sky's Avatar
    Join Date
    Apr 2002
    Vehicle
    1999 Penalty Box
    Location
    Palatka, Florida, United States
    Posts
    8,932

    Re: help with arduino visual c/c++ code writing

    Quote Originally Posted by Buzo View Post
    Did somebody say microcontrollers?
    Nope, I think they said that Buzo could help :P

  9. #9
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    Re: help with arduino visual c/c++ code writing

    yes exactly. I'm starting to compile various codes from library stored sketches. I'm sure anyone with the background will stop and seriously LOL at what it looks like. I'm sure its way out of sequence. This thread could probably end up being super long if I post my entire sketch each time i ask for help. is there a way I could IM anyone here while I'm working on it and you could help walk me through it.

    Here's what i have so far for the 1st part of the sketch. Its the PIR warming up for 30 seconds, eliminating false triggers and resetting after each use. I am also including a fade program to accent light the props when the PIR is low (not triggered). When the PIR changes to High I want to add a sequence to change the ambient LEDS to match the color for that sequence. I also need to somehow figure out how to add music. Id like to make it a header file but its going to sequence multiple tracks throughout the program. Anyways start laughing!!!!

    //VARS
    //the time we give the sensor to calibrate (30 secs according to the datasheet)
    int calibrationTime = 30;

    //the time when the sensor outputs a low impulse
    long unsigned int lowIn;

    //the amount of milliseconds the sensor has to be low
    //before we assume all motion has stopped
    long unsigned int pause = 5000;

    boolean lockLow = true;
    boolean takeLowTime;

    int pirPin = 6; //the digital pin connected to the PIR sensor's output
    int ledPin = 13; //onboard test led to check PIR function
    int relay1pin = xx; //prop 1 solenoid 1
    int relay2pin = xx; //prop 1 solenoid 2
    int relay3pin = xx; //prop 2 solenoid 1
    int relay4pin = xx; //prop 2 solenoid 2
    int zombie1Redpin = xx; //prop 1 ambient led
    int zombie1Bluepin = xx; //prop 1 ambient led
    int zombie1Greenpin = xx; //prop 1 ambient led
    int zombie1eyepin = xx; //prop 1 eye led
    int zombie2Redpin = xx; //prop 2 ambient led
    int zombie2Bluepin = xx; //prop 2 ambient led
    int zombie2Greenpin = xx; //prop 2 ambient led
    int zombie2eyespin = xx; //prop 2 eye led

    /////////////////////////////
    //SETUP
    void setup(){
    Serial.begin(9600);
    pinMode(pirPin, INPUT);
    pinMode(ledPin, OUTPUT);
    digitalWrite(pirPin, LOW);

    //give the sensor some time to calibrate
    Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
    Serial.print(".");
    delay(1000);
    }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
    }

    ////////////////////////////
    //LOOP
    void loop(){

    if(digitalRead(pirPin) == HIGH){
    digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
    if(lockLow){
    //makes sure we wait for a transition to LOW before any further output is made:
    lockLow = false;
    Serial.println("---");
    Serial.print("motion detected at ");
    Serial.print(millis()/1000);
    Serial.println(" sec");
    delay(50);
    }
    takeLowTime = true;
    }

    if(digitalRead(pirPin) == LOW){
    digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
    //Is this where the code that randomizes the props sketches (posted above in a previous reply)would go as well?

    if(takeLowTime){
    lowIn = millis(); //save the time of the transition from high to LOW
    takeLowTime = false; //make sure this is only done at the start of a LOW phase
    }
    //if the sensor is low for more than the given pause,
    //we assume that no more motion is going to happen
    if(!lockLow && millis() - lowIn > pause){
    //makes sure this block of code is only executed again after
    //a new motion sequence has been detected
    lockLow = true;
    Serial.print("motion ended at "); //output
    Serial.print((millis() - pause)/1000);
    Serial.println(" sec");
    delay(50);
    }
    }
    }

    //This example shows how to fade an LED on pin 9, 10, 11 on zombie 1
    //using the analogWrite() function.

    float RGB1[3];
    float RGB2[3];
    float INC[3];

    int red, green, blue;

    int zombie1Redpin = 9; //prop 1 ambient led
    int zombie1Bluepin = 10; //prop 1 ambient led
    int zombie1Greenpin = 11; //prop 1 ambient led


    void setup()
    {
    Serial.begin(9600);
    randomSeed(analogRead(0));

    for (int x=0; x<3; x++) {
    RGB1[x] = random(256);
    RGB2[x] = random(256); }

    }

    void loop()
    {
    randomSeed(analogRead(0));

    for (int x=0; x<3; x++) {
    INC[x] = (RGB1[x] - RGB2[x]) / 256; }

    for (int x=0; x<256; x++) {

    red = int(RGB1[0]);
    green = int(RGB1[1]);
    blue = int(RGB1[2]);

    analogWrite (zombie1RedPin, red);
    analogWrite (zombie1GreenPin, green);
    analogWrite (zombie1BluePin, blue);
    delay(250);

    for (int x=0; x<3; x++) {
    RGB1[x] -= INC[x];}

    }

    for (int x=0; x<3; x++) {
    RGB2[x] = random(956)-700;
    RGB2[x] = constrain(RGB2[x], 0, 255);

    delay(1000);
    }

    //This example shows how to fade an LED on pin xx, xx, xx on zombie 2
    //using the analogWrite() function.

    float RGB1[3];
    float RGB2[3];
    float INC[3];




    int red, green, blue;




    int zombie2Redpin = xx; //prop 1 ambient led
    int zombie2Bluepin = xx; //prop 1 ambient led
    int zombie2Greenpin = xx; //prop 1 ambient led





    void setup()
    {
    Serial.begin(9600);
    randomSeed(analogRead(0));

    for (int x=0; x<3; x++) {
    RGB1[x] = random(256);
    RGB2[x] = random(256); }




    }

    void loop()
    {
    randomSeed(analogRead(0));

    for (int x=0; x<3; x++) {
    INC[x] = (RGB1[x] - RGB2[x]) / 256; }

    for (int x=0; x<256; x++) {

    red = int(RGB1[0]);
    green = int(RGB1[1]);
    blue = int(RGB1[2]);




    analogWrite (zombie2RedPin, red);
    analogWrite (zombie2GreenPin, green);
    analogWrite (zombie2BluePin, blue);
    delay(250);




    for (int x=0; x<3; x++) {
    RGB1[x] -= INC[x];}

    }

    for (int x=0; x<3; x++) {
    RGB2[x] = random(956)-700;
    RGB2[x] = constrain(RGB2[x], 0, 255);

    delay(1000);
    }
    as you can see one is written in one format, the other in another. I understand what each sketch is trying to accomplish but don't understand how they could be written in different syntax and both still work. I guess that's the brains of computers. I don't know if all my integers have to be at the top of the 1st sketch or if they can be written throughout the sketch at each step when needed for the 1st time. Ill fill in the 'XX' values when the boards show up and i can solidly confirm the accurate pins. I have them written down at work so maybe Ill edit the thread in the morning to reflect the proper values. Just to confirm, an output is an output no matter what the output is. I was planning on using the PWM outputs as speaker outputs to self powered speakers located on the props. I can in theory play multiple tracks located on the onboard sd card at the same time as the chipset is only performing the function of an output. Sorry its so long and I'm a noob. I can reply to the post 20+ times a day if you want to help expedite the process. I check it very often. I am watching many tutorials as well. The current one is How to write in CPP.
    Last edited by mykwikcoupe; 10-04-2012 at 08:40 PM.

  10. #10
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    Re: help with arduino visual c/c++ code writing

    //This example shows how to fade an LED on pin 9, 10, 11 on zombie 1
    //using the analogWrite() function.

    float RGB1[3];
    float RGB2[3];
    float INC[3];

    int red, green, blue, red1, green1, blue1;

    int zombie1Redpin = 9; //prop 1 ambient led
    int zombie1Bluepin = 10; //prop 1 ambient led
    int zombie1Greenpin = 11; //prop 1 ambient led
    int zombie2Redpin = xx; //prop 2 ambient led
    int zombie2Bluepin = xx; //prop 2 ambient led
    int zombie2Greenpin = xx; //prop 2 ambient led



    void setup()
    {
    Serial.begin(9600);
    randomSeed(analogRead(0));

    for (int x=0; x<3; x++) {
    RGB1[x] = random(256);
    RGB2[x] = random(256); }

    }

    void loop()
    {
    randomSeed(analogRead(0));

    for (int x=0; x<3; x++) {
    INC[x] = (RGB1[x] - RGB2[x]) / 256; }

    for (int x=0; x<256; x++) {

    red = int(RGB1[0]);
    green = int(RGB1[1]);
    blue = int(RGB1[2]);
    red1 = int(RGB1[3]);
    blue1 = int(RGB1[4]);
    green1 = int(RGB1[5]);

    analogWrite (zombie1RedPin, red);
    analogWrite (zombie1GreenPin, green);
    analogWrite (zombie1BluePin, blue);
    analogWrite (zombie2RedPin, red1);
    analogWrite (zombie2GreenPin, green1);
    analogWrite (zombie2BluePin, blue1);

    delay(250);

    for (int x=0; x<3; x++) {
    RGB1[x] -= INC[x];}

    }

    for (int x=0; x<3; x++) {
    RGB2[x] = random(956)-700;
    RGB2[x] = constrain(RGB2[x], 0, 255);

    delay(1000);
    }
    could the above listed code be relabeled like this to randomize both sets of RGB on the props?
    Last edited by mykwikcoupe; 10-04-2012 at 08:57 PM.

  11. #11
    LXi User Buzo's Avatar
    Join Date
    Dec 2010
    Vehicle
    88 Honda Accord EX - Single Port EFI, 4 doors Aut.
    Location
    North of Mexico
    Posts
    836

    Re: help with arduino visual c/c++ code writing

    Could you please tell me details about your hardware? Put a link, pictures, schematics, or anything to help me understand how your controller looks like. I did read your code and I was able to understand it and follow the sequence.

    Next reply you can use the # (wrap code) above to keep the indents of your code

    Code:
    for(x=0;x<30;x++) {
        do_something
    }


    Power is nothing without control

  12. #12
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    Re: help with arduino visual c/c++ code writing

    http://www.sainsmart.com/evaluation-...8u2-w5100.html
    That is the board and ethernet shield will be using. It says its based on the arduino mega board style so that is what i have been using to gather pin data.

    http://www.arduino.cc/en/Main/ArduinoBoardMega2560
    That is the information on the arduino mega. My kit hasnt physically shown up yet.

    http://www.arduino.cc/en/Main/ArduinoEthernetShield
    That is the arduino ethernet shield as well. Once again, it was purchased for the ability to use the sd card to store music files to be played throughout each sequence.

  13. #13
    LXi User Buzo's Avatar
    Join Date
    Dec 2010
    Vehicle
    88 Honda Accord EX - Single Port EFI, 4 doors Aut.
    Location
    North of Mexico
    Posts
    836

    Re: help with arduino visual c/c++ code writing

    Wow, that board is great! It does the same I do but I have to build the board from scratch for every application.

    Now tell me, Have you practiced the "blinking LED" example with this board? I assume yes, but if not, its good idea to start from there to make sure your programmer and connections are OK.

    Sorry, I just read that you haven't received the board. So you know what's going to be the first step.

    Code:
    int ledPin = 13;                 // LED connected to digital pin 13
    
    void setup()
    {
      pinMode(ledPin, OUTPUT);      // sets the digital pin as output
    }
    
    void loop()
    {
      digitalWrite(ledPin, HIGH);   // sets the LED on
      delay(1000);                  // waits for a second
      digitalWrite(ledPin, LOW);    // sets the LED off
      delay(1000);                  // waits for a second
    }
    Last edited by Buzo; 10-06-2012 at 05:56 PM.


    Power is nothing without control

  14. #14
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    Re: help with arduino visual c/c++ code writing

    Yes exactly. I have a series of about 15 indivodual programs to try. I have the concept and the wiring knowhow. If I knew the program code id be set. I wanted to biy the actual arduino boards the prive was about double

  15. #15
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    Re: help with arduino visual c/c++ code writing

    #include <iostream>
    #include <ctime>
    #include <cstdlib>

    int pirPin = 6;
    int ledpin = 13;
    int relay1pin = 21; //solenoid 1
    int relay2pin = 22; //solenoid 2
    int relay3pin = 23; //solenoid 3
    int relay4pin = 24; //solenoid 4

    void setup(){
    pinMode(pirPin, INPUT);
    pinMode(ledPin, OUTPUT);
    pinMode (relay1pin, OUTPUT);
    pinMode (relay2pin, OUTPUT)
    pinMode (relay3pin OUTPUT);
    pinMode (relay4pin OUTPUT);

    #define relay1 = 1;
    #define relay2 = 2;
    #define relay3 = 3;
    #define relay4 = 4;


    int main()

    srand((unsigned)time(0));
    int random_integer;
    int lowest=1, highest=4;
    int range=(highest-lowest)+;
    random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
    cout << random_integer << endl;
    }

    void loop()
    {

    if (random_integer == relay1)
    (activate prop1)

    if (random_integer == relay2)
    (activate prop2)

    if (random_integer == relay3)
    (activate prop3)

    if (random_integer == relay4)
    (activate prop4)
    }
    else

    this is what i have come up with so far. Obviously i have changed the naming convention.

    In the end sequence, is that correct or should it say

    else
    if (activate prop) sequence?

    should my relay1-4 be labelled to be relay1pin etc to match the naming convention the entire way through?

    under the srand((unsigned)time(0));, cant his be changed to any random delay? the way im reading this, the relays will click with instantaneos time delay. Can i make this any value to match the needs of the program?

    Thank you for the help. This code has been verified for proper format but i cant verify functionality as my equipment hasnt shown up yet.

  16. #16
    LXi User Buzo's Avatar
    Join Date
    Dec 2010
    Vehicle
    88 Honda Accord EX - Single Port EFI, 4 doors Aut.
    Location
    North of Mexico
    Posts
    836

    Re: help with arduino visual c/c++ code writing

    should my relay1-4 be labelled to be relay1pin etc to match the naming convention the entire way through?
    No because Relay1-4 are constants and relay1pin are variables.
    It doesn't matter how you name stuff, as long as you remember it as your program grows up.

    under the srand((unsigned)time(0));, cant his be changed to any random delay?
    I would need to check how this random generator works, but basically you use a random generator to get any value between 1 and 4 repeated through the time with no specific order or pattern. I understand you want to introduce a delay to the equation, so besides the random order, you would like a random delay between generated numbers, right?

    ..the relays will click with instantaneos time delay.
    No, only the prop equals to the randomly generated value will be activated.

    Something that I guess is missing is that you need to deactivate a prop that was activated in the step before, unless you want them all at the same time, but still you need to add something to your code to deactivate them.

    for instance:

    if (random_integer == relay1)
    (activate prop1)
    (deactivate prop2)
    (deactivate prop3)
    (deactivate prop4)

    if (random_integer == relay2)
    (activate prop2)
    (deactivate prop1)
    (deactivate prop3)
    (deactivate prop4)

    if (random_integer == relay3)
    (activate prop3)
    (deactivate prop2)
    (deactivate prop1)
    (deactivate prop4)

    if (random_integer == relay4)
    (activate prop4)
    (deactivate prop2)
    (deactivate prop3)
    (deactivate prop1)


    Power is nothing without control

  17. #17
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    Re: help with arduino visual c/c++ code writing

    Yes the random delay would be nice as well. I suppose it would ha e a maximumvalue or 3 seconds and a minimum value of 250ms. Thats great. Thanks for the help

  18. #18
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    Re: help with arduino visual c/c++ code writing

    randOn = random (100, 3000); // generate ON time between 0.1 and 3 seconds
    randOff = random (200, 3000); // generate OFF time between 0.2 and 3 seconds
    digitalWrite(relay1-4, HIGH); // sets the relay on
    delay(randOn); // waits for a random time while ON
    digitalWrite(relay1-4, LOW); // sets the relay off
    delay(randOff); // waits for a random time while OFF

    would this work as a random delay on and off work? Im not sure if it can be as simple as placing the (min, max) and be done or if you need a comparison (min, max, 100, 3000)? Ive also read that you can do a random seed but that looks a bit on the complex side as well.

    Could this also be written into the program as:
    if (random_integer == relay1);
    (relay1 == high);
    delay(randOn);
    (relay2 == low);
    delay(randOff);
    (relay3 == low);
    delay(randOff);
    (relay4 == low);
    delay(randOff);

    if (random_integer == relay2);
    (relay2 == high);
    delay(randOn);
    (relay1 == low);
    delay(randOff);
    (relay3 == low);
    delay(randOff);
    (relay4 == low);
    delay(randOff);

    (would these be separated by (if) or (else)

    Sorry if Im asking improper questions or if non of this makes sense. Im entering my second week of research and training on my own with alot of internet help and co worker help.
    Last edited by mykwikcoupe; 10-08-2012 at 04:57 PM.

  19. #19
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    Re: help with arduino visual c/c++ code writing

    #include <iostream>
    #include <ctime>
    #include <cstdlib>

    int pirPin = 6; //the digital pin connected to the PIR sensor's output
    int ledPin = 13; //onboard test led to check PIR function
    int relay1pin = 21; //solenoid 1
    int relay2pin = 22; //solenoid 2
    int relay3pin = 23; //solenoid 3
    int relay4pin = 24; //solenoid 4
    int zombie1Redpin = 9; //prop 1 ambient led
    int zombie1Bluepin = 10; //prop 1 ambient led
    int zombie1Greenpin = 11; //prop 1 ambient led
    int zombie1eyepin = 12; //prop 1 eye led
    int zombie2Redpin = 50; //prop 2 ambient led
    int zombie2Bluepin = 51; //prop 2 ambient led
    int zombie2Greenpin = 52; //prop 2 ambient led
    int zombie2eyespin = 53; //prop 2 eye led

    //VARS
    //the time we give the sensor to calibrate (30 secs according to the datasheet)
    int calibrationTime = 30;

    //the time when the sensor outputs a low impulse
    long unsigned int lowIn;

    //the amount of milliseconds the sensor has to be low
    //before we assume all motion has stopped
    long unsigned int pause = 5000;

    boolean lockLow = true;
    boolean takeLowTime;



    /////////////////////////////
    //SETUP
    void setup()
    {
    Serial.begin(9600);
    pinMode(pirPin, INPUT);
    pinMode(ledPin, OUTPUT);
    digitalWrite(pirPin, LOW);
    pinMode(pirPin, INPUT);
    pinMode(ledPin, OUTPUT);
    pinMode (relay1pin, OUTPUT);
    pinMode (relay2pin, OUTPUT);
    pinMode (relay3pin, OUTPUT);
    pinMode (relay4pin, OUTPUT);
    pinMode (zombie1Redpin, OUTPUT);
    pinMode (zombie1Bluepin, OUTPUT);
    pinMode (zombie1Greenpin, OUTPUT);
    pinMode (zombie1eyepin, OUTPUT);
    pinMode (zombie2Redpin, OUTPUT);
    pinMode (zombie2Bluepin, OUTPUT);
    pinMode (zombie2Greenpin, OUTPUT);
    pinMode (zombie2eyespin, OUTPUT);


    //give the sensor some time to calibrate
    Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++)
    Serial.print(".");
    delay(1000);

    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
    }

    ////////////////////////////
    //LOOP
    void loop(){

    if(digitalRead(pirPin) == HIGH)
    digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
    if(lockLow)
    //makes sure we wait for a transition to LOW before any further output is made:
    lockLow = false;
    Serial.println("---");
    Serial.print("motion detected at ");
    Serial.print(millis()/1000);
    Serial.println(" sec");
    delay(50);

    takeLowTime = true;


    if(digitalRead(pirPin) == LOW)
    digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
    //Is this where the code that randomizes the props sketches (posted above in a previous reply)would go as well?

    if(takeLowTime)
    lowIn = millis(); //save the time of the transition from high to LOW
    takeLowTime = false; //make sure this is only done at the start of a LOW phase

    //if the sensor is low for more than the given pause,
    //we assume that no more motion is going to happen
    if(!lockLow && millis() - lowIn > pause)
    //makes sure this block of code is only executed again after
    //a new motion sequence has been detected
    lockLow = true;
    Serial.print("motion ended at "); //output
    Serial.print((millis() - pause)/1000);
    Serial.println(" sec");
    delay(50);



    #define relay1 = 1;
    #define relay2 = 2;
    #define relay3 = 3;
    #define relay4 = 4;


    int main();

    srand((unsigned)time(0));
    int random_integer;
    int lowest=1, highest=4;
    int range=(highest-lowest)+;
    random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
    cout << random_integer << endl;


    if (random_integer == relay1);
    (relay1 == high);
    delay(randOn);
    (relay2 == low);
    delay(randOff);
    (relay3 == low);
    delay(randOff);
    (relay4 == low);
    delay(randOff);

    if (random_integer == relay2);
    (relay2 == high);
    delay(randOn);
    (relay1 == low);
    delay(randOff);
    (relay3 == low);
    delay(randOff);
    (relay4 == low);
    delay(randOff);

    if (random_integer == relay3);
    (relay1 == high);
    delay(randOn);
    (relay1 == low);
    delay(randOff);
    (relay2 == low);
    delay(randOff);
    (relay4 == low);
    delay(randOff);

    if (random_integer == relay4);
    (relay4 == high);
    delay(randOn);
    (relay1 == low);
    delay(randOff);
    (relay2 == low);
    delay(randOff);
    (relay3 == low);
    delay(randOff);
    }
    Thats what I have currently. Does it make sense and will it work. I think what someone needs to create is a arduino gui simulator that you can real time load and test to without physically wiring anything. It would have to assume all electrical connects are correct. It would add another layer to troubleshooting.

  20. #20
    LXi User Buzo's Avatar
    Join Date
    Dec 2010
    Vehicle
    88 Honda Accord EX - Single Port EFI, 4 doors Aut.
    Location
    North of Mexico
    Posts
    836

    Re: help with arduino visual c/c++ code writing

    You are going to need to separate your code in several stages and test each one at the time.

    For instance, I'm am working right now modifying a piece of equipment where I need to send several information through a serial cable to a remote LCD. There is one micro inside the cabinet and another 10 feet away in the LCD.

    1) I first assured I could program my controllers individually
    2) Then I worked in the controller of the LCD. I draw stuff into the LCD just to make sure I can access every single corner of the screen.
    3) Then the serial communication from the Equipment to my computer
    4) The serial communication from the LCD to my computer
    5) the serial communication between the two controllers.

    I don't have sensors connected yet, I simulate the presence of a sensor with software, sensor=1 or sensor=0 until I get the code to flow as I want, then I test the outputs of the sensors with the voltmeter to make sure they are sending the correct signal. and finally connect the sensors to the controllers.

    Write small routines like turning ON one output by one, then turn them off.
    Then add delays, like the standard off delay contacts you are familiar width.
    Make your controller to communicate with your computer if possible, so you "see" what the controller is doing. Send messages like "activating PORT1..." etc.

    Leave the random stuff for the end, one usually want to know how long a contact should be ON or OFF, and using random numbers they will just be ON and OFF like crazy.

    I think you are doing a good progress here. You will see how fun is to have stuff doing what you imagined exactly as you imagined it.


    Power is nothing without control

  21. #21
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    Re: help with arduino visual c/c++ code writing

    my coworker calls it a video game. You may not know if you are winning or losing till the end but it really is a great game to play.

    I understand what your saying I will do that when my stuff shows up. Until then, does it make sense in code speak? Does it look correct? I can only go off what I have read and watched on youtube but that doesn't necessarily make the original code writers good at writing code. I would agree that i am probably biting off more then I chew but if it looks that I am progressing in the right direction then I am happy.

    I really appreciate the help. does it seem that the syntacs from one section to the next are flowing correctly? The best I have to go from is the arduino ide gui. Its a very basic compiler. It will correct format if there are no errors and will help you to debug. That being said it doesnt write the code for you. I still have a ton to learn such as how to use a header file to be able to delete the bulk of the code.

    In the end I purchase the mega to be able to expand and control the halloween setting dynamically. I have 8 analog inputs and near limitless digitals. Id like to have this one device control everything. I dont think that will happen before next years show.

    I will try to individualize the sequences. does the random soleniod code look good? how do you add music reference to the code? Thats going to be my last step as its the one Ive spent the least time researching.

    Back to my video game.

  22. #22
    LXi User Buzo's Avatar
    Join Date
    Dec 2010
    Vehicle
    88 Honda Accord EX - Single Port EFI, 4 doors Aut.
    Location
    North of Mexico
    Posts
    836

    Re: help with arduino visual c/c++ code writing

    Your code sequences look correct to me!

    After searching over and over about C I found this site: http://publications.gbdirect.co.uk/c_book/ with all you need to know about programming. You don't need to read it all, but if you have syntax issues when compiling you can go here and find how to properly write a for-next, an if-else, a do-while.

    Tell me more about the rest of your hardware, Are you going to use 5V relays to activate your solenoids? How many pneumatic cylinders for each prop? Are all the lights for 5V or do you have some for 110V?

    I watched the videos you posted, they are very scaring by the way!


    Power is nothing without control

  23. #23
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    Re: help with arduino visual c/c++ code writing

    int ranNum;
    int ranDel;
    void setup() {
    // Seed RNG from analog port.
    randomSeed(analogRead(0));
    // Setup 4 output ports for Relays
    pinMode(21, OUTPUT);
    pinMode(22, OUTPUT);
    pinMode(23, OUTPUT);
    pinMode (24, OUTPUT);
    }
    void loop() {
    //Generate random number between 21 and 24
    ranNum=random(21,24);
    // Generate random delay time
    ranDel=random(25,300);
    //Turn on the Relay
    digitalWrite(ranNum, HIGH);
    delay(ranDel);
    //Turn off the Relay
    digitalWrite(ranNum, LOW);
    }

    It works, my first sketch and it works. I sure am glad arduino has a huge backbone of resources behind it. No way I could ever figure this out on my own.
    Credit for the build:
    http://www.meanpc.com/2012/01/random...o-project.html

  24. #24
    SEi User gp02a0083's Avatar
    Join Date
    Nov 2004
    Vehicle
    1989 Lx-i Hatchback
    Location
    Jackson, NJ
    Posts
    1,838

    Re: help with arduino visual c/c++ code writing

    its nice to see others here on the board with some micro-controller knowledge. This is stuff I'm learning at work from an assembly language and C code point of view

    1989 Accord Lx-i hatchback (current DD project)
    1986 Olds Cutlass 442 clone (never ending project)
    3Geez resident body man
    Owner of Wreck-less auto body

  25. #25
    2.0Si User mykwikcoupe's Avatar
    Join Date
    Aug 2002
    Location
    Washington
    Posts
    3,585

    Re: help with arduino visual c/c++ code writing

    i looked into assembly language. Its nice to know the LONG hand version of writing and how these programs originally got written and how easy is it now that you can include header files and pre scripted codes.

    I played with it a bit more afterwards. Ive got fading leds that will eventually turn into color changing LEDS. I need to include the ambient LEDS that turn on with the program but that shouldnt be too hard I hope. Its really nice to be able to see it work as well.

Similar Threads

  1. Can code 1 cause code 15?
    By Civic Accord Honda in forum EFI Tech
    Replies: 6
    Last Post: 07-03-2009, 10:46 PM
  2. Efi Code 8
    By Sherlock87LXI in forum EFI Tech
    Replies: 13
    Last Post: 11-17-2003, 09:04 PM
  3. code 17
    By Tref_LXI in forum 3geez Accords
    Replies: 8
    Last Post: 11-02-2003, 09:31 PM
  4. ECU code#6
    By smufguy in forum 3geez Accords
    Replies: 35
    Last Post: 11-09-2002, 08:46 PM
  5. Exhaust Diameter, Visual Comparison
    By A20A1 in forum Classic Honda Pics & Videos
    Replies: 11
    Last Post: 09-30-2002, 07:43 PM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
This website uses cookies
We use cookies to store session information to facilitate remembering your login information, to allow you to save website preferences, to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners.
     
Links monetized by VigLink