@tadashi-hamada
please read this advice very carefully and then take this advice to your heart and follow it from now on:
Each time you write a posting take 5 minutes more time to save 55 minutes of time
The posting above is unprecise !
You don't have a function named "initialization" !
You have a function named "Initialize()"
very unprecise too ! "The prizm and the ps4"
what the prizm and the ps4
You may think that I am a very excessive pedant. No me personally I'm not. But programming is a very excessive pedant. Everything has to be correct down to each single letter to make it work.
You have no "Control function" you have a function named "Main".
But it is still unclear what exactly did you comment out
Answering this could be very easy seen if you would have posted your actual and complete sketch.
You can post even three ore more code-sections into a single posting. Code-Sections have a limited height and having the real code is a huge advantage and has unbeatable detail-information over any description of lines of code and it takes maybe 20 seconds to post a complete sketch with the "copy-for-forum-function" of the arduino-IDE
headline: Add one thing at a time - then test IMMIDIATELY
You have put together
- OLED display
- temperature sensor
- ps4
- servos
these are four things. Not a single one.
This means there are much more possabilities what could cause a malfunction than if you only put into your code one single thing and then test immidiately.
headline: put together code into senseful sub-units = functions
you have mutliple lines of code that do something with the motors
specifically these lines that handle the motors should be put together into their own function
headline: give each function and each variable a SELF-explaining name
all names that you use in your code should be SPOT-ON self-explaining what these lines of code do. This requires taking some time to think over it what would be a spot-on selfexplaining name. Take once time to find a good self-explaining name to save time each time you read your code because then the name does explain itself.
After analysing your code:
You use a OLED-display to show information.
You use a ps4-controller to
- control motors
- control servos
compare your original setup-function
void setup() {
//Initialize OLED screen
Oled.begin();
Oled.setFlipMode(true); // Sets the rotation of the screen
//Initialize sensors
Environment.begin();
//Start Serial Monitor
Serial.begin(115200);
//Initialize controls
prizm.PrizmBegin(); // Intializes the PRIZM controller and waits here for press of green start button
//Deadzone
ps4.setDeadZone (LEFT, 10);
ps4.setDeadZone (RIGHT, 10);
}
with this setup-function
void setup() {
Serial.begin(115200); // ABSOLUTE first thing to code serial monitor
Serial.println("setup-Start");
initOled(); // no comments needed because has a SELF-explaining name
initSensors(); // no comments needed because has a SELF-explaining name
initps4(); // no comments needed because has a SELF-explaining name
}
and the init-functions
void initOled() {
Oled.begin();
Oled.setFlipMode(true); // Sets the rotation of the screen
Oled.setFont(u8x8_font_chroma48medium8_r);
}
void initSensors() {
Environment.begin();
}
void initps4() {
prizm.PrizmBegin(); // Intializes the PRIZM controller and waits here for press of green start button
//Deadzone
ps4.setDeadZone (LEFT, 10);
ps4.setDeadZone (RIGHT, 10);
}
void initServos() {
prizm.setServoSpeed(1, 75);
prizm.setServoSpeed(2, 75);
prizm.setServoSpeed(3, 75);
}
needs a little bit more time to write the functions but after that it is very comfortable to work with
very easy to follow structure of the code with self-explaining names
commenting out reduces to a single line of code which is very easy to find
void setup() {
Serial.begin(115200); // ABSOLUTE first thing to code serial monitor
Serial.println("setup-Start");
initOled();
//initSensors();
//initps4();
//initServos();
}
First test is to add test-text direct below initOled();
void setup() {
Serial.begin(115200); // ABSOLUTE first thing to code serial monitor
Serial.println("setup-Start");
initOled();
Serial.println("initOled() done");
Oled.setCursor(0, 0);
Oled.print("Test direct below initOled");
Oled.display();
Serial.println("Does OLED show Test direct below initOled???");
//initSensors();
//initps4();
}
next step is to add initps4(); (= uncommenting initps4(); )
and test again does displaying text on the OLED work?
void setup() {
Serial.begin(115200); // ABSOLUTE first thing to code serial monitor
Serial.println("setup-Start");
initOled();
Serial.println("initOled() done");
Oled.setCursor(0, 0);
Oled.print("Test direct below initOled");
Oled.display();
Serial.println("Does OLED show Test direct below initOled???");
initps4();
Serial.println("initps4() done");
Oled.setCursor(0, 2);
Oled.print("Test after initps4()");
Oled.display();
Serial.println("Does OLED show Test after initps4()???");
//initSensors();
//initServos();
}
in parallel the code always prints to the serial monitor how far the code-execution is done
remember how many times you tried this, you tried that, tried this, you tried that, tried this, you tried that, tried this, you tried that, and how much time this more or less randomly trying this, trying that consumed without proceeding.
If you remember this wasting time
I hope you can see that such a very well structured way to write the code with functions and a very well structured way of adding a few lines of code and then test again immitiately saves time in the long run.
Next step is to add a few lines of code that read in data from your ps4-controller and then
directly print this data to the OLED and in parallel to the serial monitor
no motordriving yet! direct printing of the read-in data
here is your code re-organised in functions with self-explaining names
All parts that build a senseful sub-unit are put into their own function with a self-explaining name.
So your function "myMain" looks like this
void myMain() {
// Get (read) PS4 connections status
// and all PS4 button and joystick data value
// this function-call must be done very often
// otherwise you can't control
ps4.getPS4();
if (ps4.Connected) { // If PS4 controller has been succesfully connected, control motors
updateTemperatureLEDs(); // no comment needed has SELF-explaining name
setMotorPower(); // no comment needed has SELF-explaining name
stopServos(); // no comment needed has SELF-explaining name
manageElevatorPinion(); // no comment needed has SELF-explaining name
manageGripper(); // no comment needed has SELF-explaining name
manageSupportPinion(); // no comment needed has SELF-explaining name
}
else { // If PS4 is not connected, stop motors and servos
prizm.setMotorPowers(0, 0);
stopServos();
Serial.println("ps4.Connected = false PS4 NOT connected");
Serial.println("Motors and servos stopped");
delay(2000);
}
}
loop does nothing yet because we are in the stage of testing does the OLED show data after
initps4()?
here is the complete code not tested if it does compile because I don't have your libraries
There might be some minor typos in the code the compiler might complain about
/* Motor Channel 1 is controlled by PS4 Left Joystick Y axis
Motor Channel 2 is controlled by PS4 Right Joystick Y axis
Servo 1 is controlled by the Triangle and Cross Buttons
Servo 2 is controlled by the Right Trigger Button and the Left Trigger Button
Servo 3 is controlled by the Square and Circle Buttons
Hardware: TETRIX PRIZM, TeleOp module and SONY PS4 gaming controller, Grove - OLED Display 0.96.
Date: 02/03/2023
Author: BenElyon Molgeri
*/
#include "Arduino_SensorKit.h" // Arduino Sensorkit library
#include "TELEOP.h" // TETRIX TeleOp module Library
#include "PRIZM.h" // TETRIX PRIZM Library
//Library class instance creation
PRIZM prizm; // Create an instance within the PRIZM Library class named prizm
PS4 ps4; // Create an instance within the PS4 Library class named ps4
//Motor
int PowerM1; // Channel 1 Motor Power
int PowerM2; // Channel 2 Motor Power
//Servo
int Servo1 = 90; // variables for each servo channel set initially to 90
int Servo2 = 90;
int Servo3 = 90;
//Sensor
int Temp = Environment.readTemperature();
int roomTemp = 20;
void initOled() {
Oled.begin();
Oled.setFlipMode(true); // Sets the rotation of the screen
Oled.setFont(u8x8_font_chroma48medium8_r);
}
void initSensors() {
Environment.begin();
}
void initps4() {
prizm.PrizmBegin(); // Intializes the PRIZM controller and waits here for press of green start button
ps4.setDeadZone (LEFT, 10);
ps4.setDeadZone (RIGHT, 10);
ps4.getPS4(); // Get (read) PS4 connections status and all PS4 button and joystick data value
}
void initServos() {
prizm.setServoSpeed(1, 75);
prizm.setServoSpeed(2, 75);
prizm.setServoSpeed(3, 75);
}
void setup() {
// ABSOLUTE first thing to code
// init serial monitor
Serial.begin(115200);
Serial.println("setup-Start");
initOled();
Serial.println("initOled() done");
Oled.setCursor(0, 0);
Oled.print("Test direct below initOled");
Oled.display();
Serial.println("Does OLED show Test direct below initOled???");
initps4();
Serial.println("initps4() done");
Oled.setCursor(0, 2);
Oled.print("Test after initps4()");
Oled.display();
Serial.println("Does OLED show Test Test after initps4()???");
//initServos();
//initSensors();
}
void myMain() {
// Get (read) PS4 connections status
// and all PS4 button and joystick data value
// this function-call must be done very often
// otherwise you can't control
ps4.getPS4();
if (ps4.Connected) { // If PS4 controller has been succesfully connected, control motors
updateTemperatureLEDs();
setMotorPower();
stopServos();
manageElevatorPinion();
manageGripper();
manageSupportPinion();
}
else { // If PS4 is not connected, stop motors and servos
prizm.setMotorPowers(0, 0);
stopServos();
Serial.println("ps4.Connected = false PS4 NOT connected");
Serial.println("Motors and servos stopped");
delay(2000);
}
}
void Diagnostics() {
Oled.setCursor(0, 0);
Oled.print("Droid Name:");
Oled.print("Gimli");
Oled.setCursor(0, 2);
Oled.print("Status: Primed");
Oled.setCursor(0, 3);
Oled.print("Voltage mV: ");
Oled.print(prizm.readBatteryVoltage());
Serial.print("Motor 1 Current Draw:");
Serial.print(prizm.readMotorCurrent(1));
Serial.print("Motor 2 Current Draw:");
Serial.print(prizm.readMotorCurrent(2));
Oled.setCursor(0, 4);
Oled.print("Servo Temp = ");
Oled.print(Environment.readTemperature()); //print temperature
Oled.println(" C");
Oled.display();
}
void loop() {
// Diagnostics();
// myMain();
}
void updateTemperatureLEDs() {
Temp = Environment.readTemperature();
if (Temp <= roomTemp) {
ps4.setLED(BLUE);
}
else if (Temp >= roomTemp + 2.5) {
ps4.setLED(GREEN);
}
else if (Temp >= roomTemp + 5) {
ps4.setLED(YELLOW);
}
else if (Temp >= roomTemp + 7.5) {
ps4.setLED(RED);
}
}
void setMotorPower() {
PowerM1 = ps4.Motor(LY); // Power (speed) and direction of Motor 1 is set by position of Left Y-axis Joystick
PowerM2 = ps4.Motor(RY); // Power (speed) and direction of Motor 2 is set by position of Right Y-axis Joystick
prizm.setMotorPowers(PowerM1, PowerM2); // set motor output power levels for PRIZM DC motor channels 1 and 2
}
void stopServos() {
prizm.setServoPosition(1, 90); // Leaves 360 degree servo on hold
prizm.setServoPosition(2, 90);
prizm.setServoPosition(3, 90);
}
void manageElevatorPinion() {
if (ps4.Button(CROSS)) {
Servo1 = ps4.Servo(CROSS);
prizm.setServoPosition(1, 0);
}
else if (ps4.Button(TRIANGLE)) {
Servo1 = ps4.Servo(TRIANGLE);
prizm.setServoPosition(1, 180);
}
else {
prizm.setServoPosition(1, 90);
}
}
void manageGripper() {
if (ps4.Button(R1)) {
Servo2 = ps4.Servo(R1);
prizm.setServoPosition(2, 0);
}
else if (ps4.Button(L1)) {
Servo2 = ps4.Servo(L1);
prizm.setServoPosition(2, 180);
}
else {
prizm.setServoPosition(2, 90);
}
}
void manageSupportPinion() {
if (ps4.Button(DOWN)) {
Servo3 = ps4.Servo(DOWN);
prizm.setServoPosition(3, 0);
}
else if (ps4.Button(UP)) {
Servo3 = ps4.Servo(UP);
prizm.setServoPosition(3, 180);
}
else {
prizm.setServoPosition(3, 90);
}
}
best regards Stefan