Go Down

Topic: How would you do this? (Read 1 time) previous topic - next topic

Legot

Thanks! So I've fixed my code with the modifications that you all suggested.

Unfortunately I still have a problem, from what I can see, all of the if/else functions(?) are conflicting with one another, and that is pushing my output pins to a state that's not quite high, but it's also not quite low (I'm using LED's to test it). However, I can still see which pins are going higher and lower with the inputs, so at least some part of it is working.

I was thinking about using a counter type of function to keep track of which  button was selected last, but I think that this could come up with errors after multiple presses. So, if you can suggest an effective way to store the last pressed button please share!

Thank you again.

PaulS

Quote
So I've fixed my code with the modifications that you all suggested.

But, you're not going to show it to us, but you still want help. I'll leave it to you to imagine the amount of help you'll get without, or with, the code.

Quote
and that is pushing my output pins to a state that's not quite high, but it's also not quite low

Not physically possible. The outputs may not be remaining at the state you want long enough, but there is only HIGH or LOW. Nothing in between (for digital pins).

Quote
So, if you can suggest an effective way to store the last pressed button please share!

Trivially easy, or just about impossible, depending on YOUR code. (See above).

Graynomad

Quote
that is pushing my output pins to a state that's not quite high,

Not possible unless there is way too much load on the pins, in which case you are damaging the Arduino.

Quote
I'm using LED's to test it

Looking at a LED is not a test, it could be doing anything at 10000Hz (or whatever) and you wouldn't know.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

majenko

Writing a HIGH to a pin set as INPUT enables the pull-up resistors.

This can seem, when examining the pin, as if the pin is an output but is "almost" high.

Make sure the pin modes are right for your pins.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Legot

Sorry I forgot to post the code.
Code: [Select]
const int sinpin = 2;   // Sine Wave button
const int squpin = 3;    // Square Wave button
const int tripin = 4 ;   // Triangle Wave button

const int sine = 12;     // Sine Wave output
const int tri = 13;      // Triangle Wave output

void setup () {
  pinMode(sine, OUTPUT);
  pinMode(tri, OUTPUT);
  pinMode(sinpin, INPUT_PULLUP);    //Sets Internal Pullups
  pinMode(squpin, INPUT_PULLUP);
  pinMode(tripin, INPUT_PULLUP);
}

int Sinstate () { return digitalRead(sinpin); }
int Squstate () { return digitalRead(squpin); }
int Tristate () { return digitalRead(tripin); }
   
   
void loop(){
 
      if (Sinstate() == LOW) {            //Sine Things
        digitalWrite(sine, LOW);
        digitalWrite(tri, HIGH); }
      else {
        if (Sinstate() == HIGH) {
          if (Squstate() == LOW) {
            digitalWrite(sine, LOW);
            digitalWrite(tri, HIGH); }
          if (Tristate() == HIGH) {
            digitalWrite(sine, LOW);
            digitalWrite(tri, HIGH); }
             }
           }

           
       
      if (Squstate() == LOW) {            //Square Things
        digitalWrite(sine, HIGH);
        digitalWrite(tri, HIGH); }
      else {
        if (Squstate() == HIGH) {
          if (Sinstate() == HIGH) {
            digitalWrite(sine, HIGH);
            digitalWrite(tri, HIGH); }
          if (Tristate() == HIGH) {
            digitalWrite(sine, HIGH);
            digitalWrite(tri, HIGH); }
            }
          }
       
       
      if (Tristate() == LOW) {            //Triangle Things
         digitalWrite(sine, HIGH);
         digitalWrite(tri, LOW); }
      else {
        if (Tristate() == HIGH) {
          if (Squstate() == HIGH) {
            digitalWrite(sine, HIGH);
            digitalWrite(tri, LOW); }
          if (Sinstate() == HIGH) {
            digitalWrite(sine, HIGH);
            digitalWrite(tri, LOW); }
            }
          }
}



Looking at a LED is not a test, it could be doing anything at 10000Hz (or whatever) and you wouldn't know.

______
Rob

I just checked on an oscilloscope since you mentioned it, and the two output pins are oscillating at 10.53khz and getting faster and slower when I hit the input buttons!

Go Up