Show Posts
Pages: 1 [2] 3 4
16  Using Arduino / Programming Questions / Re: Serial Read with Switch/Case problem on: December 05, 2012, 03:40:59 pm
The android app sends the data over bluetooth to the serial port when you press a touchscreen button.
The app sends out two byte messages. I am trying to interpret the first byte (key) into different variables.
On the app you can set different buttons to output different key bytes. It also outputs a value for each key
as well in the second byte but I am not using that.

I added the break; to the last case but still no joy. How do I find out if the output is ASCII or binary? does the fact that
it works in the first set of code indicate anything?

17  Using Arduino / Programming Questions / Serial Read with Switch/Case problem on: December 05, 2012, 03:20:10 pm
Hi,
  I am using an UNO board with a Bluesmirf BT module connected to an android app.
I am trying to get the program to read the serial input from the app through the BlueSmirf
and run a different case depending on the input.

For some reason it does nothing when I press an input in the app and I can't work out why.

For a test I have an LED hooked up to pin 13.  I can get it to switch on and off using the app with
no problems with the first set of code.

however, I have tried to integrate that into the second set of code and I don't think I am doing it right
and possibly have the serial.read in the wrong place.

I figured if I can get case 4 (test LED)to work then I can get the rest of the code to work for my device.

can anyone see what I am doing wrong?

The android app says the following:

microController BT outputs standard 2 byte messages. The messages contain a key (0-255) and a value (0-255).

outgoing: (key, value)

(Working bit)
Code:
const int ledpin = 13;

void setup()
{
  pinMode(ledpin, OUTPUT);  // pin 13 (TestLED)
 
  Serial.begin(115200);       // start serial communication at 115200bps
 
}
 
void loop() {
    if( Serial.available() >= 2)       // if data is available to read
  {
    int key = Serial.read();
    int val = Serial.read();
    digitalWrite (key,val);
(Not Working Code)
Code:
const int ShakePin = 4;
const int FeedPin = 5;
const int StopPin = 6;
const int TestPin = 13;
byte mode;



void setup()
{
  pinMode(ShakePin, OUTPUT);  //Shaker Relay Pin
  pinMode(ShakePin, LOW);  //
  pinMode(FeedPin, OUTPUT);  // Feeder Relay Pin
  pinMode(FeedPin, LOW);  //
  pinMode(StopPin, INPUT);  // Cutout Switch
  pinMode(StopPin, HIGH);  //
 
  pinMode(TestPin, OUTPUT);  // pin 12 shakermotor as OUTPUT
  pinMode(TestPin, LOW);  // pin 12 shakermotor as OUTPUT
 
  Serial.begin(115200);       // start serial communication at 115200bps
 
}
 
void loop() {
  if( Serial.available() >= 2){
   
    int key = Serial.read();
    int val = Serial.read();
     
   
    if ( mode == 0 && key == 4){ mode = 1;}
    if ( mode == 0 && key == 5){ mode = 2;}
    if ( mode == 0 && key == 13){ mode = 4;}
   
    switch (mode){
   
case 0:  // do nothing and wait
   break;

case 1:  // Shake Relay
    digitalWrite (ShakePin, HIGH);
    delay (5000);
    digitalWrite (ShakePin, LOW);
    mode = 0;
   break;

case 2:  // Feed Relay
    digitalWrite (ShakePin, HIGH);
    delay (5000);
    digitalWrite (ShakePin, LOW);
    digitalWrite (FeedPin, HIGH);
    delay (3000);
    digitalWrite (FeedPin, HIGH);
    if (digitalRead(StopPin) == LOW) mode = 3;
break;
   
case 3:  //Stop Feed Relay and GoTo mode 0
    digitalWrite (FeedPin, LOW);
    mode = 0;
break;

case 4:   //LED Test
     digitalWrite (TestPin,HIGH);
     delay (3000);
     digitalWrite (TestPin,LOW);
     mode = 0;
     
  }
  }
}
18  Using Arduino / Programming Questions / Re: Android/Arduino bluetooth control problem on: December 02, 2012, 05:41:56 pm
Paul, thankyou.
     I understand it now, and thanks again for your patience.  The digitalWrite was the
bit that was throwing me but I see now that the key,val is defining the
pin number and setting the state.

I have fixed the other parts as well and it works perfectly

Cheers

Tim
19  Using Arduino / Programming Questions / Re: Android/Arduino bluetooth control problem on: December 02, 2012, 04:51:27 pm
Thankyou for your patience, as you can tell I am fairly new to this.

I think I understand what you mean about the bytes, so the app is sending two figures to the serial monitor via bluetooth,
one after another - 1 (button no) followed by a 0 (value). My problem is how I can interpret the different sets of data into
two variables, changing the pin state.  I know the is the bit that translates the bytes however I dont understand
what the digitalWrite (key,val); is doing.  Is this writing this figure to the arduino's memory?

void loop() {
if (Serial.available() >= 2) {
int key = Serial.read();
int val = Serial.read();

digitalWrite(key, val);

Sorry, but could you give me an example of how I can use the values to switch the pin state? it would
be very much appreciated.

My code as it currently stands is as follows. I have taken away the 'key value for now to try and simplify it.
  At the moment it is not working, however if I go to the terminal on the app and manually change the
val = 1 it does change the pin state and turn the LED on

Code:
/*
simple LED test
*/


void setup()
{
  pinMode(ledpin = 13, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
 
  Serial.begin(115200);       // start serial communication at 115200bps
 
}
 
void loop() {
    if( Serial.available() >= 2)       // if data is available to read
  {;}
    int key = Serial.read();
    int val = Serial.read();
    digitalWrite (key,val);
 
if( val == '0' )               

  {
    digitalWrite(ledpin, LOW);    // turn Off pin 13 off
    delay(1000);                  // waits for a second 
    Serial.println("13 off");
  }

if( val == '1' )             
  {
    digitalWrite(ledpin, HIGH);  // turn ON pin 13 on
    delay(1000);                  // waits for a second
    Serial.println("13 on");
  }
}



20  Using Arduino / Programming Questions / Re: Android/Arduino bluetooth control problem on: December 02, 2012, 03:29:40 pm
Thanks,
   The code seems to appear fine in my window, my apologies if it is hard to read.  I don't see any point in reposting as the thread will get long and confusing.

If you read the instructions for the app in the top set of code I believe it states that when a touchscreen button (Set up by me) is pressed it outputs a value of, for example 1,0.  One being the number assigned to the button and the zero being the state that the button is in. Button two would output a value of 2,0 when in the off position (if I am understanding this correctly)

If you press the touchscreen button 1 it outputs a value of 1,1 and so on.

I guess I a trying to get the code to recognise the difference between a 1,1 output (ON) and a 1,0 output (OFF) through the serial monitor.

I may have this completely wrong, I was hoping someone could read the instructions and give me their own understanding of how it should work and how I could implement it.

Thanks in advance
21  Using Arduino / Programming Questions / Android/Arduino bluetooth control problem on: December 02, 2012, 01:58:26 pm
Hi,
   I am using an Arduino UNO with a BlueSmirf Silver Bluetooth shield. 

I have succesfully connected to the BT module (Baud 115200) using an Android phone using various Apps
(Arduino Commander, Microcontroller BT). 

With an LED connected to Pin 13 I can succesfully turn it on and off using the Arduino Commander App and the
StandardFirmata sketch so I know I have no connection issues/problems with the boards etc.

I want to be able to turn pins on and off using the Microcontroller BT app but I am slightly confused by the instructions on
the app web page (shown in code below) I am trying to work out how to code for this.

My understanding is that when you set up a button in the app and press it, it sends a
number 1,0  (button 1, state 0)
           1,1  (button 1,state 1) through the BT module to the serial port on the Arduino.
I then need to somehow in the code monitor the serial input to look for certain states
and change the pin values accordingly.

Can someone explain how I need to set this up in the code. so far I cant seem to get it to work (see code at the end)

App instructions followed by my best guess in the second set of code:

Code:
Add a highly customizable programmable Bluetooth control panel to your electronics projects with microController BT. Drag, drop, resize and edit widgets to build the perfect controller for your microcontroller.

While designed with the Arduino microcontroller in mind, using low cost Bluetooth transceivers, microController BT should work with most projects that support serial communication and most Bluetooth transceivers that support SPP.

microController BT outputs standard 2 byte messages. The messages contain a key (0-255) and a value (0-255). For monitoring output, it listens for a standard 3 byte message. This message should contain a key (0-255), a highByte and a lowByte representing a value (0-1023).

outgoing: (key, value)
incoming: (key, highByte(value), lowByte(value))


For simple projects, you can directly plug the standard 2 byte messages into the digitalWrite/analogWrite functions, as shown below. For more advanced projects, you may choose to listen for particular keys and route accordingly.

void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT); //set for each pin
}

void loop() {
if (Serial.available() >= 2) {
int key = Serial.read();
int val = Serial.read();

digitalWrite(key, val);
}
}

To send values to microController BT for monitoring, simply write the standard 3 byte message to the serial port as shown below.

Serial.write(key);
Serial.write(highByte(val));
Serial.write(lowByte(val));


microController BT also outputs standard 2 byte messages when certain application events occur. These messages are useful for projects that change based on application state.

device connected: (0, 255)
start edit mode: (0, 253)
end edit mode: (0, 254)

My Code, adapted from an INSTRUCTABLE

Code:
/*
simple LED test
*/

char val;         // variable to receive data from the serial port (bluetooth module)
int ledpin = 2;  // LED connected to pin 2 (on-board LED)

void setup()
{
  pinMode(ledpin = 13, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
 
  Serial.begin(115200);       // start serial communication at 115200bps
 
}
 
void loop() {
  if( Serial.available() )       // if data is available to read
  {;}
    val = Serial.read();         // read it and store it in 'val'
 
  if( val == '1,0' )               // if 1,0' value recieved from bluetooth module

  {
   digitalWrite(ledpin, LOW);    // turn Off pin 13 off
delay(1000);                  // waits for a second 
Serial.println("13 off");
  }

if( val == '1,1' )               // if '1,1' value recieved from bluetooth
 {
    digitalWrite(ledpin = 13, HIGH);  // turn ON pin 13 on
    delay(1000);                  // waits for a second
    Serial.println("13 on");
  }
}

22  Using Arduino / Project Guidance / Re: UNO/motor shield power problem on: July 17, 2012, 03:42:05 pm
For the sake of the community, if anyone else has this problem, after days of frustration I have found a 'workaround'

A note for anyone trying to use a 'split power supply' with this setup. Even though my supply was split prior to plugging into the UNO/Motor Shield, there still seems to be feedback through to the adapter, and thus causing glitches on the UNO, even if the jumpers are set to have each board use an independent power supply.

 I am guessing (I'm sure someone else knows more) that because some of the pins are still shared through the board and shield for pwm, motor control etc that there is feedback somewhere, despite the supply being seperate.

To stop the interference coming through I had to carefully grind through the positive track on the board, right next to the servo sockets and completely isolate the servo power from the rest of the shield, then soldered header pins onto the +/- holes next to the servo pins and attached the supply to these pins instead. I then had the shield 'share' main power from the UNO with servos powered completely independently.

That completely solved the problem and it is running great now.
23  Using Arduino / Project Guidance / UNO/motor shield power problem on: July 17, 2012, 01:36:38 pm
Hi,
   I asked something similar yesterday with no luck, Thought I would try and simplify my explanation of what is wrong to hopefully find a solution.

I am using an Arduino UNO with an adafruit motor shield, two servos attached. Jumper pin removed from motor shield for individual power supplies. I have 6 inputs on pins a0 - a5, switches with 10k/100ohm pull up resistor configuration.

When I have an adapter (6v) plugged into the motor shield and the UNO powered by USB cable it runs perfectly, switches respond instantly and the code runs perfectly.

As soon as I try to power the UNO with a seperate power Supply (different adapter to the motor shield)  the program runs very glitchy. Switches a0- a4 only respond when held down for a couple of seconds. 

I have tried setting the adapter power from everything between 3.5v up to 12v with no change. UNO lights up all ok. If I plug the USB back in the switches work again, program runs fine etc.

I have also tried sharing the be adapter between the two boards - same result.


Any ideas what may be causing this?
24  Using Arduino / Project Guidance / UNO/adafruit Motor Shield Glitch on: July 16, 2012, 01:34:00 pm
Hi,
  I am using an UNO board with Adafruits Motor control shield. I have two servos attached to the two servo sockets on the shield and 6 switches with pull up resistors attached to analog pins A0 - A5.

I have a weird problem And cannot work out why it is happening.

When I have a seperate power supply for the UNO and the motor shield (no jumper) (both 6volts) for some reason switches on pins A0 and A1 are very glitchy and don't seem to work properly. I get a reaction from the servo's but they do not seem to trigger the right switch/case in my program.

To clarify, this only happens when using he seperate supplies. I have tried the following:

UNO, powered via USB, using the jumper on the motor shield to share the power - No Problems

UNO, powered by adapter (6v) with a seperate adapter powering motor shield- Glitchy a1 a0 response, other pins work fine

Adapter straight into motor shield (with jumper), sharing power with UNO- No Problems

Adapter straight into UNO (with jumper) sharing power - Glitchy a1 a0 response, other pins work fine.

I am stumped as to why this is happening. I thought that seperate power for the shield/UNO was preferable but it seems to cause glitches.


I have also noticed that I get instant response from all the switches when using shared USB power, however with every other method I get pins A3 - 5 giving good response with a glitchy a0/a1.


Anyone any idea as to why this may be happening?. Have not posted code as It works absolutely fine on USB power and Single shield power so I know the code is ok.
25  Using Arduino / Programming Questions / Re: Increasing two different servo PWM's with different delay's on: July 12, 2012, 12:42:22 pm
Thanks Paul, that is very helpful

Code:
long StepperAinterval = 60000; // 60 seconds break time between MINUTE movements         
long StepperBinterval = 360000; // 6 minute break time between HOUR movements
These should be:
Code:
long StepperAinterval = 60000UL; // 60 seconds break time between MINUTE movements         
long StepperBinterval = 360000UL; // 6 minute break time between HOUR movements

Just wondered what the UL is for? It seems to work fine without it.

One issue I do have however is that when Case 7 starts it is not always exactly 60 seconds before the first value is written to the motor, it seems to be random as to how long after pressing the switch to the first write, I am assuming it is something to do with the millis count not being zero when the case 7 starts to run.

After the first write it does stick strictly to the right timings for each motor so it is not too much of an issue and it is never seems to be more than a minute, Just a bit annoying.

Case 1-6 are all the same because the different 'start times' have not been entered yet. When I get the timings for the show these will all be set to different start points.

Thanks again for your help
26  Using Arduino / Programming Questions / Servo Clock Code on: July 12, 2012, 05:00:35 am
My full code for community purposes and anyone who is interested.  Be gentle! I know there are probably easier ways to do this.

Code:

#include <Servo.h>
#include <SoftwareSerial.h>

Servo MinuteHand;
Servo HourHand;

// SWITCH PINS

byte setupAswitch = A0;
byte setupBswitch = A1;
byte setupCswitch = A2;
byte setupDswitch = A3;
byte setupEswitch = A4;
byte setupFswitch = A5;
byte ResetSwitch = 2;




//  SERVO PINS

int MinuteHandPin = 9;     // Control pin for servo motor
int HourHandPin = 10;

//  STORAGE VALUES

int MinutePulse; // Storage value for counting time, MinuteHand
int HourPulse; // Storage value for counting time, HourHand

long previousMillisA = 0;        // Millis Counter Store
long previousMillisB = 0;        // Millis Counter Store

long StepperAinterval = 60000; // 60 seconds break time between MINUTE movements          
long StepperBinterval = 360000; // 6 minute break time between HOUR movements

byte mode; //

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void setup() {
  Serial.begin(9600);         // connect to the serial port
  Serial.println("**********    SETTING UP CLOCK CIRCUIT    **********"); //saying how it is
  
 //  SWITCH SETUP
 pinMode      (setupAswitch, INPUT); // set up the various push switches
 digitalWrite (setupAswitch, HIGH);  // turn on internl pullup
 pinMode      (setupBswitch, INPUT);
 digitalWrite (setupBswitch, HIGH);
 pinMode      (setupCswitch, INPUT);
 digitalWrite (setupCswitch, HIGH);
 pinMode      (setupDswitch, INPUT);
 digitalWrite (setupDswitch, HIGH);
 pinMode      (setupEswitch, INPUT);
 digitalWrite (setupEswitch, HIGH);
 pinMode      (setupFswitch, INPUT);
 digitalWrite (setupFswitch, HIGH);
 pinMode      (ResetSwitch, INPUT);
 digitalWrite (ResetSwitch, HIGH);
 
 // SERVO SETUP
 MinuteHand.attach(MinuteHandPin);  //Connects servo to relevent pin
 HourHand.attach(HourHandPin);  

 // SET BOTH SERVOS TO 'MIDNIGHT'
 
 MinuteHand.writeMicroseconds(1600);  /// SET to 12.00
 HourHand.writeMicroseconds(1600);  /// SET to 12.00

 // START MILLIS TIMER
 
 //unsigned long CurrentMillisA = millis();  //Storage for Timer NOT SURE IF RIGHT ONE!!!
 
Serial.println("WAITING FOR INPUT");
}


void loop() {
  
unsigned long CurrentMillisA = millis();  //Storage for Timer
  
if ( mode == 0 && digitalRead (setupAswitch) == LOW){ mode = 1;}
if ( mode == 0 && digitalRead (setupBswitch) == LOW){ mode = 2;}
if ( mode == 0 && digitalRead (setupCswitch) == LOW){ mode = 3;}
if ( mode == 0 && digitalRead (setupDswitch) == LOW){ mode = 4;}
if ( mode == 0 && digitalRead (setupEswitch) == LOW){ mode = 5;}
if ( mode == 0 && digitalRead (setupFswitch) == LOW){ mode = 6;}

if ( mode == 0 && digitalRead (ResetSwitch) == LOW){ mode = 9;} //reset all
if ( mode == 1 && digitalRead (ResetSwitch) == LOW){ mode = 9;} //reset all
if ( mode == 2 && digitalRead (ResetSwitch) == LOW){ mode = 9;} //reset all
if ( mode == 3 && digitalRead (ResetSwitch) == LOW){ mode = 9;} //reset all
if ( mode == 4 && digitalRead (ResetSwitch) == LOW){ mode = 9;} //reset all
if ( mode == 5 && digitalRead (ResetSwitch) == LOW){ mode = 9;} //reset all
if ( mode == 6 && digitalRead (ResetSwitch) == LOW){ mode = 9;} //reset all
if ( mode == 7 && digitalRead (ResetSwitch) == LOW){ mode = 9;} //reset all

////////////////////////////////////////////////////////////////////////////////////////
////PROGRAM VARIABLES/SERVO SETUPS

switch (mode){

case 0:  // Do Nothing and Wait for input

break;

case 1:  // Set Clock start position A
 MinutePulse = 1600;  // sets the stored Minute value to match the time
 HourPulse = 1600;   //sets the stored Hour value to match the time set
 MinuteHand.writeMicroseconds(MinutePulse); // sets Time A min
 HourHand.writeMicroseconds(HourPulse);  // sets Time A hour
 Serial.print("HOUR PWM VALUE IS SET TO ");
 Serial.println(HourPulse);
 Serial.print("MINUTE PWM VALUE IS SET TO ");
 Serial.println(MinutePulse);
 delay (1000);
 Serial.println("TICKING 1");
 previousMillisA = 0; //Reset Millis Counter A
 previousMillisB = 0; //Reset Millis Counter B
 mode= 7;
break;


case 2:  // Set Clock start position B
 MinutePulse = 1600;  // sets the stored Minute value to match the time
 HourPulse = 1600;   //sets the stored Hour value to match the time set
 MinuteHand.writeMicroseconds(MinutePulse); // sets Time A min
 HourHand.writeMicroseconds(HourPulse);  // sets Time A hour
 Serial.print("HOUR PWM VALUE IS SET TO ");
 Serial.println(HourPulse);
 Serial.print("MINUTE PWM VALUE IS SET TO ");
 Serial.println(MinutePulse);
 delay (1000);
 Serial.println("TICKING 2");
 previousMillisA = 0; //Reset Millis Counter A
 previousMillisB = 0; //Reset Millis Counter B
 mode= 7;
break;


case 3:  // Set Clock start position C
 MinutePulse = 1600;  // sets the stored Minute value to match the time
 HourPulse = 1600;   //sets the stored Hour value to match the time set
 MinuteHand.writeMicroseconds(MinutePulse); // sets Time A min
 HourHand.writeMicroseconds(HourPulse);  // sets Time A hour
 Serial.print("HOUR PWM VALUE IS SET TO ");
 Serial.println(HourPulse);
 Serial.print("MINUTE PWM VALUE IS SET TO ");
 Serial.println(MinutePulse);
 delay (1000);
 Serial.println("TICKING 3");
 previousMillisA = 0; //Reset Millis Counter A
 previousMillisB = 0; //Reset Millis Counter B
 mode= 7;
break;


case 4:  // Set Clock start position D
 MinutePulse = 1600;  // sets the stored Minute value to match the time
 HourPulse = 1600;   //sets the stored Hour value to match the time set
 MinuteHand.writeMicroseconds(MinutePulse); // sets Time A min
 HourHand.writeMicroseconds(HourPulse);  // sets Time A hour
 Serial.print("HOUR PWM VALUE IS SET TO ");
 Serial.println(HourPulse);
 Serial.print("MINUTE PWM VALUE IS SET TO ");
 Serial.println(MinutePulse);
 delay (1000);
 Serial.println("TICKING 4");
 previousMillisA = 0; //Reset Millis Counter A
 previousMillisB = 0; //Reset Millis Counter B
 mode= 7;
break;

case 5:  // Set Clock start position E
 MinutePulse = 1600;  // sets the stored Minute value to match the time
 HourPulse = 1600;   //sets the stored Hour value to match the time set
 MinuteHand.writeMicroseconds(MinutePulse); // sets Time A min
 HourHand.writeMicroseconds(HourPulse);  // sets Time A hour
 Serial.print("HOUR PWM VALUE IS SET TO ");
 Serial.println(HourPulse);
 Serial.print("MINUTE PWM VALUE IS SET TO ");
 Serial.println(MinutePulse);
 delay (1000);
 Serial.println("TICKING 5");
 previousMillisA = 0; //Reset Millis Counter A
 previousMillisB = 0; //Reset Millis Counter B
 mode= 7;
break;


case 6:  // Set Clock start position F
 MinutePulse = 1600;  // sets the stored Minute value to match the time
 HourPulse = 1600;   //sets the stored Hour value to match the time set
 MinuteHand.writeMicroseconds(MinutePulse); // sets Time A min
 HourHand.writeMicroseconds(HourPulse);  // sets Time A hour
 Serial.print("HOUR PWM VALUE IS SET TO ");
 Serial.println(HourPulse);
 Serial.print("MINUTE PWM VALUE IS SET TO ");
 Serial.println(MinutePulse);
 delay (1000);
 Serial.println("TICKING 6");
 previousMillisA = 0; //Reset Millis Counter A
 previousMillisB = 0; //Reset Millis Counter B
 mode= 7;
break;


case 7: // Commence Time
 
 if (MinutePulse >= 2495) mode = 10; //servo safety to stop it over-turning
 if (HourPulse >= 2495) mode = 10;
 // Serial.println(CurrentMillisA);
 // Serial.println(CurrentMillisB);
  if(CurrentMillisA - previousMillisA > StepperAinterval) {
    previousMillisA = CurrentMillisA;
    MinutePulse = MinutePulse + 4;
    Serial.print("MINUTE PWM VALUE IS SET TO ");
    Serial.println(MinutePulse);
    MinuteHand.writeMicroseconds(MinutePulse);
  }
  if(CurrentMillisA - previousMillisB > StepperBinterval) {
    previousMillisB = CurrentMillisA;
    HourPulse = HourPulse + 2;
    Serial.print("HOUR PWM VALUE IS SET TO ");
    Serial.println(HourPulse);
    HourHand.writeMicroseconds(HourPulse);
  }

    

case 9:  //  RESET THE PROGRAM
Serial.println("RESETTING & JOGGING");
 MinuteHand.writeMicroseconds(1700); // Jogs the motor to ensure a 'midnight'
 HourHand.writeMicroseconds(1700);
 delay (1000);
 MinuteHand.writeMicroseconds(1600); // Jogs the motor
 HourHand.writeMicroseconds(1600);
MinutePulse = 1600;  // resets values
HourPulse = 1600;
Serial.print("HOUR PWM VALUE IS RESET TO MIDNIGHT");
Serial.println(HourPulse);
Serial.print("MINUTE PWM VALUE IS RESET TO MIDNIGHT");
Serial.println(MinutePulse);
delay (1000);
Serial.println("WAITING FOR INPUT");
mode = 0;
break;

case 10:  //  SERVO OVERREACH
Serial.println("******   BWEEP SERVO OVERREACH   *******");
Serial.print("SERVO OR PWM VALUE IS ");
Serial.println(HourPulse);
Serial.print("SERVO OR PWM VALUE IS ");
Serial.println(MinutePulse);
delay (3000);
Serial.println("WAITING FOR INPUT");
MinutePulse = 1600;  // resets values to midnight
HourPulse = 1600;
mode = 0;
break;


} // end switch

} // end loop
 
 
27  Using Arduino / Programming Questions / Re: Increasing two different servo PWM's with different delay's on: July 12, 2012, 04:42:21 am
Quote
Within one continous loop

every 60 seconds - increase servoA pwm by 4
every 120 seconds - increase servoB pwm by 3
I'm really curious why you want the motor to run faster and faster, instead of a constant speed. Every clock I own has a motor that runs at a constant speed. I can't help but think I am missing something.

Hi, Increasing the PWM is not increasing the speed, it is increasing the position of the motor around the 360 degrees, for example: on the minute hand
1600pwm is midnight 1604 is one minute past 1608- two minutes past and so on.  So adding 4 to the microseconds every minute is moving the servo by one minute on the clock, every minute.

The need for a different speed on the hour hand is due to it having to only cover 1/12th of the 360 degrees in an hour.  My maths above for the hour hand were wrong however.  It is actually 3 increments every 6 minutes, not every two.

The clock is for a theatre show where there are 6 scene changes in the show. At each scene change the stage crew press a button and the time zips to the next scene time then starts ticking away until the next button is pressed, moving it on to the next scene.

I will post the full code on a seperate post for anyone who is interested. It is not perfect and probably a little messy (I still class myself as a complete newbie to all this)  It works very well at the moment and keeps proper time to within a minute which is fine for my purposes.

28  Using Arduino / Programming Questions / Re: Increasing two different servo PWM's with different delay's on: July 11, 2012, 04:27:03 pm
Just wanted to thank you all for your patience and help.  I managed to get it working with the 'millis' method
as suggested. After staring at it for an hour it finally clicked into place

My code for that particular section is below for anyone who is interested (majority omitted).

Thanks Again!

Code:

(Setup declarations)

unsigned long currentMillisA = millis();  //Storage for Timer
unsigned long currentMillisB = millis();  //Storage for Timer

long previousMillisA = 0;        // Millis Counter Store
long previousMillisB = 0;        // Millis Counter Store

long StepperAinterval = 60000; // 60 seconds break time         
long StepperBinterval = 120000; // 120 seconds break time

(inside void loop())

case 7: // Commence Time
 ////////////
 
 if (MinutePulse >= 2500) mode = 10; //servo safety to stop it over-turning
 if (HourPulse >= 2500) mode = 10;
 ////////////
 
  if(CurrentMillisA - previousMillisA > StepperAinterval) {
    MinutePulse = MinutePulse + 4;
    previousMillisA = CurrentMillisA;
    MinuteHand.writeMicroseconds(MinutePulse);
    Serial.print("MINUTE PWM VALUE IS SET TO ");
    Serial.println(MinutePulse);
  }
  if(CurrentMillisB - previousMillisB > StepperBinterval) {
    HourPulse = HourPulse + 3;
    previousMillisB = CurrentMillisB;
    Serial.print("HOUR PWM VALUE IS SET TO ");
    Serial.println(HourPulse);
    HourHand.writeMicroseconds(HourPulse);
  }

   
break;
29  Using Arduino / Programming Questions / Re: Increasing two different servo PWM's with different delay's on: July 11, 2012, 02:41:01 pm

Quote
By now, you should know that you can't do this. You should not have ANY delays(). Look at the blink without delay example to see how to write delay()less code.

I do know this, hence the reason for asking for an alternative method. I dont really see how I could use 'millis' to help here, can you suggest how?.

My understanding is that Millis counts the time since the program started, however my program waits for inputs and runs other switch/modes before running my 'clocktime loop' 

The absolute simplest explanation I can think of what I trying to achieve' is:

Within one continous loop

every 60 seconds - increase servoA pwm by 4
every 120 seconds - increase servoB pwm by 3

looped forever, (or until reset)

I am trying to understand the code for timing/updating the pwm on the two servos within the same executed loop.
30  Using Arduino / Programming Questions / Re: Increasing two different servo PWM's with different delay's on: July 11, 2012, 01:35:44 pm
Quote
Here is the 'wrong code version' of what I am trying to do

Case 0:
I'm sorry, but a case statement without the enclosing switch statement is useless. I can't figure out under what conditions you expect this code to be executed.

Sorry I cut out most of the stuff that was not relevant to what I am trying to get across. I was trying to explain the fact that I am trying to have two different delays running simultaneously within the same loop. I have many switch/ case statements working perfectly within my code at the moment, I should have left the 'case' out to avoid confusion.
Pages: 1 [2] 3 4