Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Writing a library? (Beginner) on: March 31, 2013, 07:31:09 pm
OK Its an alarm clock...

Code:
#include <Time.h>
#include "pitches.h"

int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };

int latch = 5;
int data = 6;
int clock = 7;

int digit1=8;
int digit2=9;
int digit3=10;
int digit4=12;

int num1;
int num2;
int num3;
int num4;

int number[]={
  32, 55, 12, 6, 19, 66, 64, 39, 0, 2};

int m;
int h;
int s=0;

int lastbutton1=1;
int lastbutton2=1;
int lastbutton3=1;
int lastbutton4=1;


int d=0;

int t=100;

int a1=7;
int a2=0;

int d1;
int m1;

int snooze=0;

int lastsnooze;
void setup(){
  Serial.begin(9600);
  pinMode(0,INPUT_PULLUP);
  pinMode(1,INPUT_PULLUP); 
  pinMode(2,INPUT_PULLUP);
  pinMode(3,INPUT_PULLUP);
  pinMode(4,INPUT_PULLUP);
 
  pinMode(latch,OUTPUT);
  pinMode(data,OUTPUT);
  pinMode(clock,OUTPUT);
 
  pinMode(digit1,OUTPUT);//
  pinMode(digit2,OUTPUT);
  pinMode(digit3,OUTPUT);
  pinMode(digit4,OUTPUT);

}
 
  void loop(){
 
  int switch1=digitalRead(2);
  int button1=digitalRead(3);
  int button2=digitalRead(4); 
  int button3=digitalRead(0);
  int button4=digitalRead(1);
  s=0;



 
if (switch1==1&&button4==0){
goto y;} 

if (switch1==0){
  if (button1 != lastbutton1){
    if (button1 == 0){
    h--;
 if (h<1){
   h=12;}
}
  }    lastbutton1=button1;
 
  if (button2 != lastbutton2){
     if (button2 == 0){
       m--;
  if (m<1){
  m=59;}
}
  }  lastbutton2=button2;

  if (button3 != lastbutton3){
    if (button3 == 0){
    h++;
 if (h>12){
   h=1;}
}
  }    lastbutton3=button3;
 
  if (button4 != lastbutton4){
     if (button4 == 0){
       m++;
  if (m>59){
  m=0;}
}
  }  lastbutton4=button4;
 
   setTime(h,m,s,m1,d1,13);
}

if(switch1==1&&button1==1&&d==4){
  h=hour();
  m=minute();
  s=second();
 
 setTime(h,m,s+.016,m1,d1,13);
}

if (switch1==1&&button1==0&&button2==1){
 b :
  num1=second()/10;
  num2=second()%10;
 
  digitalWrite(latch,0);
  shiftOut(data,clock,LSBFIRST,number[num1]);
  digitalWrite(latch,1);
 
  digitalWrite(digit1,0);
  digitalWrite(digit2,0);
  digitalWrite(digit3,1);
  digitalWrite(digit4,0);
 
  delay(4);
  d++;

  digitalWrite(latch,0);
  shiftOut(data,clock,LSBFIRST,number[num2]);
  digitalWrite(latch,1);
 
  digitalWrite(digit1,0);
  digitalWrite(digit2,0);
  digitalWrite(digit3,0);
  digitalWrite(digit4,1);

  delay(4);
  d++;


button1=digitalRead(3);

if(button1==0&&button2==1){
goto b;}else{
goto a;
}
}
a :

h=hour();

if(h>12){
  h=1;}
    num1=h/10;
    num2=h%10;
    num3=minute()/10;
    num4=minute()%10;
   
  if (switch1==1&&button2==0){
    goto z;
  }else
 
if (num1>0){

  digitalWrite(latch,0);//**************************************From here....
  shiftOut(data, clock, LSBFIRST, number[num1]);
  digitalWrite(latch,1);
 
  digitalWrite(digit1,1);
  digitalWrite(digit2,0);
  digitalWrite(digit3,0);
  digitalWrite(digit4,0);

delay(4);
d++;}

 
  digitalWrite(latch,0);
  shiftOut(data, clock, LSBFIRST, number[num2]);
  digitalWrite(latch,1);
 
  digitalWrite(digit1,0);
  digitalWrite(digit2,1);
  digitalWrite(digit3,0);
  digitalWrite(digit4,0);

delay(4);
d++;
  digitalWrite(latch,0);
  shiftOut(data, clock, LSBFIRST, number[num3]);
  digitalWrite(latch,1); 

  digitalWrite(digit1,0);
  digitalWrite(digit2,0);
  digitalWrite(digit3,1);
  digitalWrite(digit4,0);
 
 delay(4);
 d++;
   digitalWrite(latch,0);
   shiftOut(data, clock, LSBFIRST, number[num4]);
  digitalWrite(latch,1);
 
  digitalWrite(digit1,0);
  digitalWrite(digit2,0);
  digitalWrite(digit3,0);
  digitalWrite(digit4,1);
 
 delay(4);
d++;//***************************************To here is what i want a function for.
 
z :
if (switch1==1&&button2==0){
 
    if (button3 != lastbutton3){
    if (button3 == 0){
    a1++;
 if (a1>12){
   a1=1;}
}
  }    lastbutton3=button3;
 
  if (button1 != lastbutton1){
     if (button1 == 0){
       a2++;
  if (a2>59){
  a2=0;}
}
  }  lastbutton1=button1;


num1=a1/10;
num2=a1%10;
num3=a2/10;
num4=a2%10;

if (num1>0){
  digitalWrite(latch,0);
   shiftOut(data, clock, LSBFIRST, number[num1]);
  digitalWrite(latch,1);
 
  digitalWrite(digit1,1);
  digitalWrite(digit2,0);
  digitalWrite(digit3,0);
  digitalWrite(digit4,0);
 
 delay(4);
d++;
 
}
   digitalWrite(latch,0);
   shiftOut(data, clock, LSBFIRST, number[num2]);
  digitalWrite(latch,1);
 
  digitalWrite(digit1,0);
  digitalWrite(digit2,2);
  digitalWrite(digit3,0);
  digitalWrite(digit4,0);
 
 delay(4);
d++;

   digitalWrite(latch,0);
   shiftOut(data, clock, LSBFIRST, number[num3]);
  digitalWrite(latch,1);
 
  digitalWrite(digit1,0);
  digitalWrite(digit2,0);
  digitalWrite(digit3,1);
  digitalWrite(digit4,0);
 
 delay(4);
d++;

   digitalWrite(latch,0);
   shiftOut(data, clock, LSBFIRST, number[num4]);
  digitalWrite(latch,1);
 
  digitalWrite(digit1,0);
  digitalWrite(digit2,0);
  digitalWrite(digit3,0);
  digitalWrite(digit4,1);
 
 delay(4);
d++;
}
if (hour()-lastsnooze==1){
  snooze=0;
}

if (hour()==a1&&minute()==a2){
if(snooze>0){goto snooze;
}
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    int noteDuration = 1000/noteDurations[thisNote];
    tone(11, melody[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(11);
 if(button1==0|button2==0|button3==0|button4==0){
      snooze++;
      lastsnooze=hour();
      goto snooze;
    }
  }

 
}else snooze :noTone(11);


y :
if(switch1==1&&button4==0){

  if (button1 != lastbutton1){
    if (button1 == 0){
    m1++;
 if (m1>12){
   m1=1;}
}
  }    lastbutton1=button1;
 
  if (button2 != lastbutton2){
     if (button2 == 0){
       d1++;
  if (d1>31){
  d1=1;}
}
  }  lastbutton2=button2;
 
 num1=m1/10;
 num2=m1%10;
 num3=d1/10;
 num4=d1%10;
   
if (num1>0){

  digitalWrite(latch,0);
  shiftOut(data, clock, LSBFIRST, number[num1]);
  digitalWrite(latch,1);
 
  digitalWrite(digit1,1);
  digitalWrite(digit2,0);
  digitalWrite(digit3,0);
  digitalWrite(digit4,0);
 
  delay(4);
d++;
}
 
  digitalWrite(latch,0);
  shiftOut(data, clock, LSBFIRST, number[num2]);
  digitalWrite(latch,1);
 
  digitalWrite(digit1,0);
  digitalWrite(digit2,1);
  digitalWrite(digit3,0);
  digitalWrite(digit4,0);
 
  delay(4);
d++;

  digitalWrite(latch,0);
  shiftOut(data, clock, LSBFIRST, number[num3]);
  digitalWrite(latch,1); 

  digitalWrite(digit1,0);
  digitalWrite(digit2,0);
  digitalWrite(digit3,1);
  digitalWrite(digit4,0);

  delay(4);
d++;

   digitalWrite(latch,0);
   shiftOut(data, clock, LSBFIRST, number[num4]);
  digitalWrite(latch,1);
 
  digitalWrite(digit1,0);
  digitalWrite(digit2,0);
  digitalWrite(digit3,0);
  digitalWrite(digit4,1);

  delay(4);
d++;
 

 
 
  }
2  Using Arduino / Programming Questions / Writing a library? (Beginner) on: March 31, 2013, 05:49:01 pm
I'll start off with my code:
Code:
  digitalWrite(latch,0);
  shiftOut(data, clock, LSBFIRST, number[num1]);
  digitalWrite(latch,1);
 
  digitalWrite(digit1,1);
  digitalWrite(digit2,0);
  digitalWrite(digit3,0);
  digitalWrite(digit4,0);

So I have a 4 digit, multiplexed, 7 seg display. I'm using a 595 shift register to run it.

I found that I'm using the same block of (the code above) over and over (once for each digit, every time I want to change its display).

I understand that I could make a library to simplify it, but I don't know how. I tried and failed, but I could post it for some critique if you want.

What I want is, at the beginning of the sketch, you define each digits output pin (digit1-digit4), the data, latch and clock pin (for the shift register), and then later have a function where you input which digit(1-4) and what number to shiftOut.

That's possible right? It doesn't sound that hard but I just have no idea what I'm doing.

Any help is greatly appreciated
3  Using Arduino / Networking, Protocols, and Devices / Re: Official Arduino WiFi Shield - Issues - Improvements - Call to action on: January 24, 2013, 03:21:00 pm
How exactly would one use the server.write() to overcome the server going zombie? What would it look in a code?
4  Using Arduino / Networking, Protocols, and Devices / Re: Controlling arduino outside of the same network? on: January 22, 2013, 06:43:59 pm
Umm how would I go about doing that?
5  Using Arduino / Networking, Protocols, and Devices / Controlling arduino outside of the same network? on: January 22, 2013, 06:27:39 pm
So I just got the wifi shield and set up a simple 7-segment display that will display a number based on what number you click. The code is basically the "simple web server" example. Basically you click 9, it displays 9 etc.

It works just fine on all devices on the same network but I tried to get my sister in a different city to try it and it the web page wouldn't load. So I got on my phone, disabled wifi (to use the cellular network) and it wouldn't load either.

So basically I'm asking if there's some special firewall I have to disable, or something I have to add to the code to enable clients from outside the same network.

I'm brand new to the wifi shield but have some experience with arduino and computers in general.
6  Using Arduino / Networking, Protocols, and Devices / Wifi shield trouble on: January 21, 2013, 09:54:34 pm
Just got the Official Arduino Wi-Fi shield and I'm trying to mess around with it. I uploaded the "Connect to an open network" example, put in my networks SSID, and when i check out the serial monitor and i keep getting the WiFi shield not present message.

None of the lights on the shield are lit up but L9 does when i hit the reset button on the shield. I also have the micro usb plugged in if that matters.
7  Using Arduino / General Electronics / Re: Controlling the speed of a dc motor? on: January 12, 2013, 01:03:06 pm
2N3904 is far too puny to control a motor...

Thank you that's the kind of answer I've been looking for. So why will it click on with full PWM duty cycle though? And if it matters its a tiny DC motor, from one of those Zip-Zaps RC cars.

Quote
I'm confusimicated here

As I said the problem isn't in the code. Instead of using a potentiometer to get an analog value I'm using a stereo remote and infrared receiver on the arduino. Each time the "up" button is pushed, it adds 15 to the value that gets set to the PWM. So each button push should make the motor run a little faster.

What happens though is it does nothing until the value reaches 255(%100 duty cycle) and then it will click on.
8  Using Arduino / General Electronics / Re: Controlling the speed of a dc motor? on: January 12, 2013, 12:44:39 am
So, no code and no schematic?
Good luck.

Well the schematic is https://sites.google.com/site/arduinosoapy29/motor-speed-controller/diagram2.png?attredirects=0 and i believe i followed it correctly, however there are a few differences
2k resistor, a 9v battery, a 2N3904 transistor(also an NPN so it shouldn't matter) and instead of a potentiometer for PWM I'm using a button that increases the value by 15 each time it's pushed.

Heres my code...

Code:
#include <Servo.h>
#include <IRremote.h>

int RECV_PIN = 11;

Servo myservo;  // create servo object to control a servo
int val = 130;    // variable to read the value from the analog pin
int motor = 0;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{ Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  irrecv.enableIRIn();
 }
 
void loop() {   
  if (irrecv.decode(&results)){
    Serial.print("motor = ");
    Serial.print(motor);
    Serial.print("\t remotevalue = ");
    Serial.println(results.value);
    analogWrite(3,motor);
 
  if (results.value == 89149445){ //this is the IR code for "UP" button
    motor = motor + 15;}
   
  if (results.value == 83908956){//IR for "down"
    motor = motor - 15;}
     
   
  if (results.value == 83887105){
  val = val - 15;}
 
  if (results.value == 83920001){
  val = val + 15;}
 
  if (results.value == 87556157){
  val = 90;}
 
  myservo.write(val);  // sets the servo position according to the scaled value
 

irrecv.resume(); // Receive the next value

  }
}

9  Using Arduino / General Electronics / Re: Controlling the speed of a dc motor? on: January 10, 2013, 11:37:59 pm
So i set up everything as that example, but with a 2k resistor, a 9v battery, a 2N3904 transistor(also an NPN so it shouldn't matter) and instead of a potentiometer for PWM I'm using a button that increases the value by 15 each time it's pushed.

Nothing happens until the value gets to 255 then it kicks on. Any thoughts that may help me?

I could post the code but I don't think that's where the problem is.
10  Using Arduino / General Electronics / Re: Controlling the speed of a dc motor? on: January 09, 2013, 05:45:13 pm
I have 2n3904 transistors (if that matters) and everything else. Do I need to use a resistor before the transistor? How will it effect it?
11  Using Arduino / General Electronics / Re: Controlling the speed of a dc motor? on: January 09, 2013, 05:29:01 pm
I was planning on using the PWM im just not exactly sure how to wire it up safely. What about this https://sites.google.com/site/arduinosoapy29/motor-speed-controller schematic? this is more what i want because im not using a potentiometer.
12  Using Arduino / General Electronics / Re: Controlling the speed of a dc motor? on: January 09, 2013, 04:26:06 pm
Yes I checked out this-->http://playground.arduino.cc//Main/DCMotorControl one and I don't care about controlling the direction of the motor just the speed. I'm fairly new and don't really understand the diagrams that are on there.
13  Using Arduino / General Electronics / Controlling the speed of a dc motor? on: January 09, 2013, 03:52:11 pm
I know this has been asked before but I'm trying to control the speed of a dc motor with my arduino.

I looked around in the forum and found that I should use a transistor but I'm not exactly sure how transistors work or how I should wire it.
14  Using Arduino / Programming Questions / Re: Would I use analogRead to incorporate a joystick? on: December 31, 2012, 10:14:35 am
OK, so I looked up the Nintendo 64 controllers->http://en.wikipedia.org/wiki/Nintendo_64_controller and according to wikipedia, they dont use a normal potentiometer. They use a light and light sensor to get the position of the switch.

I took apart an old controller, took out the joystick and the "potentiometer" controlling it, and wired to a dc motor. When its all the way down, no power. And as you twist it hits a hot spot in the middle then goes to about half power when all the way up (If that makes sense).

Also i checked out using the NES controller, is the any tutorial for a 64 controller?
15  Using Arduino / Programming Questions / Re: Would I use analogRead to incorporate a joystick? on: December 30, 2012, 02:13:44 pm
That is what figured because there are 2 sets of wires coming from it.

What does HTH mean?
Pages: [1] 2