Connect with us

arduino 3 parts

Discussion in 'Microcontrollers, Programming and IoT' started by donkey, Aug 10, 2012.

  1. donkey

    donkey

    1,286
    56
    Feb 26, 2011
    ok so I am using arduino cos I can and its pretty easy so far.
    but I have an issue combining the 3 parts of my project together.
    first there is a wii nunchuck:- there is an issue with the library for this device but I will work with what I have. I have followed the examples given from most search engines, and it gave me some mixed results but after some debugging I got it to work, however the values do not go from 0 they start around 64 which might not be a big problem

    the second part is the send recieve via the nrf24l01. an example code was given here http://maniacbug.github.com/RF24/led_remote_8pde-example.html

    the 3rd part is the motor and hbridge to control them.

    the issue I have is combining these codes together to get something that uses the wii nunchuck as the controller to transmit over the 2.4ghz tranciever then recieved on the second arduino and converted into something that can control 2 motors (1 would be left other right so both on is forward, both off still reverse is both back and then l/r is only one turning forward)

    I can figure out each of the codes, but for the life of me I can't figure out how to combine them.
     
  2. donkey

    donkey

    1,286
    56
    Feb 26, 2011
  3. donkey

    donkey

    1,286
    56
    Feb 26, 2011
    am sorry to keep posting here but trying to get this to work my coding is worse than my electronics knowledge.
    I have declared the pins, i have declared the pots. I am not trying to figure out how to do 2 things, first read the pot (0-1023 range) and divide that into 3 areas. 0-510 is reverse (0 being fast reverse) 510-513 is stop and 513-1023 is forward (1023 is full forward)
    second part is to get the code to translate those signals into a pwm for both direction and speed.
     
  4. dahhal

    dahhal

    58
    0
    Jul 17, 2012
    I'm not familiar with Arduino, and my software is better then my hardware. Note: I'm familiar only with working with micro's without Operating Systems and my answer reflects this.

    Inputs
    a. If I got this right the pot is analogue though it has 1024 discrete values that will be shown in the ADC register (assuming the input voltage and configuration is correct)
    b. are you working in C? You can create a #define that maps to the bit of memory of the ADC and then you can read the 10 bits of hte ADC. you may need to shift the bits if the value is the higher 10 bits copy this into a unsigned int.
    c.
    if(pot< 510)
    {
    state = reverse
    speed = 512 - pot; // as this reaches the center the speed needs to decrease
    }
    else if(pot >= 510 && pot <= 513)
    {
    state = stop;
    speed = 0
    }
    else
    {
    state = forward
    speed = pot - 512;
    }

    output
    a. micros have timer modules fit for this purpose. the freescale s12 that i'm familiar with has timer that has outputs corresponding to it for PWM when configured.

    assuming we have 512 discrete values for speed as the above algorithm implies.

    as speed approaches 512 you want the duty cycle to be close to 100% or as much as the motor can take.
    as the duty cycle approaches 0 you want the duty cycle to be 0%.


    Hope this helps.
     
  5. donkey

    donkey

    1,286
    56
    Feb 26, 2011
    dahhal thanks for the info, when I say my programming is worse than my electronics I should point out my electronics is close to 0.
    I have a keen mind to learn and that helps but alot of googling code to tell me what it means happens. I am using win7 and arduino's compiler but I really thought this would have been done before but I just cannot find the code to learn from
    also arduino is a version of C with very few exceptions
     
  6. dahhal

    dahhal

    58
    0
    Jul 17, 2012
    I've done some arduino googling and found this : http://arduino.cc/playground/CourseWare/AnalogInput

    It seems the hard stuff is done for you by the Operating System. I've taken the tutorials and submitted what i'd attempt my first go, though not owning an Arduino and not touching c for a while the rest is up to you. Note my first attempt is sometimes very very wrong :)

    int sensorPin = A0; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int sensorValue = 0; // variable to store the value coming

    void setup() {

    // declare the ledPin as an OUTPUT:
    pinMode(ledPin, OUTPUT);

    }

    void loop() {

    // read the value from the sensor:
    sensorValue = analogRead(sensorPin);
    if(sensorValue < 510)
    {
    state = reverse // where reverse is an enum or #define. this is the wrong sytax
    speed = 512 - sensorValue ; // as this reaches the center the speed needs to decrease
    }
    else if(sensorValue >= 510 && sensorValue <= 513)
    {
    state = stop;
    speed = 0
    }
    else
    {
    state = forward
    speed = sensorValue - 512;
    }

    //output refer to http://arduino.cc/it/Tutorial/PWM

    if(speed>> 512)
    {
    //if error in the above logic force it to be no more then 512
    speed= 512
    }

    // ADC is only 8 bits, lets shift all bits right once to effectively divide it by 2 the quick way
    speed= speed> 1

    analogWrite(speed)

    }

    Note: there is no code for reverse. is that just a different pin to output the PWM signal.
     
    Last edited: Aug 11, 2012
  7. donkey

    donkey

    1,286
    56
    Feb 26, 2011
    as I am using 2 motors and 2 pots would it be better to label them sensorvalue1 and sensorvalue2 and same for the motors?
    also arduino has this annoying thing, the pot values is 0-1023 but pwm is 0 -255. how can I accomodate that?
     
  8. dahhal

    dahhal

    58
    0
    Jul 17, 2012
    label them what makes sense in the real world. I doubt 1 or 2 make any sense. ie if motor1 is on the left and motor 2 is on the right motorLeft and motorRight would be a better name. if motor 1 is forward, motorForward is a better name.

    as with the 0-1023 discrete values indicating speed and direction. Ie you have 510 discrete values for forward, 510 for reverse and 4 values for stop.

    my algorithm accounted for this knowing that 510 / 2 is 255 ie max speed = 510. 510 divided by 2 or shifted right once speed >> 1, when speed is max 510 >> 1 should be 255 (i may have only used one > in my example yesterday which is wrong see http://en.wikipedia.org/wiki/Bitwise_operation. I also should have used speed and not sensor value )
     
    Last edited: Aug 11, 2012
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-