Just initializing with an unknown. Maybe there is a difference in "val" and "valOld" which would print a value, but does not effect the results.
If you don't initialize sensorValOld, then its initial value will be random zero, so the conditional if (sensorValOld != sensorVal) will have an unpredictable undesirable result.* And if some wiring error (or other hardware issue) causes sensorVal to be stuck at a value that happens to match the initial sensorValOld value (e.g., if both are 0), then your Serial.print statements will never execute.
*Edit: Corrected one sentence to account for documented initialization behavior of global variables. This does not change the fact that the Serial.print statements will never execute if analogRead(sensorPin) always returns 0. Nor the fact that a declaration statement like int sensorVal = 0, sensorValOld; is inconsistent coding.
I know.
I predict it will have zero effect on the purpose of the sketch.
I predict the sketch will show that with a constant stream of numbers or none at all.
9 for the servo project
Wrong. Read the sketch.
I will try this asap I wanted to thank you ! my life is kind of unmanageable i have to catch up innn this thread seriously nice to see activity on this ....im staying in a barn with little to no resources so sometimes life gets little hectic with my miserable problems ....Thank you guys very much for your help I promise its not in vain
Your if (sensorValOld != sensorVal) conditional prevents the Serial.print statements from being executed unless (sensorValOld != sensorVal) is true. However, (0 != 0) will always be false. Therefore, this scenario would result in your Serial.print statements never executing, which is what I had said previously, and which is still a valid criticism (not "wrong").
The pinout table that you've posted in Comment #54 contradicts the pin numbers shown in the circuit diagram that you hade posted in Comment #17. I foresee much (further) confusion in this thread, unless you can clarify the actual circuit you are using (including any connections that have been made between the Bastl Instruments circuit and your Arduino board).
All further replies on this topic will be focused exclusively on assisting @newnumbertwo. The technical aspects of the code post #36 have already been discussed sufficiently, so no more discussion is needed on that subject.
If I see any more quarreling about that, account suspensions will be given.
@newnumbertwo - Okay... you want to use the audio output from Pin 9 [I think you want pin 10] of your schematic to move the mouth.
You will need to find the BIT VALUE RANGE of that analog signal from "Pin 9" [again, I think you want pin 10] of your schematic, [INSURE THE VOLTAGE RANGE IS 0VDC TO 5VDC, OR 3.3VDC IF USING ESP MCU] and connect it to AIN_0, then take a guess at the width of the mouth opening when BIT VALUES are reached. That is to say, if the bit range of the analog input is 0 to 1023, soft talking might only reach a value of 250, and loud talking might reach to the full 1023, but it is a good idea to "map()" that value a little lower to avoid flat-lining the input. Next, take the value you receive and write it to the servo as an angle. Finally, close the mouth (servos).
For example, in this "servoGotchi" I am using a random value from reading AIN_0, "mapping" it to a value 0 to 5, multiplying that mapped value by 2 or 5 degrees (upper lip moves less than lower lip) and sending the result as an angle to the two "lip" servos.
In the code (below, and in the simulation), you will see the "mouth" section where I execute the above description of mouth movement.
I hope this helps your project.
Here is the simulation:
sketch.ino
#include <Servo.h>
Servo eyes1;
Servo eyelids2;
Servo upperLip;
Servo lowerLip;
byte lidServoPin = 9, eyeServoPin = 10, uplipServoPin = 5, lolipServoPin = 6;
int lidOpen = 75;
int lidClose = 105;
int eyesLeft = 45;
int eyesCenter = 90;
int eyesRight = 135;
int mouthClosed = 90;
char openLeft[] = "[o ] [o ]";
char closeLeft[] = "[_ ] [_ ]";
char openRight[] = "[ o] [ o]";
char closeRight[] = "[ _] [ _]";
char openCenter[] = "[ o ] [ o ]";
char closeCenter[] = "[ _ ] [ _ ]";
char mouthShape[] = "=<";
int upLipAngle = 5; // upper lip opens less
int loLipAngle = 10; // lower lip opens more
// two timers for two events (left/right) and (open/close)
unsigned long timerEyes, timerMouth, intervalEyes = 1000, intervalMouth = 200;
void setup() {
randomSeed(analogRead(A0)); // create pseudo randomness
Serial.begin(115200);
eyes1.attach(eyeServoPin);
eyelids2.attach(lidServoPin);
upperLip.attach(uplipServoPin);
lowerLip.attach(lolipServoPin);
}
void loop() {
byte state = random(3); // eye states: left, center, right
// mouth
if (millis() - timerMouth > intervalMouth) {
int lips = map(analogRead(A0), 0, 1023, 0, 5); // closed to wide open
timerMouth = millis();
upperLip.write(mouthClosed + lips * upLipAngle); // upper lips open less
lowerLip.write(mouthClosed - lips * loLipAngle); // lower lip opens more
delay(100);
upperLip.write(mouthClosed);
lowerLip.write(mouthClosed);
}
// eyes
if (millis() - timerEyes > intervalEyes) { // check timer
timerEyes = millis(); // reset timer
switch (state) {
case 0: lookLeft(); break;
case 1: lookRight(); break;
case 2: lookCenter(); break;
}
}
delay(50); // let relays arrive
}
void lookLeft() {
Serial.print(openLeft);
Serial.println(" left");
eyes1.write(eyesLeft); //left
if (!random(3)) { // one in three chances
blinkLeft();
}
}
void lookRight() {
Serial.print(openRight);
Serial.println(" right");
eyes1.write(eyesRight); //right
if (!random(3)) {
blinkRight();
}
}
void lookCenter() {
Serial.print(openCenter);
Serial.println(" center");
eyes1.write(eyesCenter); // center
if (!random(3)) {
blinkCenter();
}
}
void blinkLeft() {
delay(500);
Serial.print(closeLeft);
Serial.println(" left closed");
eyelids2.write(lidClose); //close
delay(250);
Serial.print(openLeft);
Serial.println(" left open");
eyelids2.write(lidOpen); //open
delay(500);
}
void blinkRight() {
delay(500);
Serial.print(closeRight);
Serial.println(" right closed");
eyelids2.write(lidClose); //close
delay(250);
Serial.print(openRight);
Serial.println(" right open");
eyelids2.write(lidOpen); //open
delay(500);
}
void blinkCenter() {
delay(500);
Serial.print(closeCenter);
Serial.println(" center closed");
eyelids2.write(lidClose); //close
delay(250);
Serial.print(openCenter);
Serial.println(" center open");
eyelids2.write(lidOpen); //open
delay(500);
}
diagram.json
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
{ "type": "wokwi-servo", "id": "servo1", "top": -88.4, "left": 76.8, "attrs": {} },
{
"type": "wokwi-servo",
"id": "servo2",
"top": -154.6,
"left": -55.8,
"rotate": 270,
"attrs": {}
},
{
"type": "wokwi-text",
"id": "legendservo1",
"top": -230.4,
"left": 9.6,
"attrs": { "text": "EYES" }
},
{
"type": "wokwi-text",
"id": "legendservo2",
"top": -76.8,
"left": 153.6,
"attrs": { "text": "EYELIDS" }
},
{
"type": "wokwi-text",
"id": "legendservo3",
"top": -172.8,
"left": -38.4,
"attrs": { "text": "[oo_]" }
},
{
"type": "wokwi-text",
"id": "legendservo4",
"top": -172.8,
"left": 67.2,
"attrs": { "text": "[_oo]" }
},
{
"type": "wokwi-text",
"id": "legendservo5",
"top": -76.8,
"left": 230.4,
"attrs": { "text": "[ o ]" }
},
{
"type": "wokwi-text",
"id": "legendservo6",
"top": -9.6,
"left": 230.4,
"attrs": { "text": "[ _ ]" }
},
{
"type": "wokwi-servo",
"id": "servo3",
"top": 26,
"left": -160.4,
"rotate": 180,
"attrs": {}
},
{
"type": "wokwi-servo",
"id": "servo4",
"top": -22,
"left": -160.4,
"rotate": 180,
"attrs": {}
},
{
"type": "wokwi-text",
"id": "legendservo7",
"top": -9.6,
"left": -144,
"attrs": { "text": "UPPER LIP" }
},
{
"type": "wokwi-text",
"id": "legendservo8",
"top": 105.6,
"left": -144,
"attrs": { "text": "LOWER LIP" }
},
{
"type": "wokwi-text",
"id": "legendservo9",
"top": -211.2,
"left": 9.6,
"attrs": { "text": "[o o]" }
}
],
"connections": [
[ "nano:10", "servo2:PWM", "green", [ "v-9.6", "h-0.2" ] ],
[ "nano:GND.3", "servo1:GND", "black", [ "v0", "h-86.4", "v-38.4" ] ],
[ "nano:GND.3", "servo2:GND", "black", [ "v0", "h-134.4" ] ],
[ "nano:5V.2", "servo2:V+", "red", [ "v0", "h-57.6" ] ],
[ "nano:5V.2", "servo1:V+", "red", [ "v0", "h-96", "v-76.8" ] ],
[ "nano:9", "servo1:PWM", "green", [ "v0" ] ],
[ "nano:GND.3", "servo4:GND", "black", [ "v0", "h28.8", "v67.2" ] ],
[ "nano:GND.3", "servo3:GND", "black", [ "v0", "h28.8", "v124.8" ] ],
[ "nano:5V.2", "servo4:V+", "red", [ "v0", "h19.2", "v38.4" ] ],
[ "nano:5V.2", "servo3:V+", "red", [ "v0", "h19.2", "v86.4" ] ],
[ "nano:5", "servo4:PWM", "green", [ "v0" ] ],
[ "nano:6", "servo3:PWM", "green", [ "v0" ] ]
],
"dependencies": {}
}
For reference:
