Show Posts
Pages: 1 ... 57 58 [59] 60 61 ... 196
871  Using Arduino / Programming Questions / Re: GearTooth Sensor on: October 24, 2013, 08:51:54 pm
Ok easy, get a reading from the sensor and compare it to an "old" reading. If the two readings are different, then you know it is oscillating.


void loop()  {
currentValue = get_from_sensor();

unsigned long timer = millis();
  if(currentValue != oldValue && millis() - timer < 1000) // 1000 = 1 second
    oldValue = currentValue; // they were different and values changed within 1 second, so update old value for next reading
    Serial.println("Oscillating!  "); // show a message that it was oscillating
    timer = millis();                        // Reset timer

You could also probably use the pulseIn() function. Set the pin you want to read from, set the state you want to look for HIGH or LOW, and you can also set a timeout value  from 10 microseconds to a maximum of 3 minutes.
872  Using Arduino / Project Guidance / Re: Hey, need some help on where to start programming! (Balancing Bot) on: October 23, 2013, 07:23:09 pm
Yes, L_EN means "Left motor" enable and likewise for R_EN.

After looking at the dual motor library, there is a function called "setSpeeds(int m1Speed, int m2Speed)", so instead of calling "md.setM1Speed(Left_Motor); md.setM2Speed(Right_Motor);" you can just have "setSpeeds(Left_Motor,Right_Motor);". The enables still need to be kept the same.

Actually Im not sure if you even need to control the enables, because the library has functions to control the speed of the motors and also functions to control the breaks. It might still work the way your doing it, but I am not certain. Try it and see it if works, if not the you need to rethink your coding.
873  Using Arduino / Project Guidance / Re: Hey, need some help on where to start programming! (Balancing Bot) on: October 23, 2013, 03:36:26 pm
Great that your parts came in.

You don't need to keep declaring pinMode for every run, Just set them in the setup() function.
Also what you could do is make a function that takes in 2 or 4 variables, to control the motors.
Like so,
#define Enable 1     //Global
#define Disable 0  //Global

void loop()
  Drive(400, -400, Enable, Enable); // forward
  Drive(-400, 400, Enable, Enable); // reverse

void Drive (int L_motor, int R_motor, byte L_EN, byte R_EN)
   digitalWrite(M1EN, L_EN);   
   digitalWrite(M2EN, R_EN);

The 2 variable function would just be speed and a direction.
Drive(400, FORWARD);
Drive(400, LEFT);

Set the speed, and give a direction, then use a case statement to manipulate the speed that go to the motors.
874  Using Arduino / Programming Questions / Re: password checker 'Brute Force' on: October 23, 2013, 02:01:51 am
Do you mean something like this?

Slight edit.
char input[30];
char output[30];
unsigned long startTime = 0, endTime = 0;
uint8_t i = 0, j = 0, Clear = 0;
void setup(){
  Serial.println("Enter password followed by a period ' . ' ");

void loop() {
  if(Serial.available() > 0)
    input[i] =;
    if(input[i] != '.') i++;
      for(int j = 0; j < i; j++)
        startTime = micros();
        for(char k = 48; k < 123; k++)
          if(input[j] == k)
            output[j] = k;
            endTime += micros() - startTime;
      Serial.print("You entered: ");

      Serial.print("Code breaker found: ");

      Serial.print("Microseconds: ");

      i = 0, startTime = 0, endTime = 0, Clear = 0;
      while(Clear != 29)
        input[Clear] = '\0';
        output[Clear] = '\0';
875  Using Arduino / Programming Questions / Re: VirtualWire Explained on: October 22, 2013, 11:53:09 pm
Ok then.

"uint8_t *  " means "unsigned 8 bit int ",   * means it's a pointer. In this case it is type casting a char array to an unsigned 8 bit int pointer.

A simple transmitter code, that transmits data from an accelerometer to a receiving Arduino.
#include <VirtualWire.h>

char Array[20]; // create an array large enough to hold the string of data
int X,Y,Z; // Accelerometer variables

void setup()
  Serial.begin(9600);   // Debugging only
  Serial.println("Sending"); // Debugging only

  // Initialise the IO and ISR
  //vw_set_ptt_inverted(true); // Required for DR3100   ** NOT NEEDED UNLESS USING DR3100 MODULE **
  vw_setup(2000); // Bits per sec   **Baud Rate** Max is 4000, but you may lose data
  vw_set_tx_pin(33); //RF sending pin
  pinMode(37, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(53, OUTPUT);
  digitalWrite(37, LOW); // RF ground
  digitalWrite(35,HIGH); // RF Vcc
  digitalWrite(53,HIGH); // accelormeter Vcc

  pinMode(13, INPUT); //accel Z
  pinMode(14, INPUT); //accel Y
  pinMode(15, INPUT); //accel X

void loop()
  X = analogRead(15) / 4; // X raw data to 0 - 1023 ** Divided by 4 to give range of 0 - 255**
  Y = analogRead(14) / 4; // Y raw data to 0 - 1023 ------------------------------------------
  Z = analogRead(13) / 4; // Z raw data to 0 - 1023 ------------------------------------------
  sprintf(Array, "%d,%d,%d ",X,Y,Z);  // sprintf("storage location array" , "How the data is grouped and stringed together", "the data variables to be stringed and sent")
//Example data: "205,10,46 "

  vw_send((uint8_t*)Array, strlen(Array));
  // strlen counts the number of characters in the array,
  //and sets how many times "vw_send" needs to go through the array to send everything in it.
  vw_wait_tx(); // wait until all data is sent

Now the receiving end.
#include <VirtualWire.h>

void setup()
  Serial.begin(9600); // Debugging only
  pinMode(13,OUTPUT); // Arduino onboard LED, pin 13
  // Initialise the IO and ISR
  //vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000);      // Bits per sec **Must match transmitter baud rate**
  vw_set_rx_pin(8);    // data pin, pin 8 on arduino
  vw_rx_start();       // Start the receiver PLL running

void loop()
  uint8_t buf[VW_MAX_MESSAGE_LEN];  // The max message length is 30 chars, but after the NULL terminator and two other bytes ( not sure which) the actual max length is 27 char.
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen)) // Non-blocking
    //Data received: "205,10,46 "
    digitalWrite(13, HIGH); // Indicate data is being received
    int temp1 = atoi(strtok(buf, ",")); // reads the buf array until it finds a comma "," then stores the data before the comma in temp1
    // temp1 = 205   ** Notice no comma, that because strtok reads everything before the comma
    int temp2 = atoi(trtok(NULL,",")); // NULL tells strtok to pick up where it found the last condition IE the comma.
    //temp2 = 10
    int temp3 = atoi(strtok(NULL," "));// Now strtok is looking for a blank space indicated by " ", and like the previous two, it will store the last bit of data in temp3
    // temp3 = 46
    Serial.print(temp1); // X axis
    Serial.print(", ");
    Serial.print(temp2); // Y axis
    Serial.print(", ");
    Serial.print(temp3); // Z axis
  else digitalWrite(13, LOW); // No data received

Darn, Crossroads beat me to it.
876  Using Arduino / Programming Questions / Re: Where is mistake? on: October 22, 2013, 07:58:22 pm
Well what is the code doing?

You should maybe clean up the code just a little bit more. Maybe instead of using IF statements, try using case statements.

const byte Button = 2;
byte ButtonState;
byte last = LOW;
int count = 0;

void setup() {
  pinMode(Button, INPUT);

void loop() {
  ButtonState = digitalRead(Button);
  if(ButtonState && ButtonState != last) {
  if(count >=3) {
    count = 0;
 switch( count ) {
   case 0:

   case 1:
      Serial.println("How are you");
   case 2:
      Serial.println("Good day");

    // Add a default option here if needed

  last = ButtonState;

877  Using Arduino / Project Guidance / Re: Turn off arduino by software on: October 22, 2013, 07:19:14 pm
Well you cant fully turn off the Arduino, not unless you use external methods, but you can make it sleep. Check out this link HERE
878  Using Arduino / Project Guidance / Re: SeniorDesignProject Arduino Torsional sensor Demonstration Unit Data acquisition on: October 22, 2013, 04:55:02 pm
Every Arduino, except maybe the Lilipad, has at least 20 output pins. The Leonardo will do fine, but you do have other options, if needed.

Now what do you consider to be VERY FAST? The Arduino can send data Serially to a number of terminals at whatever baud rate you set it to. I'm not sure what the absolute highest baud rate is but the current highest is 115200.

What sensors are you planning on using?
879  Using Arduino / Project Guidance / Re: SeniorDesignProject Arduino Torsional sensor Demonstration Unit Data acquisition on: October 22, 2013, 04:39:10 pm
Well the Leonardo w/ headers in the Arduino store is $24, but you can find one cheaper online at say eBay or DigiKey. Do is need to be a Leonardo? You can do the practically the same things with a Nano, Mini, Micro and Uno. All can be found online for cheaper prices. Make sure you pick the right ones, most if not all say Arduino compatible, but you can still run into problems.

The Arduino Playground has many libraries for various sensors, I'm sure you'll find what you need there.
880  Using Arduino / Programming Questions / Re: Servo Speed Control on: October 21, 2013, 09:55:01 pm
digitalRead(firstswitch, secondswitch);
Not quite.

This is the right way.

if (digitalRead(firstswitch) == HIGH) {
    delay (20);

Now do the same of SecondSwitch.
881  Using Arduino / Programming Questions / Re: Servo Speed Control on: October 21, 2013, 08:54:33 pm
A servo can only be slowed down, and that would be by using a delay (preferably using the blink without delay method).

Now looking at your code, I see that you want to change the delay based on which of the two switches are set HIGH. That's fine to have, but your not reading your switches. Technically your comparing the pin number to HIGH, but not the pin's actual state.
You want to use the digitalRead( /* Pin Number */) function, to get the state of the pin, then compare that state to either HIGH or LOW.
882  Using Arduino / Project Guidance / Re: Trafic Light Help on: October 20, 2013, 12:53:34 pm
The most obvious issue I can see, is your using a LOT of delays, which are blocking your code from doing anything else until they are done. What you need to look at is the Blink Without Delay sketch, it's an example provided to you under the Arduino's Examples.

When you get that to work, you can cut the code down with an array and a for loop.
883  Using Arduino / Programming Questions / Re: How do I convert a char to String? on: October 19, 2013, 07:09:28 pm
What kind of (s/S)tring?

char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
char Str4[ ] = "arduino";
char Str5[8] = "arduino";
char Str6[15] = "arduino";


String object

Also, is there any way to stop an Arduino until it receives input from a keypad?
Stop the Arduino how, an example would be helpful, but first, we need to see your code.
884  Using Arduino / Programming Questions / Re: Fastest way to read a de-bounced button? on: October 18, 2013, 05:44:32 pm
Why not just debounce the button with hardware(capacitor and resistor) instead of software? But if you want to do it with software, option A is the better way.
885  Development / Other Software Development / Re: New Password Library on: October 18, 2013, 05:41:22 pm
Any thoughts as to why this is happening?
There is always a snag.

I figured it out... kinda, I went back to my original version and just modified it but kept the char * arrays instead of using a structure. I still want to understand what the problem was, I'm still thinking it was memory leakage issues.
Pages: 1 ... 57 58 [59] 60 61 ... 196