0

Can someone please help me fix my code?

Basically, a user inputs values that determine how many beeps will play from a breadboard-compatible speaker, and right after the beeps are played, a motor (connected to the breadboard) is supposed to turn.

The beep-code is mine, and I got the motor code from the Arduino website. Anyway, SEPARATELY the codes work - the beeps play from the speaker, the motor turns fine. However, the codes don't work when I combine them (beeps play, but motor does not run after beeps are played), and I have no idea how to fix it. Any help is appreciated. Thanks so much.

int pin = 3;
#include <Stepper.h>

void setup() {
  Serial.begin(9600);
}

void loop() {
  int bpmeasure, measurespg, bpminute;
  Serial.println("Enter number of beats per measure:");
  while (Serial.available() == 0) {};
  bpmeasure = Serial.parseInt();
  Serial.println(bpmeasure);
  Serial.println("Enter number of measures on page:");
  while (Serial.available() == 0) {};
  measurespg = Serial.parseInt();
  Serial.println(measurespg);
  Serial.println("Enter tempo:");
  while (Serial.available() == 0) {};
  bpminute = Serial.parseInt();
  Serial.println(bpminute);
  float tppg = (bpmeasure * measurespg * 60000.0) / bpminute;
  // time it takes to play a page, in milliseconds
  float tpbeat = tppg / (bpmeasure * measurespg);
  // time between each individual note, in milliseconds
  float t = 0.0;
  for (float t = 0.0; t <= tppg ; t += tpbeat) {
    tone (pin, 30, 50);
    delay (tpbeat+50);
  }
  // speaker is supposed to beep every 'tpbeat' amount of milliseconds,
  // until it reaches 'tppg' milliseconds and then speaker should stop beeping
  const int stepsPerRevolution = 900;
  Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
  int stepCount = 0;
  // number of steps the motor has taken
  if (stepCount = 0) {
    // read the sensor value:
    int sensorReading = analogRead(A0);
    // map it to a range from 0 to 100:
    int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
    // set the motor speed:
    if (motorSpeed > 0) {
      myStepper.setSpeed(motorSpeed);
      // step 1/100 of a revolution:
      myStepper.step(stepsPerRevolution / 100);
    }
  }
}
11
  • Not very clear what you want to do... Do you want the board to stay still until you write through serial, then play the beats, then when they finish run the motor? And until when should it rotate? Anyway try to implement it yourself, then see what is wrong and come back asking "I did this, I expected that but got this other thing. Why?"
    – frarugi87
    Commented Feb 9, 2017 at 8:49
  • Right now I'm not worrying about making the motor stop rotating, I just want it to do so after the beats are played
    – beee
    Commented Feb 9, 2017 at 12:32
  • Yes, but if you do not stop the motor and return to the initial idle state you will have to reset or turn off/on the arduino to enter the wait state again.. but if you don't care, what did you try? because in my opinion just copy-pasting the two programs one after the other and joining the loops will do that..
    – frarugi87
    Commented Feb 9, 2017 at 12:46
  • I am aware I have to reset the arduino. I did just copy and paste the 2 programs into the same loop, and it didn't work. I don't know what I'm doing wrong ;-;
    – beee
    Commented Feb 9, 2017 at 15:17
  • 1
    Ok, now that you joined them you are ready for the second step, which is "see what is wrong and come back asking "I did this, I expected that but got this other thing. Why?"". Which means, post the complete code, your expectations and your actual results...
    – frarugi87
    Commented Feb 9, 2017 at 15:19

1 Answer 1

3

The first step with combining any programs together is to forget about combining programs together. Instead you need to do something completely different.

Analyse the existing programs individually. Work out and learn exactly how they work and how they do what they do.

Then think about your program. What do you actually want it to do, and how does that relate to the things the other programs did.

Then, finally, using what you learned from analysing the other programs, you write your own new program that does what you want it to do.

Yes, there may be a certain amount of copy-and-paste from the old programs to the new, but until you have taken the time to learn how the old programs work you won't have a clue which parts of them you may want to copy-and-paste, and which you may want to use as inspiration for writing new portions of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.